MPAndroidChart饼图:未显示所有标签

时间:2018-05-18 13:01:44

标签: android 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" />

enter image description here

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

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

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

public function scopeFromCountryProvider($query, $country, $provider)
{
    $query->whereHas('countries', function($subquery_country) use ($country) {

        $subquery_country->where('name', $country);

    })->whereHas('providers', function($subquery_provider) use ($provider) {

        $subquery_provider->where('name', $provider);

    });
}

public function countries()
{
    return $this->belongsToMany(Country::class);
}

public function providers()
{
    return $this->belongsToMany(Provider::class)
}
相关问题