我需要创建一个条形图以显示一些统计数据,这些数据可以使用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)

任何帮助表示赞赏。

最佳答案

进行自定义格式设置,以将每个值更改为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)

关于ios - 在iOS的条形图中显示整数值标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47894075/

10-10 04:44