AChartEngine实现Android实时曲线绘制

前两天由于项目需要做了一个基于Android的实时采集系统,需要对参数以实时曲线的形式显示,在网上找了找基于Android的图表工具以及源码,最终还是选取了AChartEngine来绘制,这个图表库用起来很是方便。下面将自己写的一个随机数的实时曲线和大家分享一下~~

本程序是基于Android2.2的

思路比较简单,即在一个MainActivity中点击按钮跳入另一个RtChartsActivity并显示实时曲线。

MainActivity中

public class MainActivity extends Activity {
	private Button button; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button=(Button)this.findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, RtChartsActivity.class);
				startActivity(intent);
			}
		});
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}

在RtChartsActivity中,首先初始化曲线,然后在Timer中定时updateChart()曲线

public class RtChartsActivity extends Activity {
	
    private Timer timer = new Timer();
    private GraphicalView chart;
    private TextView textview;
    private TimerTask task;
    private int addY = -1;
    private long addX;
    /**曲线数量*/
    private static final int SERIES_NR=1;
    private static final String TAG = "message";
    private TimeSeries series1;
    private XYMultipleSeriesDataset dataset1;
    private Handler handler;
    private Random random=new Random();
    /**时间数据*/
    Date[] xcache = new Date[20];
	/**数据*/
    int[] ycache = new int[20];
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rtchart);
		LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout);
        //生成图表
		chart = ChartFactory.getTimeChartView(this, getDateDemoDataset(), getDemoRenderer(), "hh:mm:ss");
		layout.addView(chart, new LayoutParams(LayoutParams.WRAP_CONTENT,380));
		//为TextView添加事件
		textview = (TextView)findViewById(R.id.myview);
		textview.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(RtChartsActivity.this, "ceshiview", 1).show();
				Intent intent = new Intent();
				intent.setClass(RtChartsActivity.this, MainActivity.class);
				startActivity(intent);
			}
		});
		handler = new Handler() {
        	@Override
        	public void handleMessage(Message msg) {
        		//刷新图表
        		updateChart();
        		super.handleMessage(msg);
        	}
        };
        task = new TimerTask() {
        	@Override
        	public void run() {
        		Message message = new Message();
        	    message.what = 200;
        	    handler.sendMessage(message);
        	}
        };
        timer.schedule(task, 2*1000,1000);
	}
	   private void updateChart() {
		    //设定长度为20
		    int length = series1.getItemCount();
		    if(length>=20) length = 20;
		    addY=random.nextInt()%10;
		    addX=new Date().getTime();
		    
		    //将前面的点放入缓存
			for (int i = 0; i < length; i++) {
				xcache[i] =  new Date((long)series1.getX(i));
				ycache[i] = (int) series1.getY(i);
			}
			series1.clear();
			series1.add(new Date(addX), addY);
			for (int k = 0; k < length; k++) {
	    		series1.add(xcache[k], ycache[k]);
	    	}
			//在数据集中添加新的点集
			dataset1.removeSeries(series1);
			dataset1.addSeries(series1);
			//曲线更新
			chart.invalidate();
	    }
	/**
	 * 设定如表样式
	 * @return
	 */
	   private XYMultipleSeriesRenderer getDemoRenderer() {
		    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		    renderer.setChartTitle("实时曲线");//标题
		    renderer.setChartTitleTextSize(20);
		    renderer.setXTitle("时间");    //x轴说明
		    renderer.setAxisTitleTextSize(16);
		    renderer.setAxesColor(Color.BLACK);
		    renderer.setLabelsTextSize(15);    //数轴刻度字体大小
		    renderer.setLabelsColor(Color.BLACK);
		    renderer.setLegendTextSize(15);    //曲线说明
		    renderer.setXLabelsColor(Color.BLACK);
		    renderer.setYLabelsColor(0,Color.BLACK);
		    renderer.setShowLegend(false);
		    renderer.setMargins(new int[] {20, 30, 100, 0});
		    XYSeriesRenderer r = new XYSeriesRenderer();
		    r.setColor(Color.BLUE);
		    r.setChartValuesTextSize(15);
		    r.setChartValuesSpacing(3);
		    r.setPointStyle(PointStyle.CIRCLE);
		    r.setFillBelowLine(true);
		    r.setFillBelowLineColor(Color.WHITE);
		    r.setFillPoints(true);
		    renderer.addSeriesRenderer(r);
		    renderer.setMarginsColor(Color.WHITE);
		    renderer.setPanEnabled(false,false);
		    renderer.setShowGrid(true);
		    renderer.setYAxisMax(50);
		    renderer.setYAxisMin(-30);
		    renderer.setInScroll(true);  //调整大小
		    return renderer;
		  }
	   /**
	    * 数据对象
	    * @return
	    */
	   private XYMultipleSeriesDataset getDateDemoDataset() {
		    dataset1 = new XYMultipleSeriesDataset();
		    final int nr = 10;
		    long value = new Date().getTime();
		    Random r = new Random();
		    for (int i = 0; i < SERIES_NR; i++) {
		      series1 = new TimeSeries("Demo series " + (i + 1));
		      for (int k = 0; k < nr; k++) {
		        series1.add(new Date(value+k*1000), 20 +r.nextInt() % 10);
		      }
		      dataset1.addSeries(series1);
		    }
		    Log.i(TAG, dataset1.toString());
		    return dataset1;
		  }
	    @Override
	    public void onDestroy() {
	    	//当结束程序时关掉Timer
	    	timer.cancel();
	    	super.onDestroy();
	    };
}

效果是这样的~~

AChartEngine实现Android实时曲线绘制_第1张图片

源程序可以在这里下载http://download.csdn.net/detail/best198706/5408151

你可能感兴趣的:(Android开发,java,android,java)