Wednesday, December 1, 2010

Line charts and BAr charts


Complete Code:

U have ADD ACHARTENGINE.JAR file Also


LINE CHART:





import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

/**
 * Average temperature demo chart.
 */
public class LineChartForDailyReport extends AbstractChart {

    private LinkedHashMap rptMap;
    private String name;
    public LineChartForDailyReport(LinkedHashMap rptMap)
    {
        this.rptMap = rptMap;
    }
    public void setName( String name)
      {
        this.name = name; 
      }


public String getName() {
        return this.name;
      }
 
/**
   * Returns the chart name.
   * @return the chart name
   */
  public String getName1() {
    return "Average temperature";
  }
 
  /**
   * Returns the chart description.
   * @return the chart description
   */
  public String getDesc() {
    return "The average temperature in 4 Greek islands (line chart)";
  }
 
  /**
   * Executes the chart demo.
   * @param context the context
   * @return the built intent
   */
  public Intent execute(Context context) {
    String[] titles = new String[] { this.getName() };
    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 });
    }
    List<double[]> values = new ArrayList<double[]>();
    //values.add(new double[] { 1203, 1250, 1308, 1608, 2004, 2404, 2604, 2601, 2306, 2003, 1702,
      //  1309 });
    //values.add(new double[] { 1000, 1000, 1200, 1500, 2000, 2400, 2600, 2600, 2300, 1800, 1400, 1100 });
   // values.add(new double[] { 500, 2300, 800, 1200, 1700, 2200, 2400, 2400, 1900, 1500, 900, 600 });
    values.add(new double[] { 900, 1000, 1100, 1500, 1900, 2300, 2600, 2500, 2200, 1800, 1300, 1000 });
    int[] colors = new int[] { Color.CYAN };
    PointStyle[] styles = new PointStyle[] { PointStyle.TRIANGLE };
       
    XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
    int length = renderer.getSeriesRendererCount();
    for (int i = 0; i < length; i++) {
      ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
    }
    setChartSettings(renderer, this.getName(), "Day", "Revenue", 1, 13, 0, 3000,
        Color.GRAY, Color.WHITE);
    renderer.setXLabels(13);
    renderer.setYLabels(10);
    renderer.setShowGrid(true);
  
    Intent intent = ChartFactory.getLineChartIntent(context, buildDataset(titles, x, values),
        renderer, "Daily Reports");
    return intent;
  }


 

}




BAR CHARt



import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;

import android.R.color;
import android.content.Context;
import android.content.Intent;
import android.content.pm.LabeledIntent;
import android.graphics.Color;
import android.webkit.WebSettings.TextSize;

public class MonthlyBarChart extends AbstractChart {
   
    private LinkedHashMap rptMap;
    private String name;
   
    public MonthlyBarChart(LinkedHashMap rptMap)
    {
        this.rptMap = rptMap;
    }
    /**
       * Returns the chart name.
       * @return the chart name
       */
      public void setName( String name)
      {
        this.name = name; 
      }
      public String getName() {
        return this.name;
      }

     
     
      public String getDisplayName() {
            return "Woodland Chennai";
          }
     
      /**
       * Returns the chart description.
       * @return the chart description
       */
      public String getDesc() {
        return "The monthly sales for the last 2 years (horizontal bar chart)";
      }
     
     

      /**
       * Executes the chart demo.
       * @param context the context
       * @return the built intent
       */
      public Intent execute(Context context) {
        String title =  "2009";
       
        Collection colValues = rptMap.values();
        ArrayList<Float> values = new ArrayList<Float>(colValues);
      
        float maxValue  = ChartUtils.getMaxValue(this.rptMap) * ChartConstant.BAR_CHART_MAX_MULTIPLIER;
     //   values.add(new double[] { 230, 300, 240, 540/*, 900, 200, 30, 200, 500, 105,
        //   600, 500 */});
     
        int[] colors = new int[] { Color.parseColor("#ca892e")};
        XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
        renderer.setOrientation(Orientation.HORIZONTAL);
        setChartSettings(renderer, this.getName(), "Month", "Revenue", 0,
            13, 0, maxValue, Color.GRAY, Color.WHITE);
        renderer.setXLabels(1);
        renderer.setYLabels(10);
        renderer.addTextLabel(1, "Jan");
        renderer.addTextLabel(2, "Feb");
        renderer.addTextLabel(3, "Mar");
        renderer.addTextLabel(4, "Apr");
        renderer.addTextLabel(5, "May");
        renderer.addTextLabel(6, "June");
        renderer.addTextLabel(7, "Jul");
        renderer.addTextLabel(8, "Aug");
        renderer.addTextLabel(9, "Sep");
        renderer.addTextLabel(10, "Oct");
        renderer.addTextLabel(11, "Nov");
        renderer.addTextLabel(12, "Dec");
        renderer.setDisplayChartValues(true);
        renderer.setXTitle("Month");
        renderer.setYTitle("Revenue");
      

        return ChartFactory.getBarChartIntent(context, buildBarDataset(title, values), renderer, Type.DEFAULT);
      }

}

8 comments:

  1. can you give a complete code of this example thnku

    ReplyDelete
  2. Have a complete code for this example??

    I have error like this ......


    return ChartFactory.getBarChartIntent(context, buildBarDataset(title, values), renderer, Type.DEFAULT);


    There are method to create a new method,But u cant create this.....................

    ReplyDelete
  3. Can u give the full code

    ReplyDelete
  4. please post the complete code

    ReplyDelete
  5. Hey
    Lots of people are asking for the full source code.
    i think its a good idea to share it

    ReplyDelete
  6. how to scroll vertical bar graph externally using achartengine

    ReplyDelete

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...