iOS图表ValueFormatter [英] iOS Charts ValueFormatter
本文介绍了iOS图表ValueFormatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用iOS图表插件(折线图),希望将图表值(每个点上方的数字)设置为十进制数字.
I am using iOS charts plugin (line chart) and wish to style the chart values (the number above each point) to a decimal number.
该值为双精度,但是默认情况下,图表会对其进行四舍五入并将其显示为整数.
The value is a double, but charts by default is rounding it and displaying it as an integer.
我尝试了以下操作,但不起作用:
I have tried the following but not working:
let valueformatter = NumberFormatter()
valueformatter.numberStyle = .decimal
valueformatter.locale = Locale.current
lineChartDataSet.valueFormatter = valueformatter as? IValueFormatter
我尝试了其他各种属性,但没有一个会更改数据集中数字的格式.
I have tried various other properties but non of them change the format of the number in the dataset.
如何更改显示号码的格式?
How can I change the format of the displayed number?
推荐答案
几乎在这里,只需添加以下类:
Almost there, just need to add the following class:
class ChartValueFormatter: NSObject, IValueFormatter {
fileprivate var numberFormatter: NumberFormatter?
convenience init(numberFormatter: NumberFormatter) {
self.init()
self.numberFormatter = numberFormatter
}
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
guard let numberFormatter = numberFormatter
else {
return ""
}
return numberFormatter.string(for: value)!
}
}
现在将其用作数字格式器:
Now use this as the number formatter:
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.locale = Locale.current
let valuesNumberFormatter = ChartValueFormatter(numberFormatter: numberFormatter)
lineChartDataSet.valueFormatter = valuesNumberFormatter
lineChartDataSet.valueFont = lineChartDataSet.valueFont.withSize(chartFontPointSize)
这篇关于iOS图表ValueFormatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文