Chui
Chui

Reputation: 3

Swift : Change the color of a specific X-Axis label and the corresponding data value in a line chart using Charts framework

I am fairly new to Charts framework. I am implementing a line chart using Charts framework and in it I have a requirement to change the color of a specific X Axis label and its corresponding data value. In the chart attached below, the color of X Axis label 2004 and it's coresponding value 3.0 are to be set to different color. Can anyone suggest as to how to achieve this ?Sample Line Graph

Upvotes: 0

Views: 3549

Answers (1)

Aditya Garg
Aditya Garg

Reputation: 858

You would need to use the valueColors property of lineChartDataSet. This takes in an array, so you would create a custom function that returns a UIColor.

See the code below:

var valueColors = [UIColor]()
var dataEntries = [ChartDataEntry]()
for i in 0..<dataPoints.count
{
    let dataEntry = lineChartDataEntry(value: values[i], xIndex: i)
    dataEntries.append(dataEntry)
    valueColors.append(colorPicker(values[i]))

}

let lineChartDataSet = lineChartDataSet(yVals: dataEntries, label: "")
lineChartDataSet.valueColors = valueColors


func colorPicker(value : Double) -> UIColor {

//input your own logic for how you actually want to color the x axis
    if value == 3 {
        return UIColor.red
    }
    else {
        return UIColor.black
    }
}

Upvotes: 3

Related Questions