6

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

enter image description here

Joshua
  • 3,055
  • 3
  • 22
  • 37
johnny
  • 555
  • 4
  • 16

4 Answers4

13

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
}
Paul.V
  • 386
  • 1
  • 3
  • 13
johnny
  • 555
  • 4
  • 16
7

Could also use:

let format = NumberFormatter()
format.numberStyle = .none
let formatter = DefaultValueFormatter(formatter: format)
data.setValueFormatter(formatter)
AMAN77
  • 6,218
  • 9
  • 45
  • 60
2

You need to set delegate for value formatter in DataSet like below

Obj-C :

//DataSet 1
LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithValues:values label:@"outstanding"];
set1.valueFormatter = self;

Use below delegate method for formatting your value :

#pragma mark - IChartValueFormatter

- (NSString * _Nonnull)stringForValue:(double)value entry:(ChartDataEntry * _Nonnull)entry dataSetIndex:(NSInteger)dataSetIndex viewPortHandler:(ChartViewPortHandler * _Nullable)viewPortHandler{

        //Format your value what you want here 
        return [NSString stringWithFormat:@"%0.f",value];
}

Confirm Protocol :

@interface YourViewController ()<ChartViewDelegate,IChartValueFormatter>

For Swift you need to create Extension of BarChart and use below methods in it

Swift :

extension BarChartView {

    private class BarChartFormatter: NSObject, IAxisValueFormatter {

        var labels: [String] = []

        func stringForValue(_ value: Double, axis: AxisBase?) -> String {
            return labels[Int(value)]
        }

        init(labels: [String]) {
            super.init()
            self.labels = labels
        }
    }

    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 chartFormatter = BarChartFormatter(labels: xValues)
        let xAxis = XAxis()
        xAxis.valueFormatter = chartFormatter
        self.xAxis.valueFormatter = xAxis.valueFormatter

        self.data = chartData
    }
} 

Call Above Extension Method like this :

func setChart(){
        let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
        let unitsSold = [20.0, 4.0, 3.0, 6.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]

        barChartView.setBarChartData(xValues: months, yValues: unitsSold, label: "Monthly Sales")
    }

hope you will get your formatted value on line chart.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80
  • Thanks, the code make sense, I'm using swift and im using extensions for the data set, so I'm trying to make it work with swift! will keep you updated. – johnny Jun 02 '17 at 22:05
  • Thats great I have also updated my code with both ObjC and Swift for other user ref. – CodeChanger Jun 06 '17 at 12:49
  • Thanks! This is exactly what im doing, but im getting the decimals for the yValues – johnny Jun 06 '17 at 14:35
0

After lots of research and test. i was able to remove the decimal.

  1. First unlock the pod chartViewBase

  2. Remove two lines from the { } i.e 'digits' from chartViewBase pod.

Before:

if let formatter = defaultValueFormatter as? DefaultValueFormatter
{
  // setup the formatter with a new number of digits
  let digits = reference.decimalPlaces
  formatter.decimals = digits
}

After:

if let formatter = defaultValueFormatter as? DefaultValueFormatter
{
  
}

It will look like following:: Chart without decimal

Tyler2P
  • 2,324
  • 26
  • 22
  • 31