iOS图表仅显示一个图表细分

时间:2017-02-21 09:13:09

标签: ios swift charts ios-charts

我尝试使用两个项目从Charts库设置饼图。我的代码如果跟随

lazy var pieChart : PieChartView = {
    let pie = PieChartView()
    pie.data?.setValueTextColor(UIColor.abClear())
    pie.chartDescription?.enabled = false
    pie.legend.enabled = false
    return pie
}()

override func viewDidLoad() {
    super.viewDidLoad()
    let months = ["Jan", "Feb"]
    let unitsSold = [10.0, 10.0]

    setChart(dataPoints: months, values: unitsSold)
}

func setChart(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: "")
    pieChartDataSet.drawValuesEnabled = false
    let pieChartData = PieChartData(dataSet: pieChartDataSet)
    pieChart.data = pieChartData

    pieChartDataSet.colors = [UIColor.red, UIColor.blue]
}

问题是图表只绘制了一个项目 - 蓝色项目。看起来像这样

enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我检查了你的代码。问题是您在x中为yChartDataEntry设置的值。在函数setChart(dataPoints: [String], values: [Double])中更改代码:

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