Pooja
Pooja

Reputation: 2467

Pie chart does not have correct size inside a CardView

I am working on an Android application and I am using MPAndroidChart lib in it. Yet application was developed using MPAndroidChart version 2-0-8. And my chart display perfectly fine as shown below.

enter image description here

But today I update the lib version to 2-1-1. I haven't done any changes to my code but my chart looks like this now.

enter image description here

Here is the xml code for chart.

 <com.github.mikephil.charting.charts.PieChart
                    android:id="@+id/chart"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/top_bottomMarginForChart"
                    android:layout_marginLeft="@dimen/top_bottomMarginForChart"
                    android:layout_marginRight="30dp"
                    android:layout_marginBottom="@dimen/top_bottomMarginForChart" />

P.S. I am displaying chart on CardView if it matters.

Is there anything that I need to update in code? As I have searched on website but didn't find anything.

Here is the Java code for chart.

mChart = (PieChart) rootView.findViewById(R.id.chart);
mChart.setHoleColor(Color.WHITE);
mChart.setHoleRadius(40f);
mChart.setDescription("");
mChart.setDrawCenterText(true);
mChart.setDrawHoleEnabled(true);
mChart.setRotationAngle(0);
mChart.setDrawSliceText(false);
mChart.setRotationEnabled(true);
mChart.setUsePercentValues(true);
mChart.setTouchEnabled(false);
mChart.animateXY(1500, 1500);

Legend

  Legend l = mChart.getLegend();
  l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_CENTER);
  l.setXEntrySpace(10f);
  l.setYEntrySpace(10f);

Data setup to chart

private void setData(int count, float range)
 {

    ArrayList<Entry> yVals1 = new ArrayList<Entry>();
    for (int i = 0; i < count ; i++)
    {
        yVals1.add(new Entry(Total_Amount[i],i));
    }

    ArrayList<String> xVals = new ArrayList<String>();
    for (int i = 0; i < count ; i++) {
        xVals.add(Array1[i]);           
    }

    PieDataSet set1 = new PieDataSet(yVals1, "");
    set1.setSliceSpace(3f);

    ArrayList<Integer> colors = new ArrayList<Integer>();
    for (int i = 0; i < count ; i++) {
        colors.add(Color.parseColor(ColorName[i]));

    }


    colors.add(ColorTemplate.getHoloBlue());
    set1.setColors(colors);

    PieData data = new PieData(xVals, set1);
    data.setDrawValues(false);
    mChart.setData(data);
    mChart.highlightValues(null);
    mChart.invalidate();

}

Upvotes: 3

Views: 1667

Answers (1)

Pooja
Pooja

Reputation: 2467

I am not sure but I found out that above issue arise only when I set PieChart height as wrap_content.

I resolved this issue by hard coding the PieChart height which I would not recommend. If anyone has other solution for this then can post it.

Upvotes: 1

Related Questions