Make Bar chart colours and Legend match
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.

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"});
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.
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
Ow I see you are using the wrong method. Try legend.setCustom(...) it's in the documentation of the legend.
Hi Again, yes I read about that but it seems I can't call setCustom for some reason :

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.
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 ?
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.
Alright, Thanks ! Any idea how long this might take ?