Charts lineChartDataset rounded values

て烟熏妆下的殇ゞ 提交于 2019-12-19 03:37:13

问题


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.


回答1:


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.

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)"
    }
}


来源:https://stackoverflow.com/questions/41644918/charts-linechartdataset-rounded-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!