Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize pie chart using MPAndroidChart in Android?

What I have done: I'm using the MPAndroidChart and I was able to customize it to my requirement and tried further functionalities to remove Description Label, and to increase the font and customize the legend. What I have is now ;

enter image description here

 <com.github.mikephil.charting.charts.PieChart
            android:id="@+id/chart"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center_horizontal"
            >

        </com.github.mikephil.charting.charts.PieChart>

public class PFrag extends Fragment {

    float time[] = {55, 95, 30 , 360 - (55+95+30)};
    String activity[] ={"Jan","Feb","March",""};
    PieChart pieChart;
    CircularProgressIndicator circularProgress;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.panorama_frag,container,false);

       pieChart = view.findViewById(R.id.chart);
        setupPieChart();

        //circularProgress = view.findViewById(R.id.circular_progress);
       // circularProgress.setMaxProgress(10000);
      //  circularProgress.setCurrentProgress(5000);

        return view;
    }

    private void setupPieChart(){

        //pupulating list of PieEntires
        List<PieEntry> pieEntires = new ArrayList<>();
        for( int i = 0 ; i<time.length;i++){
            pieEntires.add(new PieEntry(time[i],activity[i]));
        }
        PieDataSet dataSet = new PieDataSet(pieEntires,"");
        dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
        PieData data = new PieData(dataSet);
        //Get the chart
        pieChart.setData(data);
        pieChart.invalidate();
        pieChart.setCenterText("50% \n ");
        pieChart.setDrawEntryLabels(false);
        pieChart.setContentDescription("");
        //pieChart.setDrawMarkers(true);
        //pieChart.setMaxHighlightDistance(34);
        pieChart.setEntryLabelTextSize(12);
        pieChart.setHoleRadius(75);

        //legend attributes
        Legend legend = pieChart.getLegend();
        legend.setForm(Legend.LegendForm.CIRCLE);
        legend.setTextSize(12);
        legend.setFormSize(20);
        legend.setFormToTextSpace(2);


    }

}

What I'm looking for: Though I tried it seems unable to find a way to edit the below functionalities.

  • How to remove the "Description Label" in the right-left corner?
  • How to increase the text size of the chart?
  • How to remove the blue item from the legend so that it will be the default remaining value?

Simply what I'm looking for is a graph like below. enter image description here

To achieve that I used MPAndoridChart library after some searching and stuck here. I'm using Android Studio 3.6.1. I would really appreciate any suggestion on this.

Thank you!


I was able to solve below two queries:

  • How to remove the "Description Label" in the right-left corner

    pieChart.getDescription().setEnabled(false);
    
  • How to increase the text size of the chart? > add

    data.setValueTextSize(10);
    
like image 236
Sachz Avatar asked Apr 06 '20 18:04

Sachz


People also ask

Is there an app to make a pie chart?

About this appThe PieChart Maker application allows you to generate pie charts quickly and easily. Characteristics and functions: - Real-time visualization of the graphic during editing. - Set the legend and color for each value to be plotted.


1 Answers

To remove label inside data just use

dataSet.setDrawValues(false)
like image 176
alexanderdo Avatar answered Oct 23 '22 09:10

alexanderdo