Android line chart

In this tip, I am going to show you how to create a line chart in Android. AChartEngine is a useful library to create different types of charts such as line, bar, pie charts, and so on. To use AChartEngine library, you need to download it from the link: Extract the zip file. Then copy the achartengine-1.1.0.jar file to the libs folder of your current working project.
A line chart needs data values for X-axis and Y-axis. The data values for X-axis displays horizontally and the data for Y-axis displays vertically. You will create an object of XYSeries to encapsulate the XY values of the line. The series will be added to the XYMultipleSeriesDataset object using the addSeries() method. Then you create a XYSeriesRenderer render object that can be used to set the values to the line chart's attributes (line with, line color, point marker, and so on). The render is added to a renders-list using the addSeriesRender() method of the XYMultipleSeriesRenderer object. To get the line chart view that is later shown to the user, you need to use the getLineChartView() method of the ChartFactory class. You pass to this method the current context, the XYMultipleSeriesDataset object, and the XYSeriesRenderer object.
Here is the complete code of the MainActivity class to display a line chart using AChartEngine.

package com.example.andchart;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
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.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;

public class MainActivity extends ActionBarActivity {

   protected void onCreate(Bundle savedInstanceState) {
     // X-axis data
     double years[]={2011,2012,2013,2014};
     // Y-axis data
     double students[]={2000,2700,3400,4600};
     // Create XY series to encapsulate the values of XY Line chart
     XYSeries series = new XYSeries("Students graduated in Software Engineering");
     for(int i=0;i<4;i++){
     series.add(years[i], students[i]);

     // Create XY Series list and add the series to the list
     XYMultipleSeriesDataset SeriesDataset =new XYMultipleSeriesDataset();
     // Create the renderer
     XYSeriesRenderer renderer = new XYSeriesRenderer();
     // Set line width
     // Set line color
     // Include low and max values
     // Add point markers
     // Set point stroke width
     // Create Series Render list and add the render to the list    
     XYMultipleSeriesRenderer mRenderer=new XYMultipleSeriesRenderer();
     // Get Line chart view
     GraphicalView chartView = ChartFactory.getLineChartView(this, SeriesDataset, mRenderer);
     // Add the Line chart view to the layout to show
     RelativeLayout chartlayout=(RelativeLayout)findViewById(;

   public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     getMenuInflater().inflate(, menu);
     return true;

   public boolean onOptionsItemSelected(MenuItem item) {
     // Handle action bar item clicks here. The action bar will
     // automatically handle clicks on the Home/Up button, so long
     // as you specify a parent activity in AndroidManifest.xml.
     int id = item.getItemId();
     if (id == {
        return true;
     return super.onOptionsItemSelected(item);


Posted by: Dara | post date: 11-05-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.