Friday, June 24, 2022
HomeAndroid ChartsMP PhilJay Pie Chart

MP PhilJay Pie Chart

Android MP PhilJay Pie Chart Android Studio

This tutorial describes how to create Android MP PhilJay Pie Chart Android Studio with MP PhilJay liberary and how to use bar entry from array list. and also describes how to create onClickListenter to show each Pie chart value in alert dialog. also, you will learn how design and show data for each pie slice. like setting the legend, colors, setting X and Y value position. what ever you need inside slice or outside the slice.

Android mp philjay charts library is jitpack library which is maven repositories. So, it requires jitpack.io dependency. Lets start.

MP Philjay Bar Chart Dependencies

Start a new android studio and open project level gradle file, then add the jitpack maven dependency.

android project level gradle file
Open project level gradle file
jitpack.io maven dependency
add jitpack.io maven dependency in allprojects
maven { url "https://jitpack.io" }

Then, open module level gradle file and add the following dependencies. And click sync.

android module level gradle file
open module level gradle file
mp philjay charts dependencies
add mp philjay dependency
implementation 'com.android.support:design:28.0.0'
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'

Pie Chart XML Layout

The next step, Open main activity xml file and add android mp philjay bar chart.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

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


</LinearLayout>
android mp philijay pie chart xml file

Then, create new java class the will represent the model of sales per region.

public class RegionalSalesData {
String region;
int sales;

public RegionalSalesData(String region, int sales) {
this.region = region;
this.sales = sales;
}

public String getRegion() {
return region;
}

public void setRegion(String region) {
this.region = region;
}

public int getSales() {
return sales;
}

public void setSales(int sales) {
this.sales = sales;
}
}

Open main activity java file and create general objects of PieChart, arrayList of PieEntries. And initiate them in onCreate method. Create ArrayList of RegionalSalesData class.

pie chart android studio
public class MainActivity extends AppCompatActivity {
PieChart pieChart;
ArrayList<PieEntry> pieEntries;
ArrayList<RegionalSalesData> regionalSalesDataArrayList =new ArrayList<>();

Next, we need to create the method that file the RegionalSalesArrayList with data the the pie chart will show it.

private void fillRegionalSalesArrayList(){

regionalSalesDataArrayList.add(new RegionalSalesData("Alex",242000));
regionalSalesDataArrayList.add(new RegionalSalesData("Cairo",300000));
regionalSalesDataArrayList.add(new RegionalSalesData("Suez",150000));
regionalSalesDataArrayList.add(new RegionalSalesData("Upper EGypt",200000));
}

And call the method in onCreate method of main activity.

fillRegionalSalesArrayList();

The PieEntryArrayList Step, we need to loop through RegionalSalesArrayList and add each sales of the month to pieEntryList. Each PieEntry object requires two parameters. The first is float value(sales) and the second is String label(region).

for (int i =0; i < regionalSalesDataArrayList.size();i++){
String region = regionalSalesDataArrayList.get(i).getRegion();
int sales = regionalSalesDataArrayList.get(i).getSales();
pieEntries.add(new PieEntry(sales,region));
}

You are a hero, next we need to create a new PieDataSet object that takes two parameters. The first is list of PieEntry list and the second is String Label name. You can set the colors of pie Chart through pieDataset.setColors() method. To set name of regions outside the slice through setXvaluePosition() and setYValuePosition() methods of pie chart. To set text size of sales use setValueTextSize() method.

PieDataSet pieDataSet = new PieDataSet(pieEntries,"Regional Sales");
pieDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
pieDataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); 
pieDataSet.setValueTextSize(16);

Very Good, next create new PieData Object and cast pieDataSet to it, and cast the pieData to the pieChart.

PieData pieData = new PieData(pieDataSet);
pieChart.setData(pieData);

MP Philjay Chart Legend

The chart legend represent the names of regions with colored square which determine the color of each region on the pie chart slice. see next image.

mp philjay charts legend
pie chart legend
Legend legend = pieChart.getLegend();
legend.setTextSize(13);
legend.setDrawInside(false);
legend.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
legend.setWordWrapEnabled(true);
pieChart.animateXY(2000,2000);
pieChart.invalidate();

You are a hero android developer , Lets run your app to show the chart.

android mp philjay pie chart

Pie chart Android set on Pie value selected listener

pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, Highlight h) {
int x = pieChart.getData().getDataSetForEntry(e).getEntryIndex((PieEntry)e);
String region = regionalSalesDataArrayList.get(x).getRegion();
String sales = NumberFormat.getCurrencyInstance().format(regionalSalesDataArrayList.get(x).getSales());
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.regional_sales_layout,null);
TextView regionTxtView = view.findViewById(R.id.region);
TextView salesTxtView = view.findViewById(R.id.sales);
regionTxtView.setText(region);
salesTxtView.setText(sales);
builder.setView(view);
alertDialog = builder.create();
alertDialog.show();

}

@Override
public void onNothingSelected() {

}
});

Thank You

Tutorials You may interested in.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments

KU NUR RASYIDAH BINTI KU BIDE on Custom Calendar View Android | Project Java Files
SHIVANT KUMAR PANDEY on Login And Registration by Firebase
Adriane Troy U. Alariao on Custom Calendar View With Events Android