Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Dynamic Chart

Hi Android developers, Currently i am working on one Financial based chart application, and i draw the chart with the help of jjoe64 blog. but now i am going to implement real time chart. for example chart is draw automatically in the surface each and every second or a min once made changes in server side. can any one implement these kind of task. please help me regarding this.

like image 234
Hariram Avatar asked Jan 16 '12 10:01

Hariram


People also ask

What is a dynamic bar chart?

A dynamic chart is one that shows change over time like the one we have right in front of us. So, you can see that we've got a progression of change happening throughout this period here.


1 Answers

Ok, I strongly recommend to use AChartEngine. It is free, lightweight and supports many types of charts pie, graph etc. As about to dynamically update the chart i achieved this by adding values in a separate thread.Here is my example,it is nothing fancy:

package my.pack;

import java.util.Date;
import java.util.Random;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

 public class ChartsActivity extends Activity {

private static Random random = new Random();

private static TimeSeries timeSeries;
private static XYMultipleSeriesDataset dataset;
private static XYMultipleSeriesRenderer renderer;
private static XYSeriesRenderer rendererSeries;
private static GraphicalView view;
private static Thread mThread;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     

    dataset = new XYMultipleSeriesDataset();

    renderer = new XYMultipleSeriesRenderer();
    renderer.setAxesColor(Color.BLUE);
    renderer.setAxisTitleTextSize(16);
    renderer.setChartTitle("Time");
    renderer.setChartTitleTextSize(15);
    renderer.setFitLegend(true);
    renderer.setGridColor(Color.LTGRAY);
    renderer.setPanEnabled(true, true);
    renderer.setPointSize(10);
    renderer.setXTitle("Time");
    renderer.setYTitle("Number");
    renderer.setMargins( new int []{20, 30, 15, 0});
    renderer.setZoomButtonsVisible(true);
    renderer.setBarSpacing(10);
    renderer.setShowGrid(true);


    rendererSeries = new XYSeriesRenderer();
    rendererSeries.setColor(Color.RED);
    renderer.addSeriesRenderer(rendererSeries);
    rendererSeries.setFillPoints(true);
    rendererSeries.setPointStyle(PointStyle.CIRCLE);

    timeSeries = new TimeSeries("Random");
    mThread = new Thread(){
        public void run(){
            while(true){
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
                timeSeries.add(new Date(), random.nextInt(10));
                view.repaint();
            }
        }
    };
    mThread.start();
}

@Override
protected void onStart() {
    super.onStart();
    dataset.addSeries(timeSeries);
    view = ChartFactory.getTimeChartView(this, dataset, renderer, "Test");
    view.refreshDrawableState();
    view.repaint();
    setContentView(view);    
}

}

Add your own values to the thread.You may want to take a look on how to use the AChartEngine library. Happy coding!

like image 166
Charles Avatar answered Oct 26 '22 13:10

Charles