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

问题描述 投票:1回答:1

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

    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)

任何帮助表示赞赏。

ios swift charts bar-chart ios-charts
1个回答
2
投票

创建自定义格式化程序以将每个值更改为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)
    }
}

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

    let formatter: CustomIntFormatter = CustomIntFormatter()
    barChartView.data?.setValueFormatter(formatter)
© www.soinside.com 2019 - 2024. All rights reserved.