pie chart in android studio

How to Create Pie Chart In Android Studio[2020]

Hi, In this post we are going to learn, How we can create a pie chart in the android studio. The pie chart is very important for data analysis because it is easy to understand.

We can create a pie chart by using MP Android chart dependency. MP Android chart is an easy and powerful way to design many types of charts.

Without wasting your time, we are going to explain to you how to make charts.

Create Pie Chart In Android Studio

Step 1: Adding Dependency

The first things you need to do is to add the dependency.

Dependencies allow us to include an external library or local jar files or other library modules in Android project

Go to Android then build.gradle(Module: app) and add this inside dependency

dependencies {
 implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}

Now add following line of code in your repository in build.gradle(Project: App Name)

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
        
    }
}

Now sync your project.

Step 2: Adding Pie Chart in .xml

After we add dependency, our second step is to add the pie chart in .xml

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

Step 3: Adding values in Pie Chart

After adding pie chart in xml we need to pass data in Pie chart, we do this in java file by using following code

public class MainActivity extends AppCompatActivity {

PieChart pieChart;
PieData pieData;
List<PieEntry> pieEntryList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pieChart = findViewById(R.id.pieChart);
        pieChart.setUsePercentValues(true);
        pieEntryList.add(new PieEntry(10,"India"));
        pieEntryList.add(new PieEntry(5,"US"));
        pieEntryList.add(new PieEntry(7,"UK"));
        pieEntryList.add(new PieEntry(3,"NZ"));
        PieDataSet pieDataSet = new PieDataSet(pieEntryList,"country");
        pieData = new PieData(pieDataSet);
        pieChart.setData(pieData);
        pieChart.invalidate();
    }
}

Now, I am going to explain each line of code. First, we create a reference variable of the pie chart, pie data. We also create a list of pie Entry by following line of code

PieChart pieChart;
PieData pieData;
List<PieEntry> pieEntryList = new ArrayList<>();

Pie chart uses pie data to represent values, and Pie Data uses a list of pie entries to represent values.

After this inside onCreate() we connect Pie chart to its xml view. We set Pie chart to use percent values to represent data by these line of code

        pieChart = findViewById(R.id.pieChart);
        pieChart.setUsePercentValues(true);

Now, we add pieEntry objects in pieEntry List and then add it in the Pie Data set and then we add pie data set to pie data by given code.

Pie entry takes two arguments in the constructor, first represents the value, and the second represents the name.

       pieEntryList.add(new PieEntry(10,"India"));
        pieEntryList.add(new PieEntry(5,"US"));
        pieEntryList.add(new PieEntry(7,"UK"));
        pieEntryList.add(new PieEntry(3,"NZ"));
        PieDataSet pieDataSet = new PieDataSet(pieEntryList,"country");
        pieData = new PieData(pieDataSet);

After this, we only need to connect it to the pie chart and we do this easily by given code.

setData() sets the data and invalidate() is used if we update our values then it automatically changes the values in the pie chart

       pieChart.setData(pieData);
        pieChart.invalidate();

If you run your code you see something like this, yes! I know what are you thinking.

pie chart in android studio
pie chart in android studio

We can change the color and UI in the next heading

Step 4: Customizing Pie chart

Changing Colours

We are going to change colour of pie chart, to do so we just add the setColor() method in pie data set like this

  pieDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
pie chart in android studio
pie chart in android studio

Complete code

public class MainActivity extends AppCompatActivity {
PieChart pieChart;
PieData pieData;
List<PieEntry> pieEntryList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pieChart = findViewById(R.id.pieChart);
        pieChart.setUsePercentValues(true);
        pieEntryList.add(new PieEntry(10,"India"));
        pieEntryList.add(new PieEntry(5,"US"));
        pieEntryList.add(new PieEntry(7,"UK"));
        pieEntryList.add(new PieEntry(3,"NZ"));
        PieDataSet pieDataSet = new PieDataSet(pieEntryList,"country");
        pieDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
        pieData = new PieData(pieDataSet);
        pieChart.setData(pieData);
        pieChart.invalidate();
    }
}

Removing hole in Pie chart

If you want to remove the hole in pie chart, you need to use this method

 pieChart.setDrawHoleEnabled(false);
pie chart in android studio
pie chart in android studio

What it does it, It removes your hole radius, also If you want to change your hole radius you can do this by the following code

pieChart.setHoleRadius(3);

Change Description Of Pie Chart

To change the description of pie chart, you need to add simply this line of code

pieChart.getDescription().setText("Country");
description label in android studio
description label in android studio

Conclusion:

I Hope, you learn something new in this post. You can refer docs if you want some other feature in the pie chart or simply comment on your doubts.

Read about toast, the back button in the android studio here. com github mikephil charting charts piechart

Sudhanshu is Technology geek and also a pro pubg player. I like to create content in my free time that helps others. Currently pursuing BCA from Noida and operating Geekstocode

Leave a Comment

Your email address will not be published. Required fields are marked *