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.51k stars 9.01k forks source link

PieChart getting cut on data array size change #3270

Open AkshayGangurde opened 7 years ago

AkshayGangurde commented 7 years ago

data_size_5 data_size_4 data_size_1

If you look into the images the behavior is changed just because of data array passed to it. Nothing is changed in code. XML: <com.github.mikephil.charting.charts.PieChart android:id="@+id/pChart" android:layout_width="0dp" android:layout_height="300dp" android:layout_weight="1.3"/> JAVA: `private void setData(int[] data, String sAvg) { mChart.setUsePercentValues(true); mChart.getDescription().setEnabled(false); mChart.setExtraOffsets(-15, -50, 10, 10); mChart.setDragDecelerationFrictionCoef(0.95f);

    Typeface font = FontCache.get(mContext, "fonts/OpenSans-Regular.ttf");
    mChart.setCenterTextTypeface(font);
    mChart.setCenterText(generateCenterSpannableText(sAvg));

    mChart.setDrawHoleEnabled(true);
    mChart.setHoleColor(Color.WHITE);
    mChart.setHoleRadius(65f);
    mChart.setTransparentCircleRadius(65f);
    mChart.setDrawCenterText(true);
    mChart.setRotationAngle(270);
    mChart.setRotationEnabled(false);
    mChart.setHighlightPerTapEnabled(true);

    ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
    String[] zoneName = mContext.getResources().getStringArray(R.array.hrm_zone_array);
    for (int i = 0; i < data.length; i++)
    {
        if (data[i] > 0)
        {
            entries.add(new PieEntry((float) (data[i]), zoneName[i]));
        }
    }

    PieDataSet dataSet = new PieDataSet(entries, "");
    dataSet.setSliceSpace(2f);
    dataSet.setSelectionShift(2f);

    int[] zoneColors = {
            Color.rgb(164, 164, 164),
            Color.rgb(54, 155, 227),
            Color.rgb(42, 160, 84),
            Color.rgb(242, 171, 21),
            Color.rgb(217, 41, 61)
    };

    ArrayList<Integer> colors = new ArrayList<Integer>();
    for (int i = 0; i < data.length; i++)
    {
        if (data[i] > 0)
        {
            colors.add(zoneColors[i]);
        }
    }
    dataSet.setColors(colors);

    PieData pieData = new PieData(dataSet);
    pieData.setValueFormatter(new MyValueFormatter());
    pieData.setValueTextSize(10f);
    pieData.setValueTextColor(Color.WHITE);
    pieData.setValueTypeface(Typeface.DEFAULT_BOLD);

    mChart.setData(pieData);
    mChart.highlightValues(null);
    mChart.invalidate();
    mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);

    Legend l = mChart.getLegend();
    l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setXEntrySpace(7f);
    l.setYEntrySpace(0f);
    l.setYOffset(0f);
    mChart.getLegend().setWordWrapEnabled(true);
    mChart.setDrawEntryLabels(false);
}`

Please suggest something on this issue.

GuitarHero02 commented 5 years ago

i think you need to add below code before mChart.invalidate() mChart.notifyDataSetChanged()

Soumyakantalt commented 3 years ago

Hi, due to your offset values this things happening. Once check your offset values mChart.setExtraOffsets(-15, -50, 10, 10); and changed to mChart.setExtraOffsets(0, 0, 0, 0);