iOS Charts remove decimal from yValues

2020-07-18 09:45发布

问题:

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

回答1:

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
}


回答2:

Could also use:

let format = NumberFormatter()
format.numberStyle = .none
let formatter = DefaultValueFormatter(formatter: format)
data.setValueFormatter(formatter)


回答3:

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.



标签: ios-charts