问题
I'm displaying a custom legend for a PieChart in MPAndroidChart however, .getColors() and .getLabels() are now deprecated.
I've been using them to get an int array and string array respectively but I can't seem to find a direct alternative. Am I missing something obvious? What should I now use instead? Thanks!
Legend legend = mChart.getLegend();
legend.setEnabled(false);
if (legend.getColors() != null) {
int colorCodes[] = legend.getColors();
String labels[] = legend.getLabels();
ArrayList<LegendItem> legendItems = new ArrayList<>();
for (int i = 0; i < legend.getColors().length-1; i++) {
legendItems.add(new LegendItem(colorCodes[i], labels[i]));
}
showLegend(legendItems);
// entry label styling
mChart.setDrawEntryLabels(false);
}
回答1:
The Legend
class now follows better OOP practices and is composed of an array of LegendEntry
. You can iterate through each and extract the colors or the labels as you wish.
private int [] getColors(Legend legend) {
LegendEntry [] legendEntries = legend.getEntries();
int [] colors = new int[legendEntries.length];
for (int i = 0; i < legendEntries.length; i++) {
colors[i] = legendEntries[i].formColor;
}
return colors;
}
private String [] getLabels(Legend legend) {
LegendEntry [] legendEntries = legend.getEntries();
String [] labels = new String[legendEntries.length];
for (int i = 0; i < legendEntries.length; i++) {
labels[i] = legendEntries[i].label;
}
return labels;
}
来源:https://stackoverflow.com/questions/42236779/mpandroidchart-getcolors-is-now-deprecated-for-legend-what-should-i-use-in