欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

achartengine动态加载数据

程序员文章站 2022-07-15 11:50:58
...

现在项目有个需求就是利用achartengine动态的加载数据,当拖动图表时会下载几条数据,拖动的轨迹越长,加载的数据就越多。网上查了些资料,找不到这样的例子。于是自己写了个package com.cng;

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

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

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;

public class DynLoadActivity extends Activity {
    /** Called when the activity is first created. */
	 private  XYMultipleSeriesDataset mDataset=new XYMultipleSeriesDataset();;
	 private  XYSeries mAtentionSeries=new XYSeries("哈哈哈");
	 //全局变量  , 记录条数
	 private int total=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	 setContentView(R.layout.main);
    	String[] titles = new String[] { "Crete"};
        int[] colors = new int[] { Color.BLUE};
        PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE};
        XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
        int length = renderer.getSeriesRendererCount(); 
        for (int i = 0; i < length; i++) {
          ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
        }
        setChartSettings(renderer, "Average temperature", "Month", "Temperature", 0.5, 12.5, -10, 2000,
            Color.LTGRAY, Color.LTGRAY);
//        renderer.setXAxisMin(0);
//        renderer.setXAxisMax(100);
        renderer.setXLabels(12);
        renderer.setYLabels(10); 
        renderer.setShowGrid(true); 
        renderer.setXLabelsAlign(Align.RIGHT);
        renderer.setYLabelsAlign(Align.RIGHT); 
        renderer.setZoomButtonsVisible(true);
        renderer.setPanLimits(new double[] { -10, 2000, -10, 2000 });
        renderer.setZoomLimits(new double[] { -10, 2000, -10, 2000 });
        LinearLayout linearLayout=(LinearLayout)findViewById(R.id.linear);
        total++;
        mAtentionSeries.add(1, 11);
        
        mDataset.addSeries(mAtentionSeries);
        View view = ChartFactory.getLineChartView(this,mDataset ,
            renderer); 
        linearLayout.addView(view);
        view.setOnTouchListener(new OnTouchListener() { 
        	int x=0;
    		int x2=0;
        	public boolean onTouch(View v, MotionEvent event) { 
        		
                switch (event.getAction()) { 

                  case MotionEvent.ACTION_DOWN: {
                        //按下时触发
                	  //判断是否向做移动
                	  if(x-x2>0)
                	  {
                		  //获取要从服务器获取的条数
                		  int length=(x-x2)/20;
                		  //添加数据至图标中
                		  for(int i=0;i<length;i++)
                		  {
                			  /** toatal可当做参数传至服务器上来获取数据
                			   *  因为获取数据时必须获取的是少量的中间的连续数据
                			   *  可从for循环外面将参数total,total+length传入获取数据库中的第total到total+length数据
                			   */
                			  total++;
                			  Log.e(total+"", total+"");
                			  mAtentionSeries.add(total, total);
                		  }
                	  }
                	  //将触摸式按下的横坐标记住
                	  x=(int) event.getX();
                  } 

                  case MotionEvent.ACTION_MOVE: {
                         //移动时触发
                	  //将触摸时手指离开时的横坐标记录
                	  x2=(int) event.getX();
                  } 

                  case MotionEvent.ACTION_UP: {
                          //触摸后触发 
                  } 
                }
				return false;
            }
        });

    }
    
    
    private XYMultipleSeriesRenderer buildRenderer(int[] colors,
			PointStyle[] styles)
	{
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		// 控制横纵轴的属性字大小
		renderer.setAxisTitleTextSize(15);
		// 控制横纵轴的值大小
		renderer.setChartTitleTextSize(20);
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		renderer.setPointSize(5f);
		renderer.setMargins(new int[]
		{ 20, 30, 15, 0 });
		int length = colors.length;
		for (int i = 0; i < length; i++)
		{
			XYSeriesRenderer r = new XYSeriesRenderer();
			r.setColor(colors[i]);
			r.setPointStyle(styles[i]);
			renderer.addSeriesRenderer(r);
		}
		return renderer;
	}

	private void setChartSettings(XYMultipleSeriesRenderer renderer,
			String title, String xTitle, String yTitle, double xMin,
			double xMax, double yMin, double yMax, int axesColor,
			int labelsColor)
	{
		renderer.setChartTitle(title);
		renderer.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}

	private XYMultipleSeriesDataset buildDataset(String[] titles,
			List<double[]> xValues, List<double[]> yValues)
	{
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		int length = titles.length;
		for (int i = 0; i < length; i++)
		{
			XYSeries series = new XYSeries(titles[i]);
			double[] xV = xValues.get(i);
			double[] yV = yValues.get(i);
			int seriesLength = xV.length;
			for (int k = 0; k < seriesLength; k++)
			{
				series.add(xV[k], yV[k]);
			}
			dataset.addSeries(series);
		}
		return dataset;
	}
}
 

其实原理很简单  就是捕获ontouch事件的ACTION_DOWN ,ACTION_UP事件,记下他们的x坐标。在下一次ACTION_DOWN 时出发加载数据事件,我这里没有调用访问webservice事件,

只是模拟了几个数据。

这代码很水,望指教