如何格式化图表折线图中显示的数据?默认情况下,数据显示为double,但我希望它显示为int。
let pre = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
for i in 0..<pre.count {
let preDataEntry = ChartDataEntry(value: pre[i], xIndex: i)
preEntries.append(preDataEntry)
}
let preChartDataSet = LineChartDataSet(yVals: preEntries, label: "Pre")
lineChartView.data = lineChartData
答案 0 :(得分:7)
<强> 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()
答案 1 :(得分:4)
您可以使用一些格式化程序属性:
valueFormatter
中的 ChartDataSet
ChartYAxis中的
格式化程序:
/// the formatter used to customly format the y-labels
public var valueFormatter: NSNumberFormatter?
/// the formatter used to customly format the y-labels
internal var _defaultValueFormatter = NSNumberFormatter()
应该足够了