Android pie chart example

In your application, you might want to represent data to the user using pie Chart. In this tip, I am going to show you how to create a pie chart using AChartEngine library. To use AChartEngine library, you need to download it from the link: AChartEngine download. Extract the zip file. Then copy the achartengine-1.1.0.jar file to the libs folder of your current working project. In Android Studio, you need to mark the jar file as library file by right-clicking the file and choose Add As Library…

A pie chart contains a number of categories or slices. You will use the CategorySeries class to create an instance of CategorySeries in which you can add many categories as you wish. Each category or slice has its own render. With the render you can set different options (color, displaying value, values distance, etc.) to the slice. The renders will be grouped in a DefaultRenderer instance. With the DefaultRenderer, you are able to specify different options (labels, legends, chart title, etc.) for the pie chart.
To get the pie chart view to display on the screen, you need to use the getPieChartView() method of the ChartFactory class. You pass to this method the current context, the CategorySeries instance, and the DefaultRenderer object.

pie chart example

Now to have an example application of drawing pie chart, you create a project. Then in the activity_main.xml file, you set value of the id attribute of the layout to “llayout” and write the following code in the MainActivity class to display a pie chart that has two categories or slices using AChartEngine.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.LinearLayout;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

public class MainActivity extends Activity {
   protected void onCreate(Bundle savedInstanceState) {


   private void drawPieChart(){
     // Ceate CategorySeries
     CategorySeries categorySeries=new CategorySeries("Pie chart categories");
     // Add categories

     // Add series render to each slide of the pie chart
     int[] colors={Color.GREEN, Color.MAGENTA};
     DefaultRenderer defaultRenderer=new DefaultRenderer();
     for(int i = 0 ;i<categorySeries.getItemCount();i++){
        SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
        // Spcify render options

     // Specify default render options
     defaultRenderer.setChartTitle("Female and Male Garment Workers in 2015");

     // Get pie chart view
     GraphicalView chartView = ChartFactory.getPieChartView(this, categorySeries,defaultRenderer);
     // Add the pie chart view to the layout to show
     LinearLayout chartlayout=(LinearLayout)findViewById(;

Posted by: Dara | post date: 03-15-2015 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.