Aditya Vyas-Lakhan
Aditya Vyas-Lakhan

Reputation: 13555

How to remove bottom space in a pie chart?

I am using Pie Chart in my application, Right now Pie Chart is displaying properly ,only issue is getting space at bottom of Pie Chart. Following is code of Pie Chart and output what I want and What I am trying to get.

Using this library for piechart

https://github.com/PhilJay/MPAndroidChart

 PieChart pieChart = (PieChart) view.findViewById(R.id.fragment_chart);
        pieChart.setUsePercentValues(true);
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        int height = display.getHeight();  // deprecated

        int offset = (int)(height * 0.65); /* percent to move */

       /* RelativeLayout.LayoutParams rlParams =
                (RelativeLayout.LayoutParams)pieChart.getLayoutParams();
        rlParams.setMargins(0, 0, 0, -offset);
        pieChart.setLayoutParams(rlParams);*/
        // IMPORTANT: In a PieChart, no values (Entry) should have the same
        // xIndex (even if from different DataSets), since no values can be
        // drawn above each other.
        ArrayList<PieEntry> yvalues = new ArrayList<PieEntry>();
        yvalues.add(new PieEntry(8f, 0));
        yvalues.add(new PieEntry(15f, 1));
        yvalues.add(new PieEntry(12f, 2));


        PieDataSet dataSet = new PieDataSet(yvalues, "Reward Points");

        ArrayList<String> xVals = new ArrayList<String>();

        xVals.add("January");
        xVals.add("February");
        xVals.add("March");
        xVals.add("April");
        xVals.add("May");
        xVals.add("June");

        PieData data = new PieData( dataSet);

        data.setValueFormatter(new PercentFormatter());
        pieChart.setData(data);
        //pieChart.setDescription("This is Pie Chart");

        pieChart.setBackgroundColor(Color.TRANSPARENT);
        pieChart.setHoleColor(Color.WHITE);
        pieChart.setTransparentCircleColor(Color.WHITE);
        pieChart.setTransparentCircleAlpha(0);
        pieChart.setHoleRadius(18f);
        pieChart.setDrawCenterText(true);
        pieChart.isRotationEnabled();
        pieChart.isHighlightPerTapEnabled();
        pieChart.setCenterTextOffset(0f,-20f);
        pieChart.setEntryLabelColor(Color.WHITE);
        pieChart.setEntryLabelTypeface(Typeface.DEFAULT);
        pieChart.setEntryLabelTextSize(16f);
        pieChart.setTransparentCircleRadius(11f);

        pieChart.setDrawHoleEnabled(true);


        pieChart.setMaxAngle(180.0f);
        pieChart.setRotationAngle(180.0f);
        pieChart.setCenterTextSize(30);

        dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
        data.setValueTextSize(13f);

        data.setValueTextColor(Color.DKGRAY);
        //pieChart.setOnChartValueSelectedListener(getActivity());

        pieChart.animateY(1400, Easing.EaseInOutQuad);

Expected Output

Out

Getting Output

enter image description here

Upvotes: 2

Views: 3345

Answers (2)

sanoJ
sanoJ

Reputation: 3128

To get the expected output,

  1. You need to add a negative bottom margin // Use LinearLayout.LayoutParams since you use a linear layout // set buttom margin (I used -450 change this as you need)

    LinearLayout.LayoutParams rlParams =
            (LinearLayout.LayoutParams) pieChart.getLayoutParams();
    rlParams.setMargins(0, 0, 0, -450);
    pieChart.setLayoutParams(rlParams);
    
  2. Disable the description

    pieChart.getDescription().setEnabled(false); // remove the description
    
  3. Increase hole radius

    pieChart.setHoleRadius(50f);        // increase hole radius
    
  4. Reduce center font size and set text

    pieChart.setCenterTextSize(15); // reduce font size
    pieChart.setCenterText("Rewarded points: 140"); // set center text
    

Also change the height to 400dp in your xml file.

<com.github.mikephil.charting.charts.PieChart
    android:id="@+id/fragment_chart"
    android:layout_width="match_parent"
    android:layout_height="400dp" />

enter image description here

Upvotes: 2

Pravin Divraniya
Pravin Divraniya

Reputation: 4376

Use below code.

pieChart.setHoleRadius(64f);//18f
pieChart.setCenterTextSize(10);//30
pieChart.setCenterText("Reward Points: 150");//new line

pieChart.setExtraOffsets(5f,0f,10f,-100f);

Adjust value of bottom offset in setExtraOffsets(in above code value -100f) in order to adjust space.enter image description here

Upvotes: 5

Related Questions