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
}
終於格式:
但是,如下圖所示它沒有很好地工作:
如圖所示,它添加6個標籤,而不是4個標籤,它也有重複。
但是,我已經閱讀this solution,正如你所看到的,它還有一些問題。
我該如何解決這個問題?
這對我不起作用。請參閱我的[問題](https://stackoverflow.com/questions/46618636/how-to-add-string-labels-xaxis-labels-to-horizontal-bar-in-charts-framework) –