快速使用Charts库的饼图 [英] Pie chart using Charts library with swift
本文介绍了快速使用Charts库的饼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用图表库将饼图集成到我的应用程序中,并且图表数据出现问题,我的代码是
I am integrating pie chart in my app using Charts library and getting issue with chart data my code is
import UIKit
import Charts
class ViewController: UIViewController {
@IBOutlet weak var pieChartView: PieChartView!
override func viewDidLoad() {
super.viewDidLoad()
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = [10.0, 4.0, 6.0, 3.0, 12.0, 16.0]
setChart(dataPoints: months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry1 = ChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)
dataEntries.append(dataEntry1)
}
print(dataEntries[0].data)
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Units Sold")
let pieChartData = PieChartData(dataSet: pieChartDataSet)
pieChartView.data = pieChartData
var colors: [UIColor] = []
for _ 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
}
}
当运行应用程序时,我得到了一些东西
and when run app I getting something this
但是我需要完整的数据记录中的饼图,如下面的屏幕截图所示
but I need pie chart in full data records like in below screen shot
推荐答案
之所以没有显示所有信息,是因为您在创建入口点时正在使用父级初始化程序.
The reason all the information isn't showing up is because you are using a parent initialiser when creating the entry point.
代替
let dataEntry1 = ChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)
改为尝试
let dataEntry1 = PieChartDataEntry(value: Double(i), label: dataPoints[i], data: dataPoints[i] as AnyObject)
PieChartDataEntry专用于饼图,因此您应该看到图表中显示的月份.
The PieChartDataEntry is specifically for Pie charts so you should see the month show up in the chart.
希望这能使您走上正确的轨道
Hopefully this gets you on the right track
这篇关于快速使用Charts库的饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文