Pages

Friday 18 October 2013

Graphs using Achart Engine

Achart-Engine is software library for android devices to draw different types of charts. It supports Pie chart, Bar chart, Line chart, scatter chart, time chart.
http://www.achartengine.org/content/demo.html.

Download the achart engine jar from official site http://repository-achartengine.forge.cloudbees.com/snapshot/org/achartengine/achartengine/1.1.0/  or https://code.google.com/p/achartengine/downloads/detail?name=achartengine-1.1.0.jar&can=2&q=  or https://code.google.com/p/achartengine/downloads/list

Create Android Application project by using Eclipse IDE and add the downloaded jar to its build path.
Adding Achartengine jar to BuildPath
     1). Right click on the project
     2). Go to properties
     3). Click on the Java Build Path on the left panel
     4). Click on the Libraries on the right panel (Top Tabs)
     5). Add the jar by clicking the Add External Jar button
     6). Click on the Order and Export on the right panel (Top Tabs) and then                  select checkbox of achartengine jar.
     7). Click OK.



Example to draw Pie-chart using Achart-Engine.
Task:-  Launching screen of the application will have one button. on clicking the button a pie chart is displayed.

MainActivity.java

package com.example.chart1;

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 MainActivity extends Activity implements OnClickListener {
Button chartButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chartButton= (Button) findViewById(R.id.chart_button);
chartButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.chart_button:
PieChart1 effort = new PieChart1 ();
Intent effortIntent = effort.getIntent(this);
startActivity(effortIntent);
break;
}
}

}



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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/chart_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Charts" />

</LinearLayout>


PieChart1.java

package com.example.chart1;
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 PieChart1{
public Intent getIntent(Context context){
int []Projects = {30, 15, 15, 30,10};  
CategorySeries series = new CategorySeries("pie"); 

series.add("Project1",Projects[0]);            
series.add("Project2",Projects[1]);
series.add("Project3",Projects[2]);
series.add("Project4",Projects[3]);
series.add("Project5",Projects[4]);

// Set colors for Projects respectively           
int []colors = new int[]{Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.CYAN};
// set style for series
DefaultRenderer dRenderer = new DefaultRenderer();
for(int color : colors){
SimpleSeriesRenderer sRenderer = new SimpleSeriesRenderer();
sRenderer.setColor(color);
sRenderer.setDisplayBoundingPoints(true);

sRenderer.setDisplayChartValuesDistance(5);

sRenderer.setDisplayChartValues(true);
sRenderer.setChartValuesTextSize(15);
dRenderer.addSeriesRenderer(sRenderer);
}
dRenderer.isInScroll();
dRenderer.setZoomButtonsVisible(true);  
//set zoom button in Graph
dRenderer.setApplyBackgroundColor(true);
dRenderer.setBackgroundColor(Color.BLACK); 
//set background color
dRenderer.setChartTitle("Projects");
dRenderer.setChartTitleTextSize((float) 30);
dRenderer.setShowLabels(true);  
dRenderer.setLabelsTextSize(20);
dRenderer.setLegendTextSize(25);
dRenderer.setDisplayValues(true);
return ChartFactory.getPieChartIntent(context, series, dRenderer, "PieChart");
}

}


AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chart1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="org.achartengine.GraphicalActivity"/>    
    </application>


</manifest>








No comments:

Post a Comment