MPAndroidChart: getColors() is now deprecated for 'Legend'. What should I use instead?

元气小坏坏 提交于 2019-12-11 07:55:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!