[英]How to set a custom x axis 0 label in charts ios
I am using Charts by Daniel Gindi我正在使用Daniel Gindi 的图表
How can I set a String 0 label on x Axis?如何在 x 轴上设置 String 0 标签? I want it to show "Now" instead of a Date type.
我希望它显示“现在”而不是日期类型。 I've seen the function that helps to format one particular label only, but I can't get how it works.
我已经看到了有助于格式化一个特定标签的功能,但我不知道它是如何工作的。 Moreover, I haven't seen any examples of it.
此外,我还没有看到任何例子。 So, how does
getFormattedLabel(index: Int)
work?那么,
getFormattedLabel(index: Int)
工作的呢? This is what I need my xAxis to look like:这就是我需要 xAxis 的样子:
Thank you!谢谢!
You can set your xAxis.valueFormatter
to your own custom class and then return now when your x value is 0. Like:您可以将
xAxis.valueFormatter
设置为您自己的自定义类,然后在 x 值为 0 时立即返回。例如:
class ChartValueFormatter: NSObject, IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
if value == 0 {
return "Now"
}
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("dd MMM")
dateFormatter.locale = .current
let date = Date(timeIntervalSince1970: value)
return dateFormatter.string(from: date)
}
}
For this to work you need to sort your values based on date and then set then now value timestamp to zero.为此,您需要根据日期对值进行排序,然后将现在的值时间戳设置为零。
It took a while to figure out, but I found a great tutorial on Medium by Spencer Mandrusiak花了一段时间才弄明白,但我找到了 Spencer Mandrusiak 在 Medium 上的一个很棒的教程
I needed to make the first label of a String type and all the others should still be Date types.我需要制作 String 类型的第一个标签,所有其他标签仍然应该是 Date 类型。 What I did:
我做了什么:
This solution works well for me!这个解决方案对我很有效!
class ChartXAxisFormatter: NSObject, IAxisValueFormatter {
enum CustomLabel: Int {
case firstLabel
var label: String {
switch self {
case .firstLabel: return "Now"
}
}
}
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "hh:mm"
let date = Date(timeIntervalSince1970: value)
let time = dateFormatterPrint.string(from: date)
let intVal = Int(value)
let customLabel = CustomLabel(rawValue: intVal)
return customLabel?.label ?? "\(time)"
}
}
This is a snippet that helps me convert an X axis into timeline and also make the last label on the chart of a String type.这是一个片段,可帮助我将 X 轴转换为时间轴,并在图表上制作 String 类型的最后一个标签。
index == count - 2
was used because the label count on X axis was set to 3 by force: index == count - 2
因为 X 轴上的标签计数被强制设置为 3:
chart.xAxis.setLabelCount(3, force: true)
chart.xAxis.avoidFirstLastClippingEnabled = false
chart.xAxis.forceLabelsEnabled = true
class ChartXAxisFormatter: NSObject, IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
if let index = axis?.entries.firstIndex(of: value), let count = axis?.entries.count , index == count - 2 {
return "Now"
}
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "HH:mm:ss"
let date = Date(timeIntervalSince1970: value)
let time = dateFormatterPrint.string(from: date)
return time
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.