I am trying to have different colours for the labels on the X-axis based on the current date of the month, but I can't seem to individually configure them. I am also trying to have a gridline that has dashes for the midline and the ceiling line would be without dashes. This is on the left axis. But I can't seem to set it individually as well.
Currently how it is:

What I want to achieve:

My code so far:
func setBarChart(dataPoints: [String], values: [String]){
barChartView.noDataTextColor = UIColor.white
barChartView.noDataText = "No data for the chart"
barChartView.backgroundColor = UIColor.white
for i in 0..<dataPoints.count {
let dataPoint = BarChartDataEntry(x: Double(i), y: Double(values[i])!)
dataEntry.append(dataPoint)
}
let chartDataSet = BarChartDataSet(values: dataEntry, label: "BPM")
let chartData = BarChartData()
chartData.addDataSet(chartDataSet)
chartData.setDrawValues(false)
chartData.barWidth = Double(0.5)
chartDataSet.colors = [UIColor(red: 0, green: 158/255, blue: 237/255, alpha: 1)]
let formatter: ChartFormatter = ChartFormatter ()
formatter.setValues(values: dataPoints)
let floatValue: [CGFloat] = [4,4]
barChartView.xAxis.valueFormatter = formatter
barChartView.xAxis.labelTextColor = UIColor(red: 141/255, green: 141/255, blue: 141/255, alpha: 1)
barChartView.getAxis(.right).labelTextColor = UIColor(red: 141/255, green: 141/255, blue: 141/255, alpha: 1)
barChartView.xAxis.axisLineColor = UIColor.clear
barChartView.getAxis(.right).axisLineColor = UIColor.clear
barChartView.getAxis(.left).axisLineColor = UIColor.clear
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.drawLabelsEnabled = true
barChartView.chartDescription?.enabled = false
barChartView.legend.enabled = false
barChartView.rightAxis.enabled = true
barChartView.rightAxis.labelCount = Int(2)
barChartView.rightAxis.drawGridLinesEnabled = false
barChartView.leftAxis.drawGridLinesEnabled = true
barChartView.leftAxis.gridColor = UIColor(red: 235/255, green: 235/255, blue: 235/255, alpha: 1)
barChartView.leftAxis.gridLineWidth = CGFloat(1.5)
barChartView.leftAxis.gridLineDashLengths = floatValue
barChartView.leftAxis.drawLabelsEnabled = false
barChartView.data = chartData
}
This functionality isn't available in the library.
However, that should not stop you from implementing it yourself and maybe even adding it to the library.
As a starting point, look at XAxisRenderer that has drawLabels(context:pos:anchor:).
Here, it basically loops through xAxis.entries and draws each label by calling drawLabel(context:formattedLabel:x:y:attributes:constrainedToSize:anchor:angleRadians:), passing it a labelAttrs that has the information regarding the color to apply.
But currently... labelAttrs is set before the loop with one color referenced from xAxis.labelTextColor.
The key over here is setting labelAttrs[NSAttributedString.Key.foregroundColor] to a color relative to the entry, say, near about this line:
let label = xAxis.valueFormatter?.stringForValue(xAxis.entries[i], axis: xAxis) ?? ""
Which probably would be best if the color were to be provided by xAxis.valueFormatter as well, and that in turn requires changes to IAxisValueFormatter in order to provide for it.
I hope this helps you in some way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With