Android Dynamic Chart

前端 未结 1 571
无人共我
无人共我 2021-02-03 13:55

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 r

相关标签:
1条回答
  • 2021-02-03 14:37

    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!

    0 讨论(0)
提交回复
热议问题