MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

Make Bar chart colours and Legend match

Open knseir opened this issue 11 years ago • 8 comments

Hello, I am building a bar chart where every bar represent one Lap (time) for swimmers, and the color is the swim style. In the image below, for example, pink is front crawl and green is breaststroke.

screenshot_2015-02-25-10-13-27

I've set the colors using this code: for every lap in an array of laps :

            if(lap._swimStyle == 0) {
                //149-209-42
                colors.add(Color.rgb(149,209,42));
            } else if(lap._swimStyle == 1) {
                colors.add(Color.rgb(252, 174, 48));
            } else if(lap._swimStyle == 2) {
                colors.add(Color.rgb(240,146,229));
            } else if(lap._swimStyle == 3) {
                colors.add(Color.rgb(144,137,245));
            } else if(lap._swimStyle == 4) {
                colors.add(Color.YELLOW);
            }

Then :

            dataSet.setColors(colors);

The problem is there is one square in the legend per bar

I need it to be one square per swim style (so four legend items total)

Code for the legend is :

    Legend legend = barChart.getLegend();
    legend.setEnabled(true);
    legend.setColors(colors);
    legend.setLabels(new String[]{"Style 1", "Style 2", "Style 3", "Style 4"});

knseir avatar Jun 12 '15 20:06 knseir

The problem is your legend.setColors(...). There you only should set the 4 colors that go along with the labels, not your full colors array.

PhilJay avatar Jun 13 '15 09:06 PhilJay

It didn't work. Here is the modification I made :

I declared an array of only 5 colours that will be used in the chart :

    Integer[] swimStyleColorArray =  {Color.rgb(149,209,42), Color.rgb(252, 174, 48), Color.rgb(240,146,229), Color.rgb(144,137,245), Color.YELLOW};

Then create another colour ArrayList with one colour per bar (82 items total);

    ArrayList<Integer> colors = new ArrayList<Integer>();

    ...

    for(IBLap lap : session._laps) {

            ...
            colors.add(swimStyleColorArray[lap._swimStyle]);
            ...
    }

I set the first array (with only 5 items) as legend's colours:

    legend.setColors(Arrays.asList(swimStyleColorArray));

Same problem

knseir avatar Jun 14 '15 09:06 knseir

Ow I see you are using the wrong method. Try legend.setCustom(...) it's in the documentation of the legend.

PhilJay avatar Jun 14 '15 09:06 PhilJay

Hi Again, yes I read about that but it seems I can't call setCustom for some reason :

screen shot 2015-06-14 at 1 12 51 pm

knseir avatar Jun 14 '15 10:06 knseir

Which version are you using? The latest 2.1.0? If so, then this has not been in a release yet, if not, update to the latest version.

PhilJay avatar Jun 14 '15 12:06 PhilJay

I have this in my build.gradle : compile 'com.github.PhilJay:MPAndroidChart:v2.1.0'

So I guess I am using the latest one. Are you suggesting I downgrade ?

knseir avatar Jun 14 '15 12:06 knseir

No, that will not do anything good. You will have to wait until there is a release 2.1.1 in which the methods mentioned before are included.

PhilJay avatar Jun 14 '15 13:06 PhilJay

Alright, Thanks ! Any idea how long this might take ?

knseir avatar Jun 14 '15 13:06 knseir