在 iOS 的条形图中显示整数值标签

Sun*_*uja 3 charts bar-chart ios swift ios-charts

我需要创建一个条形图来显示一些统计数据,我可以使用 iOS 图表库来显示这些数据。我面临的唯一问题是,条形上方的值标签打印为双精度值,我需要它们作为整数。我试过搜索它,但没有积极的结果。下面是我的代码:

    barChartView = BarChartView()
    barChartView.frame = screen.bounds
    barChartView.delegate = self
    barChartView.backgroundColor = UIColor.clear
    barChartView.layer.opacity = 0.0
    barChartView.chartDescription?.text = ""
    self.view.addSubview(barChartView)
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏。

Dev*_*B2F 5

制作自定义格式化程序以将每个值更改为 Int。

class CustomIntFormatter: NSObject, IValueFormatter{
    public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
        let correctValue = Int(value)
        print("correctValue: \(correctValue)")
        return String(correctValue)
    }
}
Run Code Online (Sandbox Code Playgroud)

然后为您的图表设置格式化程序。

    let formatter: CustomIntFormatter = CustomIntFormatter()
    barChartView.data?.setValueFormatter(formatter)
Run Code Online (Sandbox Code Playgroud)