当前位置:  开发笔记 > IOS > 正文

格式化图表中的数字ios swift

如何解决《格式化图表中的数字iosswift》经验,为你挑选了1个好方法。

如何格式化图表折线图中显示的数据?默认情况下,数据显示为double,但我希望它显示为int.

let pre = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
for i in 0..

在此输入图像描述



1> MANISH PATHA..:

固定为SWIFT 3

YAxisValueFormatter.swift

import Foundation
import Charts
class YAxisValueFormatter: NSObject, IAxisValueFormatter {

    let numFormatter: NumberFormatter

    override init() {
        numFormatter = NumberFormatter()
        numFormatter.minimumFractionDigits = 1
        numFormatter.maximumFractionDigits = 1

        // if number is less than 1 add 0 before decimal
        numFormatter.minimumIntegerDigits = 1 // how many digits do want before decimal
        numFormatter.paddingPosition = .beforePrefix
        numFormatter.paddingCharacter = "0"
    }

    /// Called when a value from an axis is formatted before being drawn.
    ///
    /// For performance reasons, avoid excessive calculations and memory allocations inside this method.
    ///
    /// - returns: The customized label that is drawn on the axis.
    /// - parameter value:           the value that is currently being drawn
    /// - parameter axis:            the axis that the value belongs to
    ///

    public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return numFormatter.string(from: NSNumber(floatLiteral: value))!
    }
}

创建YAxisValueFormatter类,并将对象分配给图形leftAxis

    // Number formatting of YAxis
    chartView.leftAxis.valueFormatter = YAxisValueFormatter()

推荐阅读
乐韵答题
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有