2017-01-07 5 views
0

我有一个iOS图表,根据下图,标签显示不正确(或不是我想要的)。如何设置正确的标签是iOS图表条形图和颜色列以适合?

enter image description here

正如你可以看到他们都表示“玩过游戏”和是相同的颜色,这在我的图中每个条。

如何更改每个栏的颜色并显示正确的相应标签'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 

} 

回答

0

只需添加chartDataSet.colors = ChartColorTemplates.material()也可以为了根据您的需要来定制添加您的UIColor阵列。

相关问题