PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.67k stars 9.02k forks source link

Piechart legend cutting issue. #3521

Open GrishmaPromact opened 7 years ago

GrishmaPromact commented 7 years ago

I have attached the screenshot of my usage chart. In red box display the legends and they are cutting in pie chart. Below is my code:

   pieChart.setUsePercentValues(false);
    pieChart.getDescription().setEnabled(false);
    pieChart.setDragDecelerationFrictionCoef(0.95f);
    pieChart.setDrawHoleEnabled(true);
    pieChart.setHoleColor(Color.WHITE);
    pieChart.setTransparentCircleColor(Color.WHITE);
    pieChart.setTransparentCircleAlpha(110);
    pieChart.setHoleRadius(55f);
    pieChart.setTransparentCircleRadius(57f);
    pieChart.setDrawCenterText(true);
    pieChart.setRotationAngle(0);
    // enable rotation of the chart by touch
    pieChart.setRotationEnabled(true);
    pieChart.setHighlightPerTapEnabled(true);
    // add a selection listener
    pieChart.setOnChartValueSelectedListener(this);
    pieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);

    Legend l = pieChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(false);
    l.setForm(Legend.LegendForm.CIRCLE);
    l.setXEntrySpace(7f);
    l.setYEntrySpace(0f);
    l.setYOffset(0f);
    l.setWordWrapEnabled(true);
    l.setDrawInside(false);
    l.getCalculatedLineSizes();

    // entry label styling
    pieChart.setEntryLabelColor(Color.WHITE);
    pieChart.setEntryLabelTextSize(12f);

plz help me to solve this issue. Thanks in advance. device-2017-10-25-182740

tutysathish commented 7 years ago

Did you find any solution ?

GrishmaPromact commented 7 years ago

@tutysathish No till now. :(

rferbesa commented 7 years ago

I have exactly the same problem, both with this library and with the iOS one, so I thought that it wasn't a bug but a miss configuration of the legend. I hope you find a solution.

GrishmaPromact commented 7 years ago

@rferbesa I don't have any solution. If you have any solution then please suggest me. Thanks.

EdwardQuixote commented 6 years ago

@GrishmaPromact @tutysathish @rferbesa Hi, I found a solution. Call the following method on your PieChart: piecPieChart.setExtraBottomOffset(20f);

The method takes a float value, which represents the offset of the Chart's viewport from the Bottom.

engr-erum commented 5 years ago

Hi how u set your legend / labels vertically ? ? pls share

GrishmaPromact commented 5 years ago

@engr-erum I didn't find any solution.

Sonali-Sharma-1 commented 5 years ago

Thanks alot for this post @GrishmaPromact , I spent half day searching for legends to be vertically aligned. After i did these changes, i faced the same issue of cutting legend as this screenshot 2019-01-27 at 4 19 52 pm

aviraj commented 5 years ago

Set the following, l.setYOffset(10f);

Increase the value as per requirement

Sonali-Sharma-1 commented 5 years ago

Thanks , it worked @aviraj

GrishmaPromact commented 5 years ago

customizing with xml attribute android:translationY="-10dp" OR view.setExtraOffsets(...); you can manage dp accordingly. l.setYOffset(10f); yay... worked.

jinshenglongsw commented 5 years ago

thanks,i have this problem,too.

androidDev-1 commented 5 years ago

My legend isn't showing my 6th entry . I have written this code - public void makePieChart() { ArrayList yValues = new ArrayList<>(); // yValues.add(new PieEntry(goodCount, "good"));

    yValues.add(new PieEntry(avgCount, EXPLAIN_TREATMENT));
    yValues.add(new PieEntry(avgCount, WAITING_TIME));
    yValues.add(new PieEntry(avgCount, RECEPTIONIST_POLITE));
    yValues.add(new PieEntry(avgCount, DENTIST_FRIENDLY));
    yValues.add(new PieEntry(avgCount, CLINIC_CHAMBER_CLEAN_HYGENIC));
    yValues.add(new PieEntry(avgCount, BILLS_NOT_GIVEN));

// yValues.add(new PieEntry(poorCount, "Poor"));

    PieDataSet dataSet = new PieDataSet(yValues, "");

    dataSet.setSliceSpace(6f);
    dataSet.setSelectionShift(6f);
    dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
    pieChart.animateY(500);
    Legend l = pieChart.getLegend();
    PieData data = new PieData(dataSet);
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(false);
    l.setForm(Legend.LegendForm.CIRCLE);
    l.setXEntrySpace(10f);
    l.setYEntrySpace(0f);
    l.setYOffset(30f);
    l.setWordWrapEnabled(true);
    l.setDrawInside(false);
    l.getCalculatedLineSizes();
    data.setValueTextSize(40f);
    data.setValueTextColor(Color.BLACK);
    pieChart.setData(data);
    pieChart.highlightValues(null);
    pieChart.invalidate();

}

here is the screenshot of my UI - Screenshot_20190318_120246

GrishmaPromact commented 5 years ago

As, you are facing horizontal legends cutting. Set X offset for horizontally cutting legend.
Set the following, l.setXOffset(10f);

Increase the value as per your requirement

androidDev-1 commented 5 years ago

As, you are facing horizontal legends cutting. Set X offset for horizontally cutting legend. Set the following, l.setXOffset(10f);

Increase the value as per your requirement

@GrishmaPromact yeah that solved my cutting issue. But can u tell, why i am unable to see my 6th entry in legend?

GrishmaPromact commented 5 years ago

@anant-dev pieChart.setEntryLabelTextSize(12f); you can reduce the text size and also decrease the Yoffset to 30f to 20f and check. I am not sure.

androidDev-1 commented 5 years ago

@anant-dev pieChart.setEntryLabelTextSize(12f); you can reduce the text size and also decrease the Yoffset to 30f to 20f and check. I am not sure.

@GrishmaPromact I tried setting textSize using above method , doesnt seem to work @PhilJay Can you help ?

jordanhbuiltbyhq commented 5 years ago

This issue is easily reproducible in the example app. In PieChartActivity comment out these two lines to move the legend underneath the pie chart

l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);

and you'll see Party D is clipped Screenshot_1559408563

This issue also occurred in the iOS repo, which I reported and they fixed.

A workaround that worked for both libraries is to mess with the legend's yOffset and the pie chart's extraBottomOffset until it works for your legend. Would be good to fix this issue for android!

jagadishnallappa commented 3 years ago

Sorry, I might be late by years. But we should also make sure we do the following before we set data and invalidate it.

legend().setWordWrapEnabled(true);
// Should you wish to set the size
legend().setTextSize(getResources().getDimension(R.dimen.text_sub_heading));
// Should you wish to set the form size 
legend().setFormSize(CHART_LEGEND_FORM_SIZE);

// and then

chart.invalidate();