图表lineChartDataset舍入值 [英] Charts lineChartDataset rounded values
问题描述
我正在尝试使用Swift 3.0的iOS Charts库进行一些操作。
我有一个关于lineChartDataset值格式化的问题(显示在每个值点上方)。现在这些值是'双'并显示为2.0和3.0而不是2和3等。是否可以舍入这些值?我尝试使用numberFormatter,但没有任何作用。任何帮助将不胜感激。
I'm trying out a few things with the iOS Charts library with Swift 3.0. I have a question about formatting of the lineChartDataset values (displayed above each value point). Right now these values are 'double' and are displayed as 2.0 and 3.0 instead of just 2 and 3 etc. Is it possible to round these values? I tried with numberFormatter but nothing works. Any help would be appreciated.
推荐答案
ios-charts已升级为协议基础方法,用于呈现各种标签,如xAxis ,yAxis等。因此,您必须实现协议 IValueFormatter
才能获得所需的结果。我已经创建了相同的类,所以直接使用如下所示&你将能够达到预期的效果。
ios-charts have been upgraded to protocol base approach for rendering the various kind of labels like xAxis, yAxis etc. So you have to implement protocol IValueFormatter
in order to achieve the required results. I have created the class for the same, so directly use this as below & you would be able to achieve desired results.
创建一个名为 DigitValueFormatter
&将其分配给 LineChartData
对象为 lineChartData.setValueFormatter(DigitValueFormatter())
Create a class named as DigitValueFormatter
& assign that to LineChartData
object as lineChartData.setValueFormatter(DigitValueFormatter())
import Foundation
import Charts
class DigitValueFormatter : NSObject, IValueFormatter {
func stringForValue(_ value: Double,
entry: ChartDataEntry,
dataSetIndex: Int,
viewPortHandler: ViewPortHandler?) -> String {
let valueWithoutDecimalPart = String(format: "%.0f", value)
return "\(valueWithoutDecimalPart)"
}
}
这篇关于图表lineChartDataset舍入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!