'How to set legend labels MPChart

I am trying to customize legend but not able to do so.My purpose is to give different legend labels.I ma using MPChart library to do so.

  ArrayList<BarEntry> entries = new ArrayList<>();
    entries.add(new BarEntry(4f, 0));
    entries.add(new BarEntry(8f, 1));
    entries.add(new BarEntry(6f, 2));
    entries.add(new BarEntry(12f, 3));
    entries.add(new BarEntry(18f, 4));
    mColors.add(R.color.red);
    mColors.add(R.color.text_color_gray);
    mColors.add(R.color.text_color_blue);
    mColors.add(R.color.green);
    mColors.add(R.color.black);
   BarDataSet dataset = new BarDataSet(entries, null);
    ArrayList<String> labels = new ArrayList<String>();
    labels.add("05");
    labels.add("06");
    labels.add("07");
    labels.add("08");
    labels.add("09");
    BarData data = new BarData(labels, dataset);
    Legend legend = mChart.getLegend();
  legend.setEnabled(true);
    legend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
    legend.setForm(Legend.LegendForm.SQUARE);
    legend.setColors(mColors);
    legend.setLabels(mLabels);
    mChart.setData(data);
    mChart.animateY(2000);

    LimitLine line = new LimitLine(10f);
    YAxis yAxis = mChart.getAxisLeft();
    yAxis.addLimitLine(line);
    yAxis.setDrawAxisLine(true);
    mChart.setDrawValueAboveBar(true);
    mChart.setDrawBarShadow(false);
    mChart.setVisibleXRange(4);
    mChart.moveViewToX(2);
    mChart.setDrawValueAboveBar(false);
    mChart.invalidate();

Please let me know any solution for this.



Solution 1:[1]

you can custom your legend like this

LegendEntry legendEntryA = new LegendEntry();
legendEntryA.label = "a";
legendEntryA.formColor = Color.GREEN;

And add them to legend of the chart

legend.setCustom(Arrays.asList(legendEntryA, legendEntryB, legendEntryC, legendEntryD));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tho nguyen