Android Tutorial: Displaying Chart On Android

This is first of the series of android tutorials. Before going through this tutorial, audience is expected to be familiar with android API and basic android programming. In this tutorial, we are going to learn, how to display chart or bar graphs on android for your statistical data.

To begin with the tutorial, lets create new project.

FILE–>NEW–>Android Project

create android project in eclipse

create android project in eclipse

Now right click on the project in Package explorer and go to Properties.

Properties–>Java Build Path–>Libraries–>Add external JARs..

Linking API

Adding external JARS..

Once your API is linked, you are ready now for coding your program. Here is the link where from you can download the API www.openintent.org/en/libraries .

Now lets code the program. This is the complete code of android chart sample.

 

package com.google.android.chartView;
import com.kidroid.kichart.model.Aitem;

import com.kidroid.kichart.view.LineView;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;
public class chartView extends Activity {

/** Called when the activity is first created. */

LineView lv;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

String xaxis[]=new String[4];

xaxis[0]=”2006″;

xaxis[1]=”2007″;

xaxis[2]=”2008″;

xaxis[3]=”2009″;

float line1[]=new float[4];

line1[0]=120;

line1[1]=240;

line1[2]=500;

line1[3]=100;

float line2[]=new float[4];

line2[0]=100;

line2[1]=650;

line2[2]=700;

line2[3]=300;

float line3[]=new float[4];

line3[0]=50;

line3[1]=180;

line3[2]=360;

line3[3]=900;

Aitem items[]=new Aitem[3];

items[0]= new Aitem(Color.BLUE,”pauOut”,line1);

items[1]= new Aitem(Color.GREEN,”pauOut”,line2);

items[2]= new Aitem(Color.YELLOW,”pauOut”,line3);

lv=new LineView(this);

lv.setTitle(“Yearly Budget”);

lv.setAxisValueX(xaxis);

lv.setItems(items);

setContentView(lv);

}

}

here is the final view of the chart module. After this demo, you should get stuff like this.

Android Chart

Android Chart

This is it. We are done with our first use of API successfully. Bellow are video tutorials.

Advertisements

34 thoughts on “Android Tutorial: Displaying Chart On Android

  1. Hi!
    I have a question related to this chart….is there anyway I could change it’s background colour….and if yes…how?Thank u….I really need to change the colour…..Thank u:)

  2. Pingback: draw a graph in android

  3. Hi, I have a question, I tried ur example for line and bar chart, it works well, but i need to draw a pie chart, but shows an error at the line of lv.setAxisValueX(xaxis);, i removed this line and tried , but didnt get success, can you tell how to draw a pie chart using ur example?

  4. I have read that pdf document, finally i have drawn pie chart using ur example, thank you, it was very useful and saved my time, nice tutorial

  5. Your logo is “We write what you think”, isn’t it? So, please, write down right here what I’m thinking at this very moment.

    I’m eager to see your response.

    Cheers.

    • Mr David, Greetings. Very Curious question and i liked it . Thanks for your response and i am sorry for being dark so long from the online community. You can access me through my blog at http://www.mashariati.com. And about your question, I am afraid “We write what you think” did not mean exactly what you asked. It is the other way like It meant that “We write what you want to write”

  6. I added the libary, it shows no syntax errors but when I try to run it, it throws “NoCLassDefFound” error. It apparently is unable to find mode.AItem[] class. Any ideas why this is hapening?

  7. 07-19 14:02:33.029: E/AndroidRuntime(474): at com.kidroid.kichart.view.AxisView.GenerateValue(AxisView.java:134)
    07-19 14:02:33.029: E/AndroidRuntime(474): at com.kidroid.kichart.view.AxisView.drawAxisXY(AxisView.java:75)
    07-19 14:02:33.029: E/AndroidRuntime(474): at com.kidroid.kichart.view.LineView.onDraw(LineView.java:19)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.View.draw(View.java:6880)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.View.draw(View.java:6883)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.widget.FrameLayout.draw(FrameLayout.java:357)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.View.draw(View.java:6883)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.widget.FrameLayout.draw(FrameLayout.java:357)
    07-19 14:02:33.029: E/AndroidRuntime(474): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.ViewRoot.draw(ViewRoot.java:1522)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.os.Handler.dispatchMessage(Handler.java:99)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.os.Looper.loop(Looper.java:123)
    07-19 14:02:33.029: E/AndroidRuntime(474): at android.app.ActivityThread.main(ActivityThread.java:3647)
    07-19 14:02:33.029: E/AndroidRuntime(474): at java.lang.reflect.Method.invokeNative(Native Method)
    07-19 14:02:33.029: E/AndroidRuntime(474): at java.lang.reflect.Method.invoke(Method.java:507)
    07-19 14:02:33.029: E/AndroidRuntime(474): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    07-19 14:02:33.029: E/AndroidRuntime(474): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

    When i am running this code getting these errors what happen don’t know ??
    i also added permisions to xml file please reply

  8. @ramesh : I am also getting this error….what i found is: setContentView(); is called twice must be called once, i used the setContentView(R.layout.lv); but still having error: java.lang.NoClassDefFoundError: com.kidroid.kichart.model.Aitem[]

  9. I got this error.
    Can u please help me in solving this.

    “08-09 15:12:40.866: E/AndroidRuntime(660): java.lang.NoClassDefFoundError: [Lcom.kidroid.kichart.model.Aitem;”

    i added the external jars but even the Aitem is said to be invalid.
    Please help me

  10. Hi Shariati ! ur guide is very useful for me. But can you explain me how to use it. Because my problem is when I defined Aitem variables (Aitem items[] = new Aitem[3]) It throw out an Exeption “Source not found” . By the way please send me you APIs for free.
    Thanks for your time!

  11. Hi Shariati! I have solved my problem. But it still display “Trial Version” on screen. Can i hide this text. The same question is APIs for free? If that, plz send me APIs, which I can hide “Trial Version”. Thank for your time!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s