How to get Pie Chart data to display using AppCoda example and iOS Charts?

1.1k Views Asked by At

I have been following an example from AppCode on creating a pie chart.

I have used the code as they say but I am not getting any data and is returning a warning 'Immutable value i was never used' for the 2nd for section in the snippet below in relation to the colours.

func setGSGCPieChart (dataPoints: [String], values: [Double]){

    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = ChartDataEntry(x: values[i], y: Double(i))
        dataEntries.append(dataEntry)
    }

    let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Units Sold")
    let pieChartData = PieChartData()
    GSGCPieView.data = pieChartData

    var colors: [UIColor] = []

    for i in 0..<dataPoints.count {
        let red = Double(arc4random_uniform(256))
        let green = Double(arc4random_uniform(256))
        let blue = Double(arc4random_uniform(256))

        let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
        colors.append(color)
    }

    pieChartDataSet.colors = colors

}

This is my section of code which should load the values:

    let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
    let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
    setGSGCPieChart (dataPoints: months, values: unitsSold)

What I get is on the left, what I should get is on the right:

enter image description here

1

There are 1 best solutions below

0
On
let pieChartData = PieChartData(dataSet: pieChartDataSet)