如何设置正确的标签是 iOS 图表适合条形图和颜色列?

How to set correct labels is iOS Charts bar chart and colour columns to suit?

我有一个 iOS 图表,如下图所示,标签显示不正确(或者不是我想要的)。

如您所见,它们都显示 'Games Played' 并且颜色相同,就像我图表中的每个条一样。

如何更改每个条形的颜色并显示正确的相应标签,'Won'、'Drawn'、'Lost'?

这是我的代码:

override func viewDidLoad() {
   super.viewDidLoad()

   let results =  ["Won", "Drawn", "Lost"]
   let games = [totalWins, totalDraws, totalLosses]
   setChart(dataPoints: results, values: games)
}

func setChart(dataPoints: [String], values: [Double]){

    let formato:BarChartFormatter = BarChartFormatter()
    let xaxis:XAxis = XAxis()

    barChartView.noDataText = "you need to provide some data for the chart."

    var dataEntries: [BarChartDataEntry] = Array()

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

    xaxis.valueFormatter = formato
    barChartView.xAxis.valueFormatter = xaxis.valueFormatter

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Games Played")
    let chartData = BarChartData(dataSets: [chartDataSet])

    chartData.addDataSet(chartDataSet)
    barChartView.data = chartData
    barChartView.leftAxis.forceLabelsEnabled = true
    barChartView.rightAxis.forceLabelsEnabled = true

    barChartView.xAxis.granularityEnabled = true
    barChartView.xAxis.granularity = 1.0

    barChartView.leftAxis.drawGridLinesEnabled = false
    barChartView.rightAxis.drawGridLinesEnabled = false
    barChartView.xAxis.drawGridLinesEnabled = false

    barChartView.rightAxis.enabled = false
    barChartView.drawGridBackgroundEnabled = false
    self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom

}

只需添加 chartDataSet.colors = ChartColorTemplates.material() 或者您可以添加 UIColor 数组以便根据您的需要进行自定义。