iOS图表从yValues中删除小数

时间:2017-05-25 17:43:55

标签: ios-charts

如何从iOS图表中的y值中删除小数? 我使用最新的iOS图表版本与Swift3

enter image description here

3 个答案:

答案 0 :(得分:6)

感谢每一位试图提供帮助的人,这是修复,添加以下代码

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

到setBarChartData函数

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.setValueFormatter(DefaultValueFormatter(formatter: formatter))

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

    self.data = chartData
}

答案 1 :(得分:4)

还可以使用:

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

答案 2 :(得分:2)

您需要在DataSet中为值格式化程序设置委托,如下所示

Obj-C:

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

使用以下委托方法格式化您的值:

#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];
}

确认协议:

@interface YourViewController ()<ChartViewDelegate,IChartValueFormatter>

对于Swift,您需要创建BarChart的扩展并在其中使用以下方法

斯威夫特:

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
    }
} 

调用以上扩展方法,如下所示:

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")
    }

希望您能在折线图上获得格式化的值。