Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Charts remove decimal from yValues

How to remove decimals from y values in iOS Charts? Im using the latest iOS Charts release with Swift3

enter image description here

like image 632
johnny Avatar asked Nov 29 '22 09:11

johnny


1 Answers

Thanks for every one who tried to help, here was the fix, adding the below code

    let formatter = NumberFormatter()
    formatter.numberStyle = .none
    formatter.maximumFractionDigits = 0
    formatter.multiplier = 1.0
    chartData.valueFormatter = DefaultValueFormatter(formatter: formatter)

to the setBarChartData func

func setBarChartData(xValues: [String], yValues: [Double], label: String) {

    var dataEntries: [BarChartDataEntry] = []

    for i in 0..<yValues.count {
        let dataEntry = BarChartDataEntry(x: Double(i), y: yValues[i])
        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(values: dataEntries, label: label)
    let chartData = BarChartData(dataSet: chartDataSet)


    let formatter = NumberFormatter()
    formatter.numberStyle = .none
    formatter.maximumFractionDigits = 0
    formatter.multiplier = 1.0
    chartData.valueFormatter = DefaultValueFormatter(formatter: formatter)

    let chartFormatter = BarChartFormatter(labels: xValues)
    let xAxis = XAxis()
    xAxis.valueFormatter = chartFormatter
    self.xAxis.valueFormatter = xAxis.valueFormatter

    self.data = chartData
}
like image 122
johnny Avatar answered Dec 20 '22 23:12

johnny