更新时间:2023-10-08 17:52:40
事实证明您不能为 BarChartDataEntry
的 X 值设置任何值.因此,您应该做的是将索引 0 置于用于保存模型的数组的长度,并在 IAxisValueFormatter
子类中保存时间戳值.因此,当您覆盖 stringForValue
时,从存储的属性中获取时间戳.所以你的子类应该是这样的:
Turns out you can not set any value to the X value of the BarChartDataEntry
. So what you should do is put index 0 to length of the array you are using to hold your model and inside your IAxisValueFormatter
subclass hold the timestamp values. so when you override stringForValue
get the timestamp from the stored property. So your subclass should look like this:
import Foundation
import Charts
public class DateValueFormatter: NSObject, IAxisValueFormatter {
private let dateFormatter = DateFormatter()
var values:[TimeInterval]!
override init() {
super.init()
dateFormatter.dateFormat = "dd/MM/yy"
}
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return dateFormatter.string(from: Date(timeIntervalSince1970: self.values![Int(value)]))
}
}
所以最重要的是你应该维护一个额外的数组来保存时间戳值并将正常的for循环索引
作为BarChartDataEntry
So bottom line is you should maintain one extra array to hold the timestamp value and put normal for loop index
as the X value of the BarChartDataEntry