Pie Chart in Android Example

In this post, you can learn how to design a Pie chart for android applications. Using an external jar file named as "aChartEngine", we can develop a pie chart for android apps. First download "achartengine.jar" from code.google.com/p/achartengine/downloads/list and paste it in the "lib" folder of your android structure. Now follow the below steps and do complete your pie chart for android apps.

1.  Just add the following activity code in the android manifest XML file.
<activity android:name="org.achartengine.GraphicalActivity"/> 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
    package="com.udhaya.graphchart"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
         >
        <activity
            android:name="com.udhaya.graphchart.Graph"
            android:label="@string/app_name"
          >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
  </activity>
<!—Add this activity in your AndroidManifest file -->
  <activity android:name="org.achartengine.GraphicalActivity"/>    
  </application>
</manifest>

2.  Place the "activity_graph.xml" in the "res" folder.



activity_graph.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <Button
        android:id="@+id/Efforts_bt_Graph"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Efforts" />
</LinearLayout>

3. Now write the Java code units for "Graph Activity".



GraphActivity.java 

package com.udhaya.graphchart;
import com.graphchart.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Graph extends Activity implements OnClickListener {
    Button efforts;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_graph);
        efforts = (Button) findViewById(R.id.Efforts_bt_Graph);
        efforts.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.Efforts_bt_Graph:
            EffortChart effort = new EffortChart();
            Intent effortIntent = effort.getIntent(this);
            startActivity(effortIntent);
            break;
        }
    }
    }

4. Create a class "PieGraph.java".

PieGraph is a class in which the graph is actually created by using the methods of "achartengine" library.
Here we are using static values inside the code units for generating the chart. It can be modified dynamically by using corresponding code units as per your requirements and the values can be fetched from static device memory (SQLite Database) or even from the server database.

PieGraph.java 

package com.udhaya.graphchart;
import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
public class EffortChart {
    public Intent getIntent(Context context){
      // this is my data of performance; data is collected in array.
       int []Performance = {42, 15, 19};  // [0] for Call, [1] for Meeting, [2] for Email
        CategorySeries series = new CategorySeries("pie"); // adding series to charts. //collect 3 value in array. therefore add three series.
            series.add("Call",Performance[0]);            
            series.add("Meeting",Performance[1]);
            series.add("Email",Performance[2]);
// add three colors for three series respectively            int []colors = new int[]{Color.MAGENTA, Color.WHITE, Color.GREEN};
// set style for series
            DefaultRenderer renderer = new DefaultRenderer();
            for(int color : colors){
                SimpleSeriesRenderer r = new SimpleSeriesRenderer();
                r.setColor(color);
                r.setDisplayBoundingPoints(true);
                r.setDisplayChartValuesDistance(5);
                r.setDisplayChartValues(true);
                r.setChartValuesTextSize(15);
                renderer.addSeriesRenderer(r);
            }
            renderer.isInScroll();
            renderer.setZoomButtonsVisible(true);   //set zoom button in Graph
            renderer.setApplyBackgroundColor(true);
            renderer.setBackgroundColor(Color.BLACK); //set background color
            renderer.setChartTitle("Efforts");
            renderer.setChartTitleTextSize((float) 30);
            renderer.setShowLabels(true);  
            renderer.setLabelsTextSize(20);
            renderer.setLegendTextSize(25);
            renderer.setDisplayValues(true);
            return ChartFactory.getPieChartIntent(context, series, renderer, "PieChart");
        }
    }

4 comments:

  1. wonderful post helped a lot.how to download this code please..........
    Thank you for your Tutorial very use full Tutorial..............

    ReplyDelete
  2. sir can you help me to display same data in line chart and bar chart ?

    ReplyDelete

My Profile