2016-10-10 60 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) –

相關問題