In last Post  we check how to hide buttons in layout In this post i want to show you how to create bar chart in android using Achartengine . Achartengine is  charting software library for Android applications. Mobile phones, tablets and other gadgets running Android will probably benefit of this charting library.

you can check more for Achartengine            http://www.achartengine.org/content/demo.html

and all the source code available here  for  Achartengine    http://www.achartengine.org/download/

This is the tested code for bar graph it is the best graph I like it very much here is code...

package com.tapt.combinedtest; 
import java.util.ArrayList;
import java.util.List;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart;
import org.achartengine.chart.LineChart;
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.graphics.Color;
import android.graphics.Paint.Align;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RelativeLayout;
public class CombinedTrial extends Activity {
    public void onCreate(Bundle savedInstanceState) {
final GraphicalView gv =createIntent();
        RelativeLayout rl=(RelativeLayout)findViewById(R.id.graph);
    public GraphicalView createIntent() {
        String[] titles = new String[] { "Target"};
        double[] y1 =new double[] { 20,30,10,40,50,20,10,30,30,40,45,25};
        double barview = 12;int maxvalue=60;
        String[] x1=new String[] {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        List<double[]> x = new ArrayList<double[]>();
        for (int i = 0; i < titles.length; i++) {
          x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
        //Line Portion//
        List<double[]> values = new ArrayList<double[]>();
        values.add(new double[] { 20,20,20,20,20,20,20,20,20,20,20,20});
        int[] colors = new int[] { Color.GREEN};
        PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE};
        XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
        int length = renderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
          XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(i);
        setChartSettings(renderer, "Sales Reports", "", "Amount (Rs)", 0.5, barview, 0, maxvalue,
            Color.LTGRAY, Color.LTGRAY,x1);
        XYSeries waterSeries = new XYSeries("Sales");
        waterSeries.add(1, y1[0]);
        waterSeries.add(2, y1[1]);
        waterSeries.add(3, y1[2]);
        waterSeries.add(4, y1[3]);
        waterSeries.add(5, y1[4]);
        waterSeries.add(6, y1[5]);
        waterSeries.add(7, y1[6]);
        waterSeries.add(8, y1[7]);
        waterSeries.add(9, y1[8]);
        waterSeries.add(10, y1[9]);
        waterSeries.add(11, y1[10]);
        waterSeries.add(12, y1[11]);
        XYSeriesRenderer waterRenderer = new XYSeriesRenderer();
        waterRenderer.setColor(Color.argb(250, 0, 210, 250));
        XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);
        dataset.addSeries(0, waterSeries);
        renderer.addSeriesRenderer(0, waterRenderer);
        String[] types = new String[] { BarChart.TYPE, LineChart.TYPE};
        final GraphicalView grfv = ChartFactory.getCombinedXYChartView(this, dataset, renderer, types);
        return grfv;
    protected static XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        setRenderer(renderer, colors, styles);
        return renderer;
    protected static void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) {
        renderer.setMargins(new int[] { 35, 65, 10, 15 });
        int length = colors.length;
        for (int i = 0; i < length; i++) {
          XYSeriesRenderer r = new XYSeriesRenderer();
    protected static void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
              String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
              int labelsColor,String[] x1) {
 for(int i=1;i<=((x1.length));i++){
                renderer.addXTextLabel(i, x1[(i-1)]);  
    protected static XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues,
              List<double[]> yValues) {
            XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
            addXYSeries(dataset, titles, xValues, yValues, 0);
            return dataset;
    public static void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues,
              List<double[]> yValues, int scale) {
            int length = titles.length;
            for (int i = 0; i < length; i++) {
              XYSeries series = new XYSeries(titles[i], scale);
              double[] xV = xValues.get(i);
              double[] yV = yValues.get(i);
              int seriesLength = xV.length;
              for (int k = 0; k < seriesLength; k++) {
                series.add(xV[k], yV[k]);

No comments:

Post a Comment