MPAndroidChart bar chart how to change color of each label

user2273146 picture user2273146 · Aug 10, 2016 · Viewed 15.7k times · Source

MPAndroidChart This is a bar chart that i build using MPAndroidChart library. Now I have to change the color of each label and I can't find the solution through searching over the internet. Can any one please help me to get through this problem.

Answer

user6542934 picture user6542934 · Aug 10, 2016

There are two parts to this answer.

1) If you want to have a singular label in your legend for your barchart, you would add all of your bars into one dataset and use the method setColors(int[] colors, android.content.Context c) to assign a color to each bar.

2) If you want to have different labels in your legend for each bar, you would need to include multiple datasets into your chart and assign a color to each dataset (number of labels = number of datasets).

I have included example code below for you to reference. The initial block of code is representative of the first option and the second block of code you can substitute in between the comments titled "replace" to get the second option.

public class SO extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.androidchart_mp);
    BarChart chart = (BarChart) findViewById(R.id.chart_bar_mp);

    // replace
    ArrayList<BarEntry> entries = new ArrayList<>();
    entries.add(new BarEntry (1, 5));
    entries.add(new BarEntry (3, 7));
    entries.add(new BarEntry (5,3));
    entries.add(new BarEntry (7,4));
    entries.add(new BarEntry (9,1));
    BarDataSet dataset = new BarDataSet(entries, "First");
    dataset.setColors(new int[] {Color.RED, Color.GREEN, Color.GRAY, Color.BLACK, Color.BLUE});
    BarData data = new BarData(dataset);
    chart.setData(data);
    // replace



    // below is simply styling decisions on code that I have)
    YAxis left = chart.getAxisLeft();
    left.setAxisMaxValue(10);//dataset.getYMax()+2);
    left.setAxisMinValue(0);
    chart.getAxisRight().setEnabled(false);
    XAxis bottomAxis = chart.getXAxis();
    bottomAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    bottomAxis.setAxisMinValue(0);

    bottomAxis.setLabelCount(10);
    bottomAxis.setAxisMaxValue(10);
    bottomAxis.setDrawGridLines(false);
    chart.setDrawValueAboveBar(false);
    chart.setDescription("");
    // legend
    Legend legend = chart.getLegend();
    legend.setYOffset(40);
    legend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
    legend.setTextSize(200);
}

Second Option:

    ArrayList<BarEntry> entries = new ArrayList<>();
    entries.add(new BarEntry (1, 5));
    ArrayList<BarEntry> entries2 = new ArrayList<>();
    entries2.add(new BarEntry (3, 2));
    ArrayList<BarEntry> entries3 = new ArrayList<>();
    entries3.add(new BarEntry (5, 7));
    ArrayList<BarEntry> entries4 = new ArrayList<>();
    entries4.add(new BarEntry (7, 7));
    ArrayList<BarEntry> entries5 = new ArrayList<>();
    entries5.add(new BarEntry (9, 1));
    List<IBarDataSet> bars = new ArrayList<IBarDataSet>();
    BarDataSet dataset = new BarDataSet(entries, "First");
    dataset.setColor(Color.RED);
    bars.add(dataset);
    BarDataSet dataset2 = new BarDataSet(entries2, "Second");
    dataset2.setColor(Color.BLUE);
    bars.add(dataset2);
    BarDataSet dataset3 = new BarDataSet(entries3, "Third");
    dataset3.setColor(Color.GREEN);
    bars.add(dataset3);
    BarDataSet dataset4 = new BarDataSet(entries4, "Fourth");
    dataset4.setColor(Color.GRAY);
    bars.add(dataset4);
    BarDataSet dataset5 = new BarDataSet(entries5, "Fifth");
    dataset5.setColor(Color.BLACK);
    bars.add(dataset5);
    BarData data = new BarData(bars);
    chart.setData(data);

I hope this helps, if you have any other questions, please let me know!