1) build.gradle( Module:app ) Code
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.
dependencies {
compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
}
repositories {
maven { url "https://jitpack.io" }
}
2) XML Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="payprocess.charts.PChart">
<com.github.mikephil.charting.charts.PieChart
android:layout_marginTop="20dp"
android:id="@+id/piechart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
3) Java Code
public class PChart extends AppCompatActivity {
PieChart mChart;
private String[] xValues = {"Data 1", "Data 2", "Data 3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pchart);
mChart = (PieChart)findViewById(R.id.piechart);
ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(40, 0));
entries.add(new Entry(11, 0));
entries.add(new Entry(2, 0));
PieDataSet dataSet = new PieDataSet(entries, "");
PieData data = new PieData(xValues, dataSet);
dataSet.setColors(new int[]{Color.YELLOW, Color.CYAN, Color.GREEN});
dataSet.setSliceSpace(5f);
dataSet.setValueTextSize(14f);
mChart.setUsePercentValues(true);
mChart.setDrawHoleEnabled(false);
mChart.setData(data);
mChart.invalidate();
}
}