Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPAndroidChart PieChart how to set label text?

got the following code:

    Legend legend = mChart.getLegend();
    legend.setLabels(new String[]{"aaaaa", "bbbbb", "ccccc"});

This setting does not take effect Is there an other way to set the text ?

like image 810
Fomovet Avatar asked Nov 17 '16 01:11

Fomovet


3 Answers

I could not find the method setCustom(int[] color, String[] labels) in v3.0.0. Only setCustom(LegendEntry[]) for which you have to pass LegendEntry objects.

 List<LegendEntry> entries = new ArrayList<>();

 for (int i = 0; i < titleList.size(); i++) {
     LegendEntry entry = new LegendEntry();
     entry.formColor = colorList.get(i);
     entry.label = titleList.get(i);
     entries.add(entry);
 }

 legend.setCustom(entries);
like image 169
donny.rewq Avatar answered Nov 12 '22 02:11

donny.rewq


You can set custom labels with colors:

First make sure Legend is enable. Unless enable legend.

legend.setEnabled(true);

With com.github.PhilJay:MPAndroidChart:v3.0.0:-

legend .setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "aaaaa", "bbbbb", "ccccc"});

setCustom(int[] colors, String[] labels): Sets a custom legend's labels and colors arrays. The colors count should match the labels count. Each color is for the form drawn at the same index.

like image 6
Chandana Kumara Avatar answered Nov 12 '22 01:11

Chandana Kumara


Pass the label name as second parameter to constructor PieEntry(). (For version > 3.0.0)

Example:

ArrayList<PieEntry> yvalues = new ArrayList<PieEntry>();
yvalues.add(new PieEntry(8f, "JAN"));
yvalues.add(new PieEntry(15f, "FEB"));
yvalues.add(new PieEntry(12f, "MAR"));
yvalues.add(new PieEntry(25f, "APR"));
yvalues.add(new PieEntry(23f, "MAY"));
yvalues.add(new PieEntry(17f, "JUNE"));
PieDataSet dataSet = new PieDataSet(yvalues, "Election Results");
PieData data = new PieData();
data.addDataSet(dataSet);
data.setValueFormatter(new PercentFormatter());
pieChart.setData(data);
like image 5
siva Avatar answered Nov 12 '22 01:11

siva