我需要创建一个条形图来显示一些统计数据,我可以使用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)
感谢任何帮助。
答案 0 :(得分: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)
答案 1 :(得分: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]];