2

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?

A.Danibo
  • 494
  • 1
  • 4
  • 16
  • Maybe it'll help https://stackoverflow.com/questions/39772646/mpandroidchart-can-i-set-different-colours-for-the-x-axis-labels – Merov Nov 21 '18 at 15:48
  • I already saw this question before typing mine. In his case, the bars colors are rendered and then the color doesn't change. The XAxisRenderer is totally fine for this, because the colors won't change later. What I need to do is that if I highlight a bar (click on it), the color changes, and if I click on another bar, it returns to the base color and the other clicked element takes the highlighted color. – A.Danibo Nov 21 '18 at 15:59
  • 1
    I see. I have no time to test it myself now for my regret. But also found this, hope it can help: https://github.com/PhilJay/MPAndroidChart/wiki/Highlighting – Merov Nov 21 '18 at 16:19

1 Answers1

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()
        }