Android Drawing Line Chart using AChartEngine

October 19, 2012

AChartEngine is a charting library for Android applications. Using AChartEngine library, we can plot various charts like line chart, area chart, pie  chart, time chart, bubble chart etc.

In this article we will draw Income vs Expense line chart in an Android application using AChartEngine library.

This application is developed in Eclipse (4.2.0) with ADT plugin (20.0.3) and Android SDK ( R20.0.3 )

1. Create a new Android application project namely “AChartEngineLineChart”

New Android Application Project

Figure 1 : New Android Application Project

2. Design application launcher icon

Design application launcher icon

Figure 2 : Design application launcher icon

3. Create a blank activity to define the class MainActivity

Create a blank activity

Figure 3 : Create a blank activity

4. Enter MainActivity details

Enter MainActivity details

Figure 4 : Enter MainActivity details

5. Delete Android Support Library from this project, if exists

By default Eclipse ( 4.2.0) adds Android Support Library to  Android application project. For this application, we don’t need to use this support library. So the library file libs/android-support-v4.jar may be removed manually via ProjectExplorer by simply right click on the file and then clicking the menu item “delete”.

6. Download the latest version of AChartEngine library ( jar file ) from here

7. Copy the downloaded library file to “libs” directory of the project

8. Update the file res/values/strings.xml

    <string name="app_name">AChartEngineLineChart</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="str_btn_chart">Income vs Expense Line Chart</string>

9. Update the layout of the MainActivity in the file res/layout/activity_main.xml

<RelativeLayout xmlns:android=""
    android:layout_height="match_parent" >

        tools:context=".MainActivity" />


10. Update the file res/values/styles.xml

    <style name="AppTheme" parent="android:Theme" />

11. Update the file res/values-v11/styles.xml

    <style name="AppTheme" parent="android:Theme.Holo" />

12. Update the file res/values-v14/styles.xml

    <style name="AppTheme" parent="android:Theme.Holo" />

13. Update the class MainActivity in the file src/in/wptrafficanalyzer/achartenginelinechart/

package in.wptrafficanalyzer.achartenginelinechart;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private String[] mMonth = new String[] {
        "Jan", "Feb" , "Mar", "Apr", "May", "Jun",
        "Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"

    public void onCreate(Bundle savedInstanceState) {

        // Getting reference to the button btn_chart
        Button btnChart = (Button) findViewById(;

        // Defining click event listener for the button btn_chart
        OnClickListener clickListener = new OnClickListener() {

            public void onClick(View v) {
                // Draw the Income vs Expense Chart

        // Setting event click listener for the button btn_chart of the MainActivity layout

    private void openChart(){
        int[] x = { 1,2,3,4,5,6,7,8 };
        int[] income = { 2000,2500,2700,3000,2800,3500,3700,3800};
        int[] expense = {2200, 2700, 2900, 2800, 2600, 3000, 3300, 3400 };

        // Creating an  XYSeries for Income
        XYSeries incomeSeries = new XYSeries("Income");
        // Creating an  XYSeries for Expense
        XYSeries expenseSeries = new XYSeries("Expense");
        // Adding data to Income and Expense Series
        for(int i=0;i<x.length;i++){
            incomeSeries.add(x[i], income[i]);

        // Creating a dataset to hold each series
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        // Adding Income Series to the dataset
        // Adding Expense Series to dataset

        // Creating XYSeriesRenderer to customize incomeSeries
        XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();

        // Creating XYSeriesRenderer to customize expenseSeries
        XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();

        // Creating a XYMultipleSeriesRenderer to customize the whole chart
        XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
        multiRenderer.setChartTitle("Income vs Expense Chart");
        multiRenderer.setXTitle("Year 2012");
        multiRenderer.setYTitle("Amount in Dollars");
        for(int i=0;i<x.length;i++){
            multiRenderer.addXTextLabel(i+1, mMonth[i]);

        // Adding incomeRenderer and expenseRenderer to multipleRenderer
        // Note: The order of adding dataseries to dataset and renderers to multipleRenderer
        // should be same

        // Creating an intent to plot line chart using dataset and multipleRenderer
        Intent intent = ChartFactory.getLineChartIntent(getBaseContext(), dataset, multiRenderer);

        // Start Activity

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;

14. Update the file AndroidManifest.xml

<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="15" />

        android:theme="@style/AppTheme" >
            android:label="@string/title_activity_main" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
        <activity android:name="org.achartengine.GraphicalActivity" />

15. Run the application

Income vs Expense Chart using AChartEngine in Android

Figure 5 : Income vs Expense Chart using AChartEngine in Android

16. Download

17. Reference

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , ,

10 Responses to Android Drawing Line Chart using AChartEngine

  1. janee on July 23, 2013 at 12:14 pm

    Is there any way to display achartengine chart in a full screen view ? (without android notification bar and title bar)

  2. shree on August 9, 2013 at 10:35 am

    Hi I want to plot data from origin say from (0,0) to (2,3) on x,y axiz in all quadrants. How can I do this using AChartengine or androidplot. I used android canvas method drawline() but for dynamic data it does not work well. Thanks

  3. Asha Yeldose on August 30, 2013 at 2:02 pm

    i’m new to android.I just want to know how i can draw a line graph with date and time( ie,yyyy-mm-dd hh:mm:ss:SS) in y-axis and threshold in x axis,which is read from a file which is updated by some other applications.Can you suggest me some helpful sources for the same.

  4. Wan Ahmad on April 9, 2014 at 12:22 pm

    This is a great tutorial. Thank you very much.

  5. abhishek on June 25, 2014 at 5:06 pm

    I want to change the interval which is appearing on y axis .how can i do that ??

  6. betül on November 2, 2015 at 2:37 am

    This is q great tutorial and i just want to know it but could not find anywhere

  7. Amit Kukreja on November 4, 2015 at 4:12 pm

    Instead of showing graph in separate activity can I have fragment for it or view in xml?

  8. tejash on November 10, 2015 at 6:56 pm

    can i plot more than 2 lines on the same graph???

  9. issac on November 20, 2015 at 12:08 pm

    how to change showlegends bottom to top

  10. Farzaneh on December 10, 2015 at 6:35 pm

    Thank’s a lot
    I want to change the color of chart background, how I could do?

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner