How to:‎ > ‎

Create Graphs

Achartengine is an libary for Android and provides various types of Graphcharts

You can download it under http://www.achartengine.org/index.html.  It is free and open source. So you
can adjust it to your project as you need.

How to set it up:
First you have to import the jar file into your project.

In the Activity you want to show your Graph:
Intent intent = GraphFactory.getTimeChartIntent(this, getDateDemoDataset(), getDemoRenderer(), null);
       startActivity(intent);

the two methods are calibrating the Graph for example like this:

the first method supposed to Return a XYMultipleSeriesRenderer. This object contains Graph attributes like color etc.

  private XYMultipleSeriesRenderer getDemoRenderer() {
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
    XYSeriesRenderer r = new XYSeriesRenderer();
        //first graph
    r.setColor(Color.BLUE);
    r.setPointStyle(PointStyle.SQUARE);
    r.setFillBelowLine(true);
    r.setFillBelowLineColor(Color.WHITE);
    r.setFillPoints(true);
    renderer.addSeriesRenderer(r);
        //second graph
    r = new XYSeriesRenderer();
    r.setPointStyle(PointStyle.CIRCLE);
    r.setColor(Color.GREEN);
    r.setFillPoints(true);
    renderer.addSeriesRenderer(r);
    renderer.setAxesColor(Color.DKGRAY);
    renderer.setLabelsColor(Color.LTGRAY);
    return renderer;
  }

the second method are supposed to return the actual data values to display
(this is not as important for you because its just generates random data which you would replace with your own,
but its good for testing purposes.)
 
private XYMultipleSeriesDataset getDateDemoDataset() {
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    final int nr = 10;
    long value = new Date().getTime() - 3 * TimeChart.DAY;
    Random r = new Random();
    boolean b=false;
    Calendar c = Calendar.getInstance();
    for (int i = 0; i < SERIES_NR; i++) { // for each graph
      TimeSeries series = new TimeSeries("Demo series " + (i + 1));
      for (int k = 0; k < nr && b; k++) {add the points
        series.add(new Date(value + k * TimeChart.DAY / 4), 20 + r.nextInt() % 100);
        Log.i("A",value + k * TimeChart.DAY / 4 + "  :::  "+  20 + r.nextInt() % 100 + "  :::  "+c.getTimeInMillis());
      }
      b=true;
      dataset.addSeries(series);
    }
    return dataset;
  }

Comments