【问题标题】:MPAndroidChart pie chart : not showing all labelsMPAndroidChart 饼图:未显示所有标签
【发布时间】:2018-05-18 13:01:43
【问题描述】:

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

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

    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" />

【问题讨论】:

  • 有解决这个问题的办法吗?

标签: android mpandroidchart


【解决方案1】:

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

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

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);

【讨论】:

    【解决方案2】:

    我在您的代码中看不到任何地方,绘制了实际的图例。所以初始化饼图后,只需设置图例

    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);
    
        Legend l = pcVehicle.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setWordWrapEnabled(true);
        l.setDrawInside(false);
        l.setYOffset(5f);
    }
    

    【讨论】:

    • 我用了这个,但仍然不适合我,还有其他解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多