Android bar chart

In this Android tip, I am going to show you how to create a bar chart using AChartEngine. If you want to create other types of charts, you read pie chart and line chart tutorials.
Creating a bar chart is similar to creating a line chart. First, you need arrays of data for X and Y values. Then you create XYSeries for the data arrays. You will add X and Y values stored in the arrays to the XYSeries. The XYSeries must be added to the XYMultipleSeriesDataset. The XYSeries need XYSeriesRenderer to draw the XYSeries on the screen. If you have two XYSeries, you also need two XYSeriesRenderer. The XYSeriesRenderer will be placed in the XYMultipleSeriesRenderer. To display the bar chart, you call the getBarChartView method of ChartFactory class to get a bar chart view object. You pass the current context, XYMultipleSeriesDataset, XYMultipleSeriesRenderer, and type of the bar chart. There are two types of bar chart: DEFAULT and STACKED that can be passed to the getBarChartView method.
Finally, add the view to you layout.



Now to have an example app on drawing a bar chart using AChartEngine, you create a new Android project. Add the id attribute with the chartlayout value to the parent layout in the activity_main.xml file.

bar chart

Here is the example code to draw a stacked bar chart. It has two groups of bars or columns. The bars that have magenta color are of the first group and the light gray bars are of the second group.

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
public class MainActivity extends Activity {
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     drawBarChart();    
}
   private void drawBarChart(){    
     // X-axis data
     double years[]={2012,2013,2014};
     // Y-axis data arrays
     double male_students[]={2700,3400,4600};
     double female_students[]={1700,2400,4000};
     // Create XY series for the first data array
     XYSeries series = new XYSeries("Male students graduated in Network Engineering");
     for(int i=0;i<years.length;i++){
        series.add(years[i], male_students[i]);
     }
     // Create XYSeries for the second data array
     XYSeries series1 = new XYSeries("Female students graduated in Network Engineering");
     for(int i=0;i<years.length;i++){
        series1.add(years[i], female_students[i]);
     }
     // Create XY Series list and add the series to the list
     XYMultipleSeriesDataset SeriesDataset =new XYMultipleSeriesDataset();
     SeriesDataset.addSeries(series);
     SeriesDataset.addSeries(series1);
     // Create renderer for the series
     XYSeriesRenderer renderer = new XYSeriesRenderer();
     // Set bar color
     renderer.setColor(Color.MAGENTA);
     // Create render for the series1
     XYSeriesRenderer renderer1 = new XYSeriesRenderer();
     // Set bar color
     renderer1.setColor(Color.LTGRAY);
     // Create renderer list and add renderers to the list
     XYMultipleSeriesRenderer mRenderer=new XYMultipleSeriesRenderer();
     mRenderer.addSeriesRenderer(renderer);
     mRenderer.addSeriesRenderer(renderer1);
     // Set chart background color
     mRenderer.setApplyBackgroundColor(true);
     mRenderer.setBackgroundColor(Color.BLACK);
     // Get stacked bar chart view
     // Types of bar chart: DEFAULT and STACKED
     GraphicalView chartView = ChartFactory.getBarChartView(this, SeriesDataset, mRenderer, BarChart.Type.STACKED);
     // Add the bar chart view to the layout to show
     LinearLayout chartlayout=(LinearLayout)findViewById(R.id.chartlayout);
     chartlayout.addView(chartView);
   }
}

Posted by: Dara | post date: 03-21-2015 | 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.