Friday, April 15, 2016

Create PieChart using MPAndroidChart Library




Step: 1
======
Add Dependency to your build.gradle file in android studio

Credits to Author : PhilJay
GithubLink : https://github.com/PhilJay/MPAndroidChart


1
2
3
4
dependencies {
compile 'com.github.PhilJay:MPAndroidChart:v2.2.2'

}



Step: 2
======
create an XML layout with PieChart like below

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

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

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:gravity="center_horizontal"
android:text="February, 2016"/>
</LinearLayout>



Step: 3
======
Create an Activity like below


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.pratap.piechart;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
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.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ViewPortHandler;
import java.text.DecimalFormat;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


PieChart mChart;
// we're going to display pie chart for school attendance
private int[] yValues = {21, 2, 2};
private String[] xValues = {"Present Days", "Absents", "Leaves"};

// colors for different sections in pieChart
public static final int[] MY_COLORS = {
Color.rgb(84,124,101), Color.rgb(64,64,64), Color.rgb(153,19,0),
Color.rgb(38,40,53), Color.rgb(215,60,55)
};

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

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

// mChart.setUsePercentValues(true);
mChart.setDescription("");

mChart.setRotationEnabled(true);

mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {

@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
// display msg when value selected
if (e == null)
return;

Toast.makeText(MainActivity.this,
xValues[e.getXIndex()] + " is " + e.getVal() + "", Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected() {

}
});

// setting sample Data for Pie Chart
setDataForPieChart();


}


public void setDataForPieChart() {
ArrayList<Entry> yVals1 = new ArrayList<Entry>();

for (int i = 0; i < yValues.length; i++)
yVals1.add(new Entry(yValues[i], i));

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

for (int i = 0; i < xValues.length; i++)
xVals.add(xValues[i]);

// create pieDataSet
PieDataSet dataSet = new PieDataSet(yVals1, "");
dataSet.setSliceSpace(3);
dataSet.setSelectionShift(5);

// adding colors
ArrayList<Integer> colors = new ArrayList<Integer>();

// Added My Own colors
for (int c : MY_COLORS)
colors.add(c);


dataSet.setColors(colors);

// create pie data object and set xValues and yValues and set it to the pieChart
PieData data = new PieData(xVals, dataSet);
// data.setValueFormatter(new DefaultValueFormatter());
// data.setValueFormatter(new PercentFormatter());

data.setValueFormatter(new MyValueFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);

mChart.setData(data);

// undo all highlights
mChart.highlightValues(null);

// refresh/update pie chart
mChart.invalidate();

// animate piechart
mChart.animateXY(1400, 1400);


// Legends to show on bottom of the graph
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
l.setXEntrySpace(7);
l.setYEntrySpace(5);
}


public class MyValueFormatter implements ValueFormatter {

private DecimalFormat mFormat;

public MyValueFormatter() {
mFormat = new DecimalFormat("###,###,##0"); // use one decimal if needed
}

@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
// write your logic here
return mFormat.format(value) + ""; // e.g. append a dollar-sign
}
}





}

Screenshot
=========


























Demo
=====




No comments:

Post a Comment