How to set MPChart legend labels

I am trying to set up a legend but cannot do it. My goal is to give different labels to the labels. I am using MPChart library for this.

  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.

+3


source to share


1 answer


If you want to have 4 legend labels you need 4 BarDataSet objects.

Having different colors will only group different colors in one legend that will be generated.



And you need to pass the colors to the DataSet and it will map to the legend.

Finally, you need a DataSets label to use for the legend. You can specify a label as the second parameter in the constructor.

+3


source







All Articles