Sudarshan H. S.
Sudarshan H. S.

Reputation: 39

MPAndroidChart pie chart : not showing all labels

I am using compile 'com.github.PhilJay:MPAndroidChart:v3.0.0' for pie chart in android but am not able to see all labels

Also one label is displayed but with color mismatch.

    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

Upvotes: 1

Views: 3615

Answers (2)

Asghar Nazir
Asghar Nazir

Reputation: 369

The Color Template you are using only has five colors in it. If you want to draw more data values than five you will need to provide more colors, otherwise it will use some of the same colors to draw more than five slices of pie chart.

One way to solve this is to combine colors from two different Templates like this.

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

Upvotes: 3

art
art

Reputation: 115

I do not see anywhere in your code, drawing the actual Legend. So after initialising the pie, just set the Legend

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

Upvotes: 2

Related Questions