iOS chart framework Charts

1. github sample image

Please add a picture description

Please add a picture description

2. GitHub URL of Charts

Charts - github

3. After downloading on github, take a screenshot of running the demo on the mobile phone

Please add a picture description

Please add a picture description

4. How to use

iOS-Charts is enough to see this- Rare Earth Nuggets

4.1 Bar Chart - BarChartView

4.1.2 Showing sleep with a bar graph

Please add a picture description

  • To achieve the above effect
 func addBarChart(){
    
    
        let chartView = HorizontalBarChartView(frame: CGRect(x: 0, y: 300, width: view.frame.size.width, height: 100))
        view.addSubview(chartView)
        chartView.backgroundColor = .yellow
        
        let xAxis = chartView.xAxis
        xAxis.enabled = false
        
        let leftAxis = chartView.leftAxis
        leftAxis.enabled = false
        
        let rightAxis = chartView.rightAxis
        rightAxis.enabled = false
        
        //最大值到顶部所占整个轴的百分比,默认0.1
        leftAxis.spaceTop = 0.01
        //最小值到底部所占整个轴的百分比,默认0.1
        leftAxis.spaceBottom = 0.01
        
        //是否显示图例
        chartView.legend.enabled = false
        
        //浅,green 7
        //深,red  2
        //醒,black  1
        //data
        let yVals = [
            BarChartDataEntry(x: 0, yValues: [7,2,1]),
            BarChartDataEntry(x: 1, yValues: [7,2,1])
        ]
        
        let set = BarChartDataSet(entries: yVals)
        set.drawValuesEnabled = false
        set.colors = [NSUIColor.green,NSUIColor.yellow,NSUIColor.yellow,
                      NSUIColor.yellow,NSUIColor.red,NSUIColor.black]
        
        let data = BarChartData(dataSet: set)
        //圆柱和间距的比例,默认0.85
        data.barWidth = 1
        chartView.data = data
        
    }

  • The effect of the above code is as follows
    Please add a picture description

Reference blog:
Simple use of iOS Charts library - BarChartView

4.2 Line Chart - LineChartView

Swift - Third-party chart library Charts use detailed explanation 8 (line chart 7: event response, MarkerView label)

4.3 Pie Chart (Pie Chart) - PieChartView

Please add a picture description

override func viewDidLoad() {
    
    
        super.viewDidLoad()
        
        view.backgroundColor = .white

        let pieChartView = PieChartView(frame: CGRect(x: 0, y: 50, width: 200, height: 200))
        view.addSubview(pieChartView)
        pieChartView.backgroundColor = .gray
        
        //中心文字
        pieChartView.centerText = "睡眠占比"
        //是否显示图例
        pieChartView.legend.enabled = false
        
        let datas: [Double] = [32, 100, 65]

        var values = [PieChartDataEntry]()
        for i in 0..<datas.count {
    
    
            values.append(PieChartDataEntry(value: datas[i], label: "\(Int(datas[i]))分"))
            //            let entry = PieChartDataEntry(value: datas[i])
//            values.append(entry)
        }

        let dataSet = PieChartDataSet(entries: values, label: "图例")
        //是否显示数值。默认true
        dataSet.drawValuesEnabled = false

        dataSet.colors = [NSUIColor.black,NSUIColor.red,NSUIColor.green]
        
        

        let data = PieChartData(dataSet: dataSet)

        pieChartView.data = data
        
    }

Reference blog:
Simple use of iOS Charts library - PieChartView

5. Problems encountered

Question one

  • 1. When the bar chart is clicked, the proxy method chartValueSelected is not called (only a few will be called), and the proxy method chartValueNothingSelected is always called

The downloaded demo does not have this problem, but the demo I wrote after the pod has this problem. It should be that the charts library of the pod has a bug

Solution, download the 4.1.0 demo, and then manually import Charts

Guess you like

Origin blog.csdn.net/baidu_40537062/article/details/130362266