iOS 图表只显示一个图表段

iOS Charts show only one chart segment

我尝试从 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]
}

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

我做错了什么?

我检查了你的代码。问题是您在 ChartDataEntry 中为 xy 设置的值。更改函数 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)
}