0

Create Pie Chart Graph In Android App Using MpAndroidChart Library Android Studio Tutorial

How to make round degree chart with animation in android with multiple product details.

As in our last tutorial we have learn about making Bar Char Graph with the use of MpAndroidChart library so in this tutorial we are going to again learn about making a new type of round pie chart . Pie chart is mostly used where android developer wants to show the data in round graph with degree and also percent format. So here is the complete step by step tutorial for Create Pie Chart Graph In Android App Using MpAndroidChart Library Android Studio .

android-project-download-code-button

Please add below following repositories and dependencies code to your build.gradle( Module:app ) file and You don’t need to download manually the whole library because after adding below code it will automatically download and install the Mp Android Library into your project.

build-gradle

NOTE : Add this to your build.gradle( Module:app ) file.

repositories {
 maven { url "https://jitpack.io" }
}

dependencies {
 compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
}

Here is the screenshot of my build.gradle( Module:app ) file after adding above code .

mp-android-chart

How to Create Pie Chart Graph In Android App Using MpAndroidChart Library Android Studio Tutorial .

Code for MainActivity.java file.

package com.android_examples.piechart_android_examplescom;
import java.util.ArrayList;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

public class MainActivity extends AppCompatActivity {

    PieChart pieChart ;
    ArrayList<Entry> entries ;
    ArrayList<String> PieEntryLabels ;
    PieDataSet pieDataSet ;
    PieData pieData ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pieChart = (PieChart) findViewById(R.id.chart1);

        entries = new ArrayList<>();

        PieEntryLabels = new ArrayList<String>();

        AddValuesToPIEENTRY();

        AddValuesToPieEntryLabels();

        pieDataSet = new PieDataSet(entries, "");

        pieData = new PieData(PieEntryLabels, pieDataSet);

        pieDataSet.setColors(ColorTemplate.COLORFUL_COLORS);

        pieChart.setData(pieData);

        pieChart.animateY(3000);

    }

    public void AddValuesToPIEENTRY(){

        entries.add(new BarEntry(2f, 0));
        entries.add(new BarEntry(4f, 1));
        entries.add(new BarEntry(6f, 2));
        entries.add(new BarEntry(8f, 3));
        entries.add(new BarEntry(7f, 4));
        entries.add(new BarEntry(3f, 5));

    }

    public void AddValuesToPieEntryLabels(){

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

    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.piechart_android_examplescom.MainActivity">

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

</RelativeLayout>

Screenshot:

Create Pie Chart Graph In Android App Using MpAndroidChart Library Android Studio Tutorial

Click here to download Create Pie Chart Graph In Android App Using MpAndroidChart Library Android Studio Tutorial project with source code.

Leave a Reply

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