Charts linesChartDataset rounded values

I am trying a few things with the iOS Charts library with Swift 3.0. I have a question about formatting lineChartDataset values ​​(displayed above each value point). Now these values ​​are "double" and are displayed as 2.0 and 3.0 instead of 2 and 3, etc. Is it possible to round these values? I tried with numberFormatter but nothing works. Any help would be appreciated.

+4
source share
1 answer

ios diagrams have been updated to a basic protocol approach to display various types of labels like xAxis, yAxis, etc. Thus, to achieve the desired results, you need to implement the protocol IValueFormatter. I created a class for the same, so I use it directly, as shown below, and you can achieve the desired results.

Create a class with a name DigitValueFormatterand assign it to an object LineChartDataaslineChartData.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)"
    }
}
+5
source

Source: https://habr.com/ru/post/1666758/


All Articles