Chris Mikkelsen Chris Mikkelsen - 19 days ago 11
Swift Question

How to create multiple data sets in BarCharts IOS Charts

I created two data sets, chartDataSet and 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


I need to add the datasets in my Bar Chart, but the code I used would only work for single dataSet.

This is the chart that I was using in my project.
https://github.com/danielgindi/Charts

Answer

I have not added 2 dataSets on BarChart.

But i have done this with PieChart.

Hope this will help you.

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.
    }


}

Happy a happy coding.

Comments