我使用@danielgindi 的图表库为我的 iOS 应用程序生成图表。一切似乎都很好,但我们面临着 X 轴数据重复的问题。
图表的输入数据(X 轴值,Y 轴值)
("29/03/2017 00:00:00","2.7")
("29/03/2017 00:00:00","2.4")
("29/03/2017 00:10:00","1.3")
("29/03/2017 00:10:00","1.5")
("29/03/2017 00:20:00","1.8")
....
....
....
("29/03/2017 01:00:00","1.2")
("29/03/2017 09:00:00","2.7")
("29/03/2017 09:10:00","-10.8")
....
....
....
("29/03/2017 13:10:00","3.9")
("29/03/2017 13:20:00","-.8")
("29/03/2017 13:20:00","5.9"
其中 X 轴值是日期时间,Y 轴值是与该日期时间关联的 Double 值。
我使用了以下代码来格式化 X 轴
class ChartStringFormatter: NSObject, IAxisValueFormatter {
public func stringForValue(_ value: TimeInterval, axis: AxisBase?) -> String {
let date = Date(timeIntervalSince1970: value)
return date.toString(format: "hh:mm a")
}
}
但是当我缩放图表时,它有多个 X 轴值。我该如何解决这个问题?
X 轴值的预期输出:
最初 - [12:00 AM、03:00 AM、06:00 AM、09:00 AM、12:00 PM、03:00 PM、06:00 PM、09:00 PM、12:00 PM ]
缩放时 - [12:00 AM, 01:00 AM, 02:00 AM, 03:00 ...... 12:00 PM]
再次缩放 - [12:00 AM、12:30 AM、01:00 AM、01:30 AM、02:00 AM、02:30 AM、03:00 AM ...... 12:00 PM ]
再次放大 - [12:00 AM, 12:10 AM, 12:20 AM, 12:30 AM ...... 12:00 PM]
iOS 图表库链接:https://github.com/danielgindi/Charts
最佳答案
如 iOS-Charts 库文档中所述:
When true, axis labels are controlled by the
granularity
property.When false, axis values could possibly be repeated.
...
@property (nonatomic) BOOL granularityEnabled;
和:
The minimum interval between axis values.
This can be used to avoid label duplicating when zooming in.
default: 1.0
@property (nonatomic) double granularity;
所以我很确定这些行会为您完成工作:
<strong>[yourChart.xAxis setGranularityEnabled:YES];</strong>
<strong>[yourChart.xAxis setGranularity:1.0];</strong>
关于swift - iOS 图表库 - 如何处理 X 轴重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43096731/