我尝试使用两个项目从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]
}
问题是图表只绘制了一个项目 - 蓝色项目。看起来像这样
我做错了什么?
答案 0 :(得分:1)
我检查了你的代码。问题是您在x
中为y
和ChartDataEntry
设置的值。在函数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)
}