“如何使用iOS Charts API在Swift中创建漂亮的图表”时遇到的困难 [英] difficulties following “ How to Use iOS Charts API to Create Beautiful Charts in Swift”
问题描述
2015年6月23日发布的AppCoda教程将指导您完成一个入门项目,该项目不会将其视图吸引到一个入门项目中。但是,我的项目错误地绘制了示例条形图;在本教程中,所有条形都将出现,并正确标记。我将项目的版本放置在
the AppCoda tutorial, dated 2015-06-23, guides you through finishing a starter project that doesnt draw its views to one that does. however, my project draws the example bar graph incorrectly; in the tutorial all the bars appear, and are labeled correctly. i placed my version of the project here IOChartsDemo.
this is what happens in my project
development environment:
- macOS 12.5.5, Xcode 8.3.3 (8E3004b)
- Charts 3.0.2
i downloaded the starter project indicated in the tutorial.
the starter project in the tutorial didnt build, there were api problems. after following the instructions in the tutorial, and correcting the errors, i end up with this (problem lines are commented out):
class BarChartViewController: UIViewController {
@IBOutlet weak var barChartView: BarChartView!
var months: [String]!
override func viewDidLoad() {
super.viewDidLoad()
// barChartView.noDataTextDescription = "Just because" type BarChartView doest have noDataTextDescription
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
// setChart(months, values: unitsSold) missing argument label dataPoints: in call
setChart(dataPoints: months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
// let dataEntry = BarChartDataEntry(value: values[i], xIndex: i) // argument labeld dont match
let dataEntry = BarChartDataEntry(x: values[i], y: Double(i), data: dataPoints[i] as AnyObject)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
// let chartData = BarChartData(xVals: months, dataSet: chartDataSet) cannot invoke initializer BarCharData with arg list of (xVals: [String]!, dataSet: BarCharDataSet
let chartData = BarChartData(dataSets: [chartDataSet])
barChartView.data = chartData
}
}
at this point, when i run the app, i get the result indicated earlier. i fixed those after going through the Charts api. i suppose this problem is caused by the changes from Charts 2.x to 3.x.
if youre a Charts expert, i kindly request your assistance.
I had been working on your question, first of all, you have inverted the values of y
and x
fixing this
this line let dataEntry = BarChartDataEntry(x: values[i], y: Double(i), data: dataPoints[i] as AnyObject)
must be replaced by this one let dataEntry = BarChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)
and then you must use the IndexAxisValueFormatter
class in order to show the legend in the bottom, I mean the months names
adding this lines
barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
barChartView.xAxis.labelCount = months.count
barChartView.xAxis.labelPosition = .bottom
this is the full code
import UIKit
import Charts
class BarChartViewController: UIViewController {
@IBOutlet weak var barChartView: BarChartView!
var months: [String]!
override func viewDidLoad() {
super.viewDidLoad()
// barChartView.noDataTextDescription = "Just because" type BarChartView doest have noDataTextDescription
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
//setChart(months, values: unitsSold) missing argument label dataPoints: in call
setChart(dataPoints: months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
// let dataEntry = BarChartDataEntry(value: values[i], xIndex: i) // argument labeld dont match
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
// let chartData = BarChartData(xVals: months, dataSet: chartDataSet) cannot invoke initializer BarCharData with arg list of (xVals: [String]!, dataSet: BarCharDataSet
let chartData = BarChartData(dataSets: [chartDataSet])
barChartView.data = chartData
//barChartView.xAxis.ent
barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
barChartView.xAxis.labelCount = months.count
barChartView.xAxis.labelPosition = .bottom
}
}
And this is how it looks, Hope this helps you
这篇关于“如何使用iOS Charts API在Swift中创建漂亮的图表”时遇到的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!