How to implement simple Pie Chart in app | Android Studio | Java | Android App Development
In this post, you will learn how to implement Pie Chart in you Android Application with Java.
What is Pie Chart and what are its uses ? Pie charts are used in data handling and are circular charts divided up into segments which each represent a value. Pie charts are divided into sections (or 'slices') to represent values of different sizes. Pie charts are a visual device to help us understand data more easily.
Implementation:
First, add the dependency for Pie Chart in build.gradle(Module: app) with latest version.
implementation 'com.github.blackfizz:eazegraph:1.2.5l@aar' implementation 'com.nineoldandroids:library:2.4.0'
Now, in the activity_main.xml file, add the following code:
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/layout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<org.eazegraph.lib.charts.PieChart | |
android:id="@+id/pie_chart" | |
android:layout_width="300dp" | |
android:layout_height="300dp" | |
android:padding="10dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.5" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<Button | |
android:id="@+id/btn_click" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="92dp" | |
android:text="click" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.5" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/pie_chart" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
Now, in the MainActivity.java file, add the following code:
(make sure to include your package name while copying the code)
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import androidx.appcompat.app.AppCompatActivity; | |
import org.eazegraph.lib.charts.PieChart; | |
import org.eazegraph.lib.models.PieModel; | |
public class MainActivity extends AppCompatActivity { | |
private Button click; | |
private PieChart chart; | |
private int i1 = 15; | |
private int i2 = 25; | |
private int i3 = 35; | |
private int i4 = 45; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
click = findViewById(R.id.btn_click); | |
chart = findViewById(R.id.pie_chart); | |
click.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
addToPieChart(); | |
} | |
}); | |
} | |
private void addToPieChart() { | |
// add to pie chart | |
chart.addPieSlice(new PieModel("Integer 1", i1, Color.parseColor("#FFA726"))); | |
chart.addPieSlice(new PieModel("Integer 2", i2, Color.parseColor("#66BB6A"))); | |
chart.addPieSlice(new PieModel("Integer 3", i3, Color.parseColor("#EF5350"))); | |
chart.addPieSlice(new PieModel("Integer 4", i4, Color.parseColor("#2986F6"))); | |
chart.startAnimation(); | |
click.setClickable(false); | |
} | |
} |
Output:
Comments
Post a Comment