gpt4 book ai didi

ios - 格式化图表中的数字 ios swift

转载 作者:搜寻专家 更新时间:2023-11-01 05:47:07 30 4
gpt4 key购买 nike

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

enter image description here

最佳答案

已为 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()

关于ios - 格式化图表中的数字 ios swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34283335/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com