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: https://code.google.com/p/achartengine/downloads/list. 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.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;


public class MainActivity extends ActionBarActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     // 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();
     SeriesDataset.addSeries(series);    
     // Create the renderer
     XYSeriesRenderer renderer = new XYSeriesRenderer();
     // Set line width
     renderer.setLineWidth(3);
     // Set line color
     renderer.setColor(Color.MAGENTA);
     // Include low and max values
     renderer.setDisplayBoundingPoints(true);
     // Add point markers
     renderer.setPointStyle(PointStyle.DIAMOND);
     // Set point stroke width
     renderer.setPointStrokeWidth(3);    
     // Create Series Render list and add the render to the list    
     XYMultipleSeriesRenderer mRenderer=new XYMultipleSeriesRenderer();
     mRenderer.addSeriesRenderer(renderer);
     // 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(R.id.chartlayout);
     chartlayout.addView(chartView);
      
   }

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

   @Override
   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 == R.id.action_settings) {
        return true;
     }
     return super.onOptionsItemSelected(item);
   }
}

Android-line-chart

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.