首页 > 解决方案 > MPAndroidChart 饼图:未显示所有标签

问题描述

我正在使用 compile 'com.github.PhilJay:MPAndroidChart:v3.0.0' 作为 android 中的饼图,但无法查看所有标签

还会显示一个标签,但颜色不匹配。

    private void drawMap()
{

    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());
    pcVehicle.setData(data);

    dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
}<com.github.mikephil.charting.charts.PieChart
    android:padding="@dimen/padding_12"
    android:layout_margin="@dimen/margin_08"
    android:id="@+id/pcVehicle"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

在此处输入图像描述

标签: androidmpandroidchart

解决方案


您使用的颜色模板只有五种颜色。如果要绘制多于五个的数据值,则需要提供更多的颜色,否则它将使用一些相同的颜色来绘制超过五个饼图。

解决这个问题的一种方法是像这样组合来自两个不同模板的颜色。

int[] colors = new int[10];
int counter = 0;

for (int color : ColorTemplate.JOYFUL_COLORS
) {
    colors[counter] = color;
    counter++;
}

for (int color : ColorTemplate.MATERIAL_COLORS
) {
    colors[counter] = color;
    counter++;
}

dataSet.setColors(colors);

推荐阅读