如何格式化图表折线图中显示的数据?默认情况下,数据显示为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()