图表 lineChartDataset 舍入值 [英] Charts lineChartDataset rounded values

查看:26
本文介绍了图表 lineChartDataset 舍入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Swift 3.0 的 iOS Charts 库进行一些操作.我有一个关于 lineChartDataset 值(显示在每个值点上方)的格式的问题.现在这些值是双倍"并显示为 2.0 和 3.0 而不是 2 和 3 等.是否可以四舍五入这些值?我尝试使用 numberFormatter 但没有任何效果.任何帮助将不胜感激.

I'm trying out a few things with the iOS Charts library with Swift 3.0. I have a question about formatting of the lineChartDataset values (displayed above each value point). Right now these values are 'double' and are displayed as 2.0 and 3.0 instead of just 2 and 3 etc. Is it possible to round these values? I tried with numberFormatter but nothing works. Any help would be appreciated.

推荐答案

ios-charts 已升级为协议基础方法,用于渲染 xAxis、yAxis 等各种标签.因此您必须实现协议 IValueFormatter 以达到所需的结果.我已经创建了相同的类,所以直接使用下面的 &您将能够达到预期的结果.

ios-charts have been upgraded to protocol base approach for rendering the various kind of labels like xAxis, yAxis etc. So you have to implement protocol IValueFormatter in order to achieve the required results. I have created the class for the same, so directly use this as below & you would be able to achieve desired results.

创建一个名为 DigitValueFormatter 的类 &将其分配给 LineChartData 对象作为 lineChartData.setValueFormatter(DigitValueFormatter())

Create a class named as DigitValueFormatter & assign that to LineChartData object as lineChartData.setValueFormatter(DigitValueFormatter())

import Foundation
import Charts

class DigitValueFormatter : NSObject, IValueFormatter {

    func stringForValue(_ value: Double,
                        entry: ChartDataEntry,
                        dataSetIndex: Int,
                        viewPortHandler: ViewPortHandler?) -> String {
        let valueWithoutDecimalPart = String(format: "%.0f", value)
        return "(valueWithoutDecimalPart)"
    }
}

这篇关于图表 lineChartDataset 舍入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆