Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS Charts highlight specific labels on X-axis and dashed gridline only in the middle

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:

enter image description here

What I want to achieve:

enter image description here

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
}
like image 989
Darsshan Avatar asked Oct 28 '25 14:10

Darsshan


1 Answers

This functionality isn't available in the library.

  • https://github.com/danielgindi/Charts/issues/3317
  • https://github.com/danielgindi/Charts/issues/2925

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.

like image 64
staticVoidMan Avatar answered Oct 31 '25 03:10

staticVoidMan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!