iOS Charts remove decimal from yValues

血红的双手。 提交于 2020-03-18 07:07:26

问题


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.



来源:https://stackoverflow.com/questions/44186683/ios-charts-remove-decimal-from-yvalues

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