2016-10-10 101 views
5

我向故事板添加了bar chart,但我无法为我的数据条目正确设置标签。如何在ios图表中为BarChartView的XAxis添加标签

这里是我的代码:

var names = ["aaa", "bbb", "ccc", "ddd"] 
var values = [230.0, 280.0, 450.0, 340.0] 

setChart(dataPoints: names, values: values) 

setChart功能:

func setChart(dataPoints: [String], values: [Double]) 
{ 
    let formatter = BarChartFormatter() 
    formatter.setValues(values: dataPoints) 
    let xaxis:XAxis = XAxis() 


    barChartView.noDataText = "You need to provide data for the chart." 
    var dataEntries: [BarChartDataEntry] = [] 

    for i in 0..<dataPoints.count 
    { 
     let dataEntry = BarChartDataEntry(x: Double(i), y: values[i]) 
     dataEntries.append(dataEntry) 
    } 

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "موجودی") 

    let chartData = BarChartData(dataSet: chartDataSet) 


    xaxis.valueFormatter = formatter 
    barChartView.xAxis.labelPosition = .bottom 
    barChartView.xAxis.drawGridLinesEnabled = false 
    barChartView.xAxis.valueFormatter = xaxis.valueFormatter 
    barChartView.chartDescription?.enabled = false 
    barChartView.legend.enabled = true 
    barChartView.rightAxis.enabled = false 
    barChartView.data = chartData 
} 

终于格式:

​​

但是,如下图所示它没有很好地工作:

enter image description here

如图所示,它添加6个标签,而不是4个标签,它也有重复。

但是,我已经阅读this solution,正如你所看到的,它还有一些问题。

我该如何解决这个问题?

回答

9

我想你可以尝试设置以下属性:

barChartView.xAxis.granularityEnabled = true 
barChartView.xAxis.granularity = 1.0 //default granularity is 1.0, but it is better to be explicit 
barChartView.xAxis.decimals = 0 

源代码告诉你很多关于上述特性。 https://github.com/danielgindi/Charts/blob/master/Source/Charts/Components/AxisBase.swift

+0

这对我不起作用。请参阅我的[问题](https://stackoverflow.com/questions/46618636/how-to-add-string-labels-xaxis-labels-to-horizo​​ntal-bar-in-charts-framework) –