gpt4 book ai didi

ios - 如何使用 iOS 图表库在底部轴上创建带有日期/时间的图表?

转载 作者:行者123 更新时间:2023-12-01 15:33:30 26 4
gpt4 key购买 nike

我目前正在使用 iOS 库图表 并且在实现 时遇到了一些问题时间线 x 轴 .

图书馆链接:https://github.com/danielgindi/Charts

I have a List of Objects as the Input for the Chart.



let objectList : [Object] = fillObjectList()

class Object {
var timeCreated : Date
var value : Double
}

I already implemented a solution where I just used the timeCreated.timeIntervalsince1970 as the values for the x Axis. But this does not look so great.



所以如果你们有使用iOS Charts Library的经验,希望你们能想出解决这个问题的办法。非常感谢您的帮助!

最佳答案

你需要创建 IAxisValueFormatter 的实现类,像这样
注意:我没有编译它,所以可能需要一些更正

public class DateValueFormatter: NSObject, IAxisValueFormatter {
private let dateFormatter = DateFormatter()
private let objects:[Object]

init(objects: [Object]) {
self.objects = objects
super.init()
dateFormatter.dateFormat = "dd MMM HH:mm"
}

public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
if value >= 0 && value < objects.count{
let object = objects[Int(value)]
return dateFormatter.string(from: object.timeCreated)
}
return ""
}
}
并使用此代码格式化
xAxis.valueFormatter = DateValueFormatter(objects: objectList)
编辑:
您可以在此 repo 中看到我尝试使用图表库的一些示例

关于ios - 如何使用 iOS 图表库在底部轴上创建带有日期/时间的图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61302858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com