Thursday 25 July 2013

Pie Charts in Android Using achartEngine

Leave a Comment
We often Required to present  data in Pictorial View(chart)  in our android application. Today I will implement the pie chart using achartengine library for android application.  I have downloaded the library  achartengine.jar from http://www.achartengine.org/
Create an android Project in your Eclipse IDE, I have created one with named GraphChart.

Step 1:- Add chart activity in manifest
GraphChartManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.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.arpit.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>
Step 2:- Creating a xml file
activity_graph.xml
we don’t need any xml for displaying. I am using xml for having a button on it, which in turn open my graphical activity.
<LinearLayout 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"
    android:orientation="vertical"
    >
    <Button
        android:id="@+id/Efforts_bt_Graph"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Efforts" />
</LinearLayout>
Step 3:-Create Java file 
Graph.java
package com.arpit.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;
        }
    }
    }
Step 4:- Java file for Creating actual chart
EffortChart.java
package com.arpit.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");
        }
    }
piechart

Catch Me On: CreateAppFaster.com

0 comments:

Post a Comment