ios 图表中未显示的正确数据
Correct data not showing in ios Charts
我正在使用 Charts 版本 3.0.1。我目前正在通过转换我的 Rails 项目来自学 Swift。我不知道 Objective C 所以请多多包涵;到目前为止我做得很好。
我在 is/may 下面的代码格式不正确,因为我缺少 PieChart
中的 days
数组:
数字似乎不正确。代码:
@IBOutlet weak var chartView: PieChartView!
override func viewDidLoad() {
super.viewDidLoad()
let days = ["Mo", "Tu", "Wed", "Thur", "Fri", "Sat", "Sun"]
let gross = [11.00, 90.95, 250.00, 40.90, 60.88, 99.99, 25.00]
setChart(dataPoints: days, values: gross)
}
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: "Gross Income")
pieChartDataSet.sliceSpace = 2.0
let pieChartData = PieChartData(dataSets: [pieChartDataSet]) // somewhere here confuses me.
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
chartView.data = pieChartData
}
我是不是漏掉了什么?非常感谢您的第二只眼睛。
您更改了 x 和 y 值,因此索引将作为饼图的值。另外,请使用 PieChartDataEntry。
(我盒子上的工作代码)
override func viewDidLoad() {
super.viewDidLoad()
let days = ["Mo", "Tu", "Wed", "Thur", "Fri", "Sat", "Sun"]
let gross = [11.00, 90.95, 250.00, 40.90, 60.88, 99.99, 25.00]
setChart(days: days, gross: gross)
}
func setChart(days: [String], gross: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<days.count {
let dataEntry = PieChartDataEntry(value : gross[i], label : days[i])
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Gross Income")
pieChartDataSet.sliceSpace = 2.0
let pieChartData = PieChartData(dataSets: [pieChartDataSet])
var colors: [UIColor] = []
for _ in 0..<days.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
pieChartView.data = pieChartData
}
试试这个然后告诉我。
我正在使用 Charts 版本 3.0.1。我目前正在通过转换我的 Rails 项目来自学 Swift。我不知道 Objective C 所以请多多包涵;到目前为止我做得很好。
我在 is/may 下面的代码格式不正确,因为我缺少 PieChart
中的 days
数组:
数字似乎不正确。代码:
@IBOutlet weak var chartView: PieChartView!
override func viewDidLoad() {
super.viewDidLoad()
let days = ["Mo", "Tu", "Wed", "Thur", "Fri", "Sat", "Sun"]
let gross = [11.00, 90.95, 250.00, 40.90, 60.88, 99.99, 25.00]
setChart(dataPoints: days, values: gross)
}
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: "Gross Income")
pieChartDataSet.sliceSpace = 2.0
let pieChartData = PieChartData(dataSets: [pieChartDataSet]) // somewhere here confuses me.
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
chartView.data = pieChartData
}
我是不是漏掉了什么?非常感谢您的第二只眼睛。
您更改了 x 和 y 值,因此索引将作为饼图的值。另外,请使用 PieChartDataEntry。 (我盒子上的工作代码)
override func viewDidLoad() {
super.viewDidLoad()
let days = ["Mo", "Tu", "Wed", "Thur", "Fri", "Sat", "Sun"]
let gross = [11.00, 90.95, 250.00, 40.90, 60.88, 99.99, 25.00]
setChart(days: days, gross: gross)
}
func setChart(days: [String], gross: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<days.count {
let dataEntry = PieChartDataEntry(value : gross[i], label : days[i])
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Gross Income")
pieChartDataSet.sliceSpace = 2.0
let pieChartData = PieChartData(dataSets: [pieChartDataSet])
var colors: [UIColor] = []
for _ in 0..<days.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
pieChartView.data = pieChartData
}
试试这个然后告诉我。