我正在设置一个在xAxis上有一些价值的CombinedChartView,比方说(15,16,17,2,3)
使用这行代码
for i in 0..<gameStatsMonth.count {
let dataEntryTeam = ChartDataEntry(x: Double(gameStatsMonth[i].day!), y: Double(gameStatsMonth[i].points!))
lineDataEntryTeam.append(dataEntryTeam)
}
恰好以xAxis显示的值显示,如下所示:2,3,15,16,17。
有什么方法可以保持x值的第一个顺序? (它们在lineDataEntryTeam中插入的顺序)
答案 0 :(得分:1)
为xAxis添加数组(在函数之外):
var xaxisVals = [Double]()
更新for
循环
for i in 0..<gameStatsMonth.count {
let dataEntryTeam = ChartDataEntry(x: Double(gameStatsMonth[i].day!), y: Double(gameStatsMonth[i].points!))
xaxisVals.append(Double(gameStatsMonth[i].day!))
lineDataEntryTeam.append(dataEntryTeam)
}
采用IAxisValueFormatter
:
class YourViewController: UIViewController, IAxisValueFormatter
填写委托方法:
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return xaxisVals[Int(value)]
}
在课堂上,定义委托:
weak var axisFormatDelegate: IAxisValueFormatter?
在viewDidLoad
中将代理设置为self
:
axisFormatDelegate = self
在for循环之后,设置xAxis:
let xaxis = lineDataEntryTeam.xAxis
xaxis.granularityEnabled = true
xaxis.granularity = 1
xaxis.valueFormatter = axisFormatDelegate