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 ?
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);
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With