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

3

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

非常感谢您的帮助。


你应该包含一个链接到你正在使用的图表库。 - Duncan C
1
BarChartView是什么?它在哪里声明?请只显示与问题相关的代码。backgroundColordelegatelayer.opacity等与问题无关。 - Ashley Mills
https://github.com/danielgindi/Charts 似乎是一个图表库。 - David S.
2个回答

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)
    }
}

然后为您的图表设置格式化程序。
    let formatter: CustomIntFormatter = CustomIntFormatter()
    barChartView.data?.setValueFormatter(formatter)

0
您可以简单地定义格式化程序,并将该格式化程序应用于数据集,如下所示:
     NSNumberFormatter *pFormatter = [[NSNumberFormatter alloc] init];
     pFormatter.numberStyle = NSNumberFormatterDecimalStyle;
     pFormatter.maximumFractionDigits = 100;
     pFormatter.multiplier = @1;
     BarChartDataSet *set = nil;
     set = [[BarChartDataSet alloc] initWithEntries:values label:@"Election Result"];
     [set setValueFormatter:[[ChartDefaultValueFormatter alloc]initWithFormatter:pFormatter]];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接