Bar Chart in Android Example

In this post, you can learn how to design a bar chart for android applications. Using an external jar file named as "aChartEngine", we can develop a bar 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 bar chart for android apps.

Live Chat Discussion

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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.graphdemo"
    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.example.graphdemo.GraphActivity"
            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 the below activity code in your Android Manifest 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<Button
      android:id="@+id/BarGraph"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="BarGraph" />
</LinearLayout>


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


GraphActivity.java 

package com.udhaya.graphdemo;
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 GraphActivity extends Activity implements OnClickListener {
    Button  barGraph;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_graph);
        barGraph = (Button) findViewById(R.id.BarGraph);
        barGraph.setOnClickListener(this);
        }
    @Override
    public void onClick(View v) {
        switch(v.getId()){
                  case R.id.BarGraph:
                  BarGraph bar = new BarGraph();
                  Intent barIntent = bar.getIntent(this);
                  startActivity(barIntent);
                  break;
        }
        }
}


4. Create a class "BarGraph.java".

    
BarGraph 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.

BarGraph.java 

package com.udhaya.graphdemo;
import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
public class BarGraph {
        public Intent getIntent(Context context){
        int y[] = {25,10,15,20};
        CategorySeries series = new CategorySeries("Bar1");
        for(int i=0; i < y.length; i++){
            series.add("Bar"+(i+1),y[i]);
        }
        XYMultipleSeriesDataset dataSet = new XYMultipleSeriesDataset();  // collection of series under one object.,there could any
        dataSet.addSeries(series.toXYSeries());                            // number of series
        //customization of the chart
        XYSeriesRenderer renderer = new XYSeriesRenderer();     // one renderer for one series
        renderer.setColor(Color.RED);
        renderer.setDisplayChartValues(true);
        renderer.setChartValuesSpacing((float) 5.5d);
        renderer.setLineWidth((float) 10.5d);
         XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();   // collection multiple values for one renderer or series
        mRenderer.addSeriesRenderer(renderer);
        mRenderer.setChartTitle("Demo Graph");
//      mRenderer.setXTitle("xValues");
        mRenderer.setYTitle("Rupee");
        mRenderer.setZoomButtonsVisible(true);    mRenderer.setShowLegend(true);
        mRenderer.setShowGridX(true);      // this will show the grid in  graph
        mRenderer.setShowGridY(true);              
//      mRenderer.setAntialiasing(true);
        mRenderer.setBarSpacing(.5);   // adding spacing between the line or stacks
        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.BLACK);
        mRenderer.setXAxisMin(0);
//      mRenderer.setYAxisMin(.5);
        mRenderer.setXAxisMax(5);
        mRenderer.setYAxisMax(50);
//    
        mRenderer.setXLabels(0);
        mRenderer.addXTextLabel(1,"Income");
        mRenderer.addXTextLabel(2,"Saving");
        mRenderer.addXTextLabel(3,"Expenditure");
        mRenderer.addXTextLabel(4,"NetIncome");
        mRenderer.setPanEnabled(true, true);    // will fix the chart position
        Intent intent = ChartFactory.getBarChartIntent(context, dataSet, mRenderer,Type.DEFAULT);
        return intent;
    }
}


After completing the above steps your bar chart is ready for android devices.

15 comments:

  1. Wow. Thats simple and great. Thanks. This wa exactly what i was looking for.

    ReplyDelete
  2. Very simple and neat example. Can I get list of all classes and methods of achartengine on any site??? Thanks in advance.

    ReplyDelete
  3. hii...will u pls suggest me how to create 3D pie graph and bar graph in android??

    ReplyDelete
  4. How to create bargraph along with dynamic values and static values in android using achartengine. Every time the graph needs to be updated dynamically with values and displayed along with static values. Also I need to create a source file for updating dynamic values.

    ReplyDelete
  5. Please upload a picture of activity or graph to understand it better

    ReplyDelete

My Profile