Mpandroidchart: PieChart legend entries not displayed if too many

Created on 17 Sep 2014  路  11Comments  路  Source: PhilJay/MPAndroidChart

First of all its the best library of many I have tried.

I wanted to display values in legend> 5 (Inside Pie Chart). Currently all legend entries are displayed in one line and they do not wrap automatically to next line if there are many entries.

I have tried following legend positions:

  1. Below Center (Values gets skipped no wrapping done)
  2. Right (Values gets skipped no wrapping done)
  3. Left (Values gets skipped no wrapping done)

Example of Legend Values Wrapping

[] Party A [] Party B [] Party C [] Party D [] Party E (Screen Size reached)
[] Party F and so on

Can you please let me know how can I achieve this in existing code.

Most helpful comment

The "issue" occurs because you are reusing colors. Provide a larger color array (different colors would make sense) to display all entries.

All 11 comments

The PieChart does not have a rule that does not allow more than 5 legend entries. How many entries can be displayed simply depends on the length of the Strings the legend uses.

At the moment the library does not support a "wrapping" mechanism for overflowing legend entries. If you want to implement it yourself, I suggest you get started inside the Chart.java file.

Around line 730 there is a method called drawLegend() you can modify it to wrap legend entries there.

Regards,
Phil

I was able to find workable solution to wrap entries under BELOW_CHART_LEFT mode.

If any one wants please find attached modification in drawLegend() method.

                     float entryspace = Utils.calcTextWidth(mLegendLabelPaint,
                        labels[i]) + mLegend.getXEntrySpace();
                if ((entryspace + posX) >= (getWidth() - getOffsetRight())) {

                    posX = mLegend.getOffsetLeft();
                    posY += Utils.calcTextHeight(mLegendLabelPaint, labels[i])+mLegend.getYEntrySpace();

                }

Copy this code before mLegend.drawLabel() call.

@PhilJay

Thanks for the reply. I found out the entry problem. I was specifying color array of size 5 so only those entries were getting displayed

Hi Phil,

First, These charts are awesome !
Just wondering, has this issue been resolved ?
I am using mpandroidchartlibrary-1-6-5.jar and still having the same issue.

Thanks.

image

The "issue" occurs because you are reusing colors. Provide a larger color array (different colors would make sense) to display all entries.

Thanks a lot. That works.

Hey Phil,

I'm wraping the legend, but when there are more than one line of legend, the chart gets smaller and smaller. Is there a way to fix the chart size?

If we reusing colors for pie chart legends not coming . Is there any solution to get legends with same color.

Hi Phil,

I am setting some text inside pie chart,
Problem is if text increases then it moves outside the pie slice.
Can we make ellipses true if text increases a particular length.

Hiii i want to set Text like below image in android.
I am used MPchart library, Please help me.

capture

I am using v 'com.github.PhilJay:MPAndroidChart:v2.2.4' and I am still facing the issue mentioned above of legends not being shown if too many. I tried checking the Chart.java class but couldn't find that function drawLegend(). Any help?

I am using latest version of MPAndroidChart and facing a problem in pie chart .
I can able to show only 5 legend but I want to show more legends according to my array list size. code which I used mentioned below:

Legend l = pieChart.getLegend();
l.setWordWrapEnabled(true);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chathudan picture chathudan  路  3Comments

Nima9Faraji picture Nima9Faraji  路  3Comments

thanhcly920 picture thanhcly920  路  3Comments

OnlyInAmerica picture OnlyInAmerica  路  3Comments

JungYongWook picture JungYongWook  路  3Comments