如何使用AppCoda示例和iOS图表显示饼图数据?

时间:2017-01-05 14:38:19

标签: ios swift charts highcharts ios-charts

我一直在关注创建饼图的AppCode示例。

我已经按照他们的说法使用了代码,但是我没有收到任何数据,并且在下面的代码段中与颜色相关的第二个for部分返回了一个警告“我永远不会使用不可变值”。

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

}

这是我应该加载值的代码部分:

    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)

我得到的是左侧,我应该得到的是右侧:

enter image description here

1 个答案:

答案 0 :(得分:0)

let pieChartData = PieChartData(dataSet: pieChartDataSet)