问题
As title,After I check How to add Strings on X Axis in iOS-charts? this post. I still don't know where can I put "formato.stringForValue(Double(i), axis: taxis)" in for-in loop, as I know, when I implement this protocol ,this function will send out a "String".My question is how does it connect to my xAxis data??
Following is my code ,please refer to it.
//setChartData
func setChart(dataPoints:[String],values:[Double]){
self.barChartView.noDataText = "You need to provide data for the chart."
let xaxis = XAxis()
let fomatter = BarChartFomatter()
fomatter.setValues(values: dataPoints)
var dataEntries:[BarChartDataEntry] = []
for i in 0..<dataPoints.count{
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
*fomatter.stringForValue(Double(i), axis: xaxis)*
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let data = BarChartData(dataSet: chartDataSet)
xaxis.valueFormatter = fomatter
self.barChartView.backgroundColor = UIColor.clear
self.barChartView.xAxis.drawLabelsEnabled = false
self.barChartView.chartDescription?.enabled = false
self.barChartView.legend.enabled = false
self.barChartView.rightAxis.enabled = false
self.barChartView.xAxis.valueFormatter = xaxis.valueFormatter
self.barChartView.xAxis.labelPosition = .bottom
//self.barChartView.description = "Test String"
barChartView.xAxis.granularityEnabled = true
barChartView.xAxis.granularity = 1.0 //default granularity is 1.0, but it is better to be explicit
barChartView.xAxis.decimals = 0
self.barChartView.data = data
}
And my BarChartFomatter as follow:
import UIKit
@objc(BarChartFomatter)
open class BarChartFomatter: NSObject,IAxisValueFormatter {
var names = [String]()
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return names[Int(value)]
}
public func setValues(values:[String]){
self.names = values
}
}
Result: There is no text under the chart
Please help me , thanks
来源:https://stackoverflow.com/questions/40376720/how-to-use-xaxis-with-label-in-charts-frameworks