IOS 使用 Swift X 轴问题的图表
IOS Charts using Swift X-Axis issues
我最近升级到最新版本的 IOS 图表和 X 代码 (8.3),但我在使用 IOS 图表和我的 x 轴时遇到问题。我的 x 轴支持显示日期,但自从升级后我无法让它工作,我只能得到 0 到 6。我相信我知道为什么,这是因为我似乎无法设置 X-轴值了。
这里是升级前的原代码:
func setChart(_ dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Total Items")
let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
lineChartDataSet.colors = [UIColor(red: 27.0/255.0, green: 114.0/255.0, blue: 216.0/255.0, alpha: 1.0)]
lineChartView.xAxis.labelPosition = .bottom
lineChartView.xAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
lineChartView.legend.enabled = false
lineChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInOutQuad)
lineChartView.chartDescription?.text = ""
lineChartView.rightAxis.enabled = false
lineChartView.leftAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
lineChartDataSet.drawValuesEnabled = false
lineChartDataSet.drawCirclesEnabled = false
lineChartDataSet.lineWidth = 2.0
lineChartView.leftAxis.valueFormatter = NumberFormatter()
lineChartView.leftAxis.valueFormatter.minimumFractionDigits = 0
lineChartView.isUserInteractionEnabled = false
lineChartView.data = lineChartData
}
所以这总是工作正常,因为升级的东西已经改变了,所以我的代码现在是:
func setChart(_ dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Total Items")
let lineChartData = LineChartData(dataSet: lineChartDataSet)
lineChartDataSet.colors = [UIColor(red: 27.0/255.0, green: 114.0/255.0, blue: 216.0/255.0, alpha: 1.0)]
lineChartView.xAxis.labelPosition = .bottom
lineChartView.xAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
lineChartView.legend.enabled = false
lineChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInOutQuad)
lineChartView.chartDescription?.text = ""
lineChartView.rightAxis.enabled = false
lineChartView.leftAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
lineChartDataSet.drawValuesEnabled = false
lineChartDataSet.drawCirclesEnabled = false
lineChartDataSet.lineWidth = 2.0
lineChartView.leftAxis.valueFormatter = NumberFormatter() as? IAxisValueFormatter
//lineChartView.leftAxis.valueFormatter.minimumFractionDigits = 0
lineChartView.isUserInteractionEnabled = false
lineChartView.data = lineChartData
}
所以之前我在 lineChartData 中设置 x 轴值,但现在我不能这样做,所以我认为我的 x 轴数据没有被设置。任何人都可以帮助让我的日期再次显示在我的 x 轴上吗?
谢谢
戴夫
您需要使用 stringForValue 方法实现 IAxisValueFormatter 协议并将其设置为 xAxis.valueFormatter
的委托
我看到了两种方式。
您只需将 IAxisValueFormatter 协议添加到 ViewController 并在您的 ViewController.
中实现 stringForValue
class ViewController: UIViewController, IAxisValueFormatter {
// your code ...
// implement delegate method
func stringForValue(_ dataPointIndex: Double, axis: AxisBase?) -> String {
return someDataPoints[Int(dataPointIndex)]
}
func setChart(dataPoints: [String], values: [Double]){
// your code ...
// Set delegate
lineChartView.xAxis.valueFormatter = self
}
// your code ...
}
或者您可以为 IAxisValueFormatter 协议创建特定委托 class。
private class DataPointFormatter: NSObject, IAxisValueFormatter {
var dataPoints: [String] = []
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return dataPoints[Int(value)]
}
init(dataPoints: [String]) {
super.init()
self.dataPoints = dataPoints
}
}
然后在您的 setChart 函数中设置委托
func setChart(dataPoints: [String], values: [Double]){
// your code ...
lineChartView.xAxis.valueFormatter = DataPointFormatter(dataPoints: dataPoints)
}
我最近升级到最新版本的 IOS 图表和 X 代码 (8.3),但我在使用 IOS 图表和我的 x 轴时遇到问题。我的 x 轴支持显示日期,但自从升级后我无法让它工作,我只能得到 0 到 6。我相信我知道为什么,这是因为我似乎无法设置 X-轴值了。
这里是升级前的原代码:
func setChart(_ dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Total Items")
let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
lineChartDataSet.colors = [UIColor(red: 27.0/255.0, green: 114.0/255.0, blue: 216.0/255.0, alpha: 1.0)]
lineChartView.xAxis.labelPosition = .bottom
lineChartView.xAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
lineChartView.legend.enabled = false
lineChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInOutQuad)
lineChartView.chartDescription?.text = ""
lineChartView.rightAxis.enabled = false
lineChartView.leftAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
lineChartDataSet.drawValuesEnabled = false
lineChartDataSet.drawCirclesEnabled = false
lineChartDataSet.lineWidth = 2.0
lineChartView.leftAxis.valueFormatter = NumberFormatter()
lineChartView.leftAxis.valueFormatter.minimumFractionDigits = 0
lineChartView.isUserInteractionEnabled = false
lineChartView.data = lineChartData
}
所以这总是工作正常,因为升级的东西已经改变了,所以我的代码现在是:
func setChart(_ dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Total Items")
let lineChartData = LineChartData(dataSet: lineChartDataSet)
lineChartDataSet.colors = [UIColor(red: 27.0/255.0, green: 114.0/255.0, blue: 216.0/255.0, alpha: 1.0)]
lineChartView.xAxis.labelPosition = .bottom
lineChartView.xAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
lineChartView.legend.enabled = false
lineChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInOutQuad)
lineChartView.chartDescription?.text = ""
lineChartView.rightAxis.enabled = false
lineChartView.leftAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
lineChartDataSet.drawValuesEnabled = false
lineChartDataSet.drawCirclesEnabled = false
lineChartDataSet.lineWidth = 2.0
lineChartView.leftAxis.valueFormatter = NumberFormatter() as? IAxisValueFormatter
//lineChartView.leftAxis.valueFormatter.minimumFractionDigits = 0
lineChartView.isUserInteractionEnabled = false
lineChartView.data = lineChartData
}
所以之前我在 lineChartData 中设置 x 轴值,但现在我不能这样做,所以我认为我的 x 轴数据没有被设置。任何人都可以帮助让我的日期再次显示在我的 x 轴上吗?
谢谢
戴夫
您需要使用 stringForValue 方法实现 IAxisValueFormatter 协议并将其设置为 xAxis.valueFormatter
的委托我看到了两种方式。
您只需将 IAxisValueFormatter 协议添加到 ViewController 并在您的 ViewController.
中实现 stringForValueclass ViewController: UIViewController, IAxisValueFormatter { // your code ... // implement delegate method func stringForValue(_ dataPointIndex: Double, axis: AxisBase?) -> String { return someDataPoints[Int(dataPointIndex)] } func setChart(dataPoints: [String], values: [Double]){ // your code ... // Set delegate lineChartView.xAxis.valueFormatter = self } // your code ... }
或者您可以为 IAxisValueFormatter 协议创建特定委托 class。
private class DataPointFormatter: NSObject, IAxisValueFormatter { var dataPoints: [String] = [] func stringForValue(_ value: Double, axis: AxisBase?) -> String { return dataPoints[Int(value)] } init(dataPoints: [String]) { super.init() self.dataPoints = dataPoints } }
然后在您的 setChart 函数中设置委托
func setChart(dataPoints: [String], values: [Double]){ // your code ... lineChartView.xAxis.valueFormatter = DataPointFormatter(dataPoints: dataPoints) }