android – MPAndroidChart, set different color to bar in a bar chart based on y axis values-ThrowExceptions

Exception or error:

I’m plotting a bar chart using MPAndroid chart. Now all my bars have the same color but I want different colors for the bars based on Y axis values, like if value >100. color = red, like in the picture below. Is that possible? someone please help me.

enter image description here

Regards.

How to solve:

You can override the BarDataSet class to achieve this

public class MyBarDataSet extends BarDataSet {


    public MyBarDataSet(List<BarEntry> yVals, String label) {
        super(yVals, label);
    }

    @Override
    public int getColor(int index) {
        if(getEntryForXIndex(index).getVal() < 95) // less than 95 green
            return mColors.get(0);
        else if(getEntryForXIndex(index).getVal() < 100) // less than 100 orange
            return mColors.get(1);
        else // greater or equal than 100 red
            return mColors.get(2);
    }

}

And you need to define your colors like this:

MyBarDataSet set = new MyBarDataSet(yVals, "");
set.setColors(new int[]{ContextCompat.getColor(context, R.color.green), 
                ContextCompat.getColor(context, R.color.orange), 
                ContextCompat.getColor(context, R.color.red)});
ArrayList<BarDataSet> dataSets = new ArrayList<>();
dataSets.add(set);


BarData data = new BarData(xVals, dataSets);

###

You can find out the documentation about setting colors in MPAndroidChart from this link

    LineDataSet setComp1 = new LineDataSet(valsComp1, "Company 1");
  // sets colors for the dataset, resolution of the resource name to a "real" color is done internally
  setComp1.setColors(new int[] { R.color.red1, R.color.red2, R.color.red3, R.color.red4 }, Context);

  LineDataSet setComp2 = new LineDataSet(valsComp2, "Company 2");
  setComp2.setColors(new int[] { R.color.green1, R.color.green2, R.color.green3, R.color.green4 }, Context);

###

I have done this for 3 colors, and it worked

if(floatArray.get(i) >= 0.0 && floatArray.get(i) <= max1)
            {
                barColorArray1[i] = Color.rgb(0, 128, 0);           
            }
            else if(floatArray.get(i) > max1 && floatArray.get(i) <= max2)
            {
                barColorArray1[i] = Color.rgb(247, 207, 19);                
            }
            else if(floatArray.get(i) > max2 )
            {
                barColorArray1[i] = Color.rgb(199, 0, 15);              
            }

In this i have created an array with integer values of colors.
Lastly pass this in your BarDataSet as barDataSet1.setColors(barColorArray1);

###

here is the class for MPAndroidChart:v3.0.2

class MyBarDataSet extends BarDataSet { 

    public MyBarDataSet(List<BarEntry> yVals, String label) {
        super(yVals, label);
    }

    @Override
    public int getColor(int index) {
        if(getEntryForIndex(index).getY() < 140)
            return mColors.get(0);
        else if(getEntryForIndex(index).getY() > 145)
            return mColors.get(1);
        else
            return mColors.get(2);
    }

}

###

Thumbs up to the answer from @m4n3k4s. Just want to add some clarification with regards to these lines, since it took me a while to figure out:

set.setColors(new int[]{ContextCompat.getColor(context, R.color.green), 
            ContextCompat.getColor(context, R.color.orange), 
            ContextCompat.getColor(context, R.color.red)});

I didn’t know what context was or how to use it until I found this thread. I replaced the lines above with:

barDataSet.setColors(
                ContextCompat.getColor(barchart.getContext(), R.color.green),
                ContextCompat.getColor(barchart.getContext(), R.color.yellow),
                ContextCompat.getColor(barchart.getContext(), R.color.red)
        );

Given that BarChart is a subclass of View, and getContext() is a method of the View class.

So my complete code looks like this, where KpBarDataSet is my class that overrides BarDataSet, and DateKp is a custom class.

BarChart barchart = (BarChart) findViewById(R.id.barchart);

    List<BarEntry> entries = new ArrayList<BarEntry>();
    List<String> dateLabels = new ArrayList<>();

    int i = 0;
    for (DateKp day : data) {
        // turn your data into Entry objects
        entries.add(new BarEntry(i, day.getValueY()));
        dateLabels.add(day.getLabel());
        i++;
    }

    KpBarDataSet barDataSet = new KpBarDataSet(entries, null);

    barDataSet.setColors(
            ContextCompat.getColor(barchart.getContext(), R.color.green),
            ContextCompat.getColor(barchart.getContext(), R.color.yellow),
            ContextCompat.getColor(barchart.getContext(), R.color.red)
    );

Lastly, R.color.green, R.color.red etc won’t exist until you define them in /res/values/colors.xml.

Hope this helps.

###

Update

form m4n3k4 answer

public class MyBarDataSet extends BarDataSet {


    public MyBarDataSet(List<BarEntry> yVals, String label) {
        super(yVals, label);
    }

     @Override
    public int getColor(int index) {
        if(getEntryForIndex(index).getY() ==40) // less than 95 green
            return mColors.get(0);
        else if(getEntryForIndex(index).getY() ==30) // less than 100 orange
            return mColors.get(1);
        else // greater or equal than 100 red
            return mColors.get(2);
    }
}

and in JavaCode

 set1.setColors(ContextCompat.getColor(StepCountsActivity.this, R.color.purple),
                ContextCompat.getColor(StepCountsActivity.this, R.color.light_purple),
                ContextCompat.getColor(StepCountsActivity.this, R.color.blue));

    }

Leave a Reply

Your email address will not be published. Required fields are marked *