ANDROID DRAWING PIE-CHART GRAPH USING ACHARTENGINE LIBRARY

In this blog post, i’ll  explain how to draw a Pie chart graph in Android using AChartEngine Library.

My Other blog on drawing charts.

  1. Bar Chart
  2. Line Chart
  3. Pie Chart
  4. Area Chart
  5. Combined Chart

 

Step 1: Create a new project File -> Android Project. While creating a new project give activity name as MainActivity.

Step 2:

  1.  Download the latest version of AChartEngine library ( jar file ) from here.
  2.  Copy the downloaded library file to “libs” directory of the project.
  3. Right click on jar file -> Build Path -> Add to Build path.

Step 3: Open MainActivity class and copy paste this code:

import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
 private View mChart;

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

 // Getting reference to the button btn_chart
 Button btnChart = (Button) findViewById(R.id.btn_chart);

 // Defining click event listener for the button btn_chart
 OnClickListener clickListener = new OnClickListener() {

 @Override
 public void onClick(View v) {
 // Draw the pie Chart
 openChart();
 }
 };

 // Setting event click listener for the button btn_chart of the
 // MainActivity layout
 btnChart.setOnClickListener(clickListener);
 }

 private void openChart() {

 // Pie Chart Section Names
 String[] code = new String[] { "Froyo", "Gingerbread",
 "IceCream Sandwich", "Jelly Bean", "KitKat" };

 // Pie Chart Section Value
 double[] distribution = { 0.5, 9.1, 7.8, 45.5, 33.9 };

 // Color of each Pie Chart Sections
 int[] colors = { Color.BLUE, Color.MAGENTA, Color.GREEN, Color.CYAN,
 Color.RED };

 // Instantiating CategorySeries to plot Pie Chart
 CategorySeries distributionSeries = new CategorySeries(
 " Android version distribution as on October 1, 2012");
 for (int i = 0; i < distribution.length; i++) {
 // Adding a slice with its values and name to the Pie Chart
 distributionSeries.add(code[i], distribution[i]);
 }

 // Instantiating a renderer for the Pie Chart
 DefaultRenderer defaultRenderer = new DefaultRenderer();
 for (int i = 0; i < distribution.length; i++) {
 SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
 seriesRenderer.setColor(colors[i]);
 seriesRenderer.setDisplayChartValues(true);
//Adding colors to the chart
 defaultRenderer.setBackgroundColor(Color.BLACK);
 defaultRenderer.setApplyBackgroundColor(true);
 // Adding a renderer for a slice
 defaultRenderer.addSeriesRenderer(seriesRenderer);
 }

 defaultRenderer
 .setChartTitle("Android version distribution as on December 1, 2014. ");
 defaultRenderer.setChartTitleTextSize(20);
 defaultRenderer.setZoomButtonsVisible(false);

 // this part is used to display graph on the xml
 // Creating an intent to plot bar chart using dataset and
 // multipleRenderer
 // Intent intent = ChartFactory.getPieChartIntent(getBaseContext(),
 // distributionSeries , defaultRenderer, "AChartEnginePieChartDemo");

 // Start Activity
 // startActivity(intent);

 LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart);
 // remove any views before u paint the chart
 chartContainer.removeAllViews();
 // drawing pie chart
 mChart = ChartFactory.getPieChartView(getBaseContext(),
 distributionSeries, defaultRenderer);
 // adding the view to the linearlayout
 chartContainer.addView(mChart);

 }

}

Step 4: Open your activity_main.xml and copy paste this code:

<RelativeLayout 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" >

 <LinearLayout
 android:id="@+id/chart"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_alignParentLeft="true"
 android:layout_below="@+id/btn_chart"
 tools:ignore="Orientation" >
 </LinearLayout>

 <Button
 android:id="@+id/btn_chart"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Draw Pie Chart" />

</RelativeLayout>


Tips to Remember always: 1. Here i’m displaying graph in the same activity by using the view, [i.e on line 88-95]. else if you want to open in new activity i.e. achartengine graph activity, then un-comment the highlighted lines in the above code and comment lines from 88-95, and this code in the manifest file under application :

<activity android:name="org.achartengine.GraphicalActivity" />

2. You can also have a background image to the chart. Insert an image in the res/drawable folder. Now open your activity_main.xml file and add this line in between 3 & 4:


android:background:"@drawable/imageName"


ScreenShots:
Screenshot_2014-12-15-15-55-03

That’s it.

Hope this helpful. Your valuable comments are always welcomed. It will help to improve my post and understanding.

Download Source Code from here.

durga chiranjeevi

durga chiranjeevi

I'm currently working as Android developer at Energy Alternatives India. Interested in Application and Game development.
durga chiranjeevi
Comments
  1. Lalita Gill

Leave a Reply

Your email address will not be published. Required fields are marked *