如何在 BarCharts IOS 图表中创建多个数据集

How to create multiple data sets in BarCharts IOS Charts

我创建了两个数据集,chartDataSet 和 limitDataSet。

    var dataEntries = [BarChartDataEntry]()
    var limitEntries = [BarChartDataEntry]()

    for i in 0..<daily[index].count {
        let dataEntry = BarChartDataEntry(value: values[index][i], xIndex: i)
        let limitEntry = BarChartDataEntry(value: limitValues[index][i], xIndex: i)
        dataEntries.append(dataEntry)
        limitEntries.append(limitEntry)
    }

    let chartDataSet = BarChartDataSet(yVals: dataEntries, label: label)
    chartDataSet.colors = [UIColor(red: 108/255, green: 189/255, blue: 146/255, alpha: 1)]
    let limitDataSet = BarChartDataSet(yVals: limitEntries, label: "Limit in mg")
    limitDataSet.colors = [UIColor(red: 219/255, green: 70/255, blue: 70/255, alpha: 1)]

    var dataSets = [BarChartDataSet]()
    dataSets.append(chartDataSet)
    dataSets.append(limitDataSet)

    //this line does has error
    let chartData = BarChartData(xVals: daily[index], dataSet: dataSets)

    monthlyChart.data = chartData

我需要在我的条形图中添加数据集,但我使用的代码仅适用于单个数据集。

这是我在项目中使用的图表。 https://github.com/danielgindi/Charts

我还没有在 BarChart 上添加 2 个数据集。

但我已经用 PieChart 做到了。

import UIKit

class ViewController: UIViewController {

    
        @IBOutlet weak var pieChartView: PieChartView!
        override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
        let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
        
        setChart(months, values: unitsSold)
            
        setChart2(months, values: unitsSold)
            
        
    }

    
    func setChart(dataPoints: [String], values: [Double]) {
        
        var dataEntries: [ChartDataEntry] = []
        
        for i in 0..<dataPoints.count {
            let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
            dataEntries.append(dataEntry)
        }
        
        let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "Units Sold")
        
        let formatter = NSNumberFormatter()
        formatter.numberStyle = .PercentStyle
        formatter.maximumFractionDigits = 1
        formatter.multiplier = 1.0
      
        
        let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)

        pieChartData.dataSet?.valueFormatter = formatter
        
        
        pieChartView.data = pieChartData
       
        pieChartView.holeColor = UIColor.clearColor()
      
        pieChartView.holeRadiusPercent = 0.95
        
        pieChartView.centerText =  "Hello\nThis is Pie chart"

 pieChartView.usePercentValuesEnabled = true
        
        
        var colors: [UIColor] = []
        
        for i in 0..<dataPoints.count {
            let red = Double(arc4random_uniform(255))
            let green = Double(arc4random_uniform(255))
            let blue = Double(arc4random_uniform(255))
            
            let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
            colors.append(color)
        }
        
        pieChartDataSet.colors = colors
        
        
        
        
    }

    func setChart2(dataPoints: [String], values: [Double]) {
        
        var dataEntries: [ChartDataEntry] = []
        
        for i in 0..<dataPoints.count {
            let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
            dataEntries.append(dataEntry)
        }
        
        let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "Units Sold")
        
        let formatter = NSNumberFormatter()
        formatter.numberStyle = .PercentStyle
        formatter.maximumFractionDigits = 1
        formatter.multiplier = 1.0
        
        
        let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
        
        pieChartData.dataSet?.valueFormatter = formatter
        
        
        pieChartView.data = pieChartData
        
        pieChartView.holeColor = UIColor.clearColor()
        
        pieChartView.holeRadiusPercent = 0.95
        
        pieChartView.centerText =  "Hello\nThis is Pie chart"
        
        pieChartView.usePercentValuesEnabled = true
        
        
        var colors: [UIColor] = []
        
        for i in 0..<dataPoints.count {
            let red = Double(arc4random_uniform(255))
            let green = Double(arc4random_uniform(255))
            let blue = Double(arc4random_uniform(255))
            
            let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
            colors.append(color)
        }
        
        pieChartDataSet.colors = colors
        
        
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

   
}