'MPAndroidChart Barchart: Set the color of the label associated to the highlighted bar

I am currently using MPAndroidChart on a project. I am using a BarChart with a custom BarChartRenderer and XAxisRenderer.

The problem I'm facing is that I would like the label associated to the highlighted (clicked) bar to be colored the same color as the bar.

Is there a way to do that by overriding lib functions/classes?

Here is a screenshot of the actual state of my chart (the bar on Oct. 16th is highlighted).

enter image description here

And here is what I would like to have as a final result

enter image description here

Has anyone tried to do this kind of thing on MPAndroidChart?



Solution 1:[1]

Try this solution it work for me:

The solution is to use custom ColoredLabelXAxisRenderer class extend XAxisRenderer and when override fun drawLabels you can use 2 variables isUpdated: Boolean and selectedIndex: Int, when you click in the barChart you need to change the values (isUpdated,selectedIndex) and use this condition to change the color of label

 if (isUpdated && selectedIndex != -1) {
                mAxisLabelPaint.color =
                    getUpdateColorForXValue(selectedIndex, mXAxis.mEntries[j / 2].toInt())
            }

and call the builder when click in the barChart and change the values like this

  barChart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
    override fun onValueSelected(e: Entry?, h: Highlight?) {

        var lastValue: ArrayList<String> = arrayListOf()
        var beforeLastValue: ArrayList<String> = arrayListOf()

        if (e?.x != null && !xValues.isNullOrEmpty() && !yValues.isNullOrEmpty()) {
            buildBarChartView(
                getCurrency(currency),
                xValues,
                yValues,
                isUpdate = true,
                selectedIndex = e.x.toInt()
            )
            updateBarView()
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oussema Helal