achartengine刷新数据

achartengine工具比较强大,偶在闲余时间玩了下,想通过achartengine来模拟股票线性图,于是就针对achartengine中线性图尝试效果,achartengine中包含了很多图表,希望同学们可以去研究下,偶只会点毛片,不说那么多了,进入正题。

偶上传了应用,请到该地址下载:http://download.csdn.net/detail/a1031359915/8990855


开始解析代码:

String[] titles = new String[] { "Crete", "Corfu", "Thassos", "Skiathos" };
	List<double[]> x = new ArrayList<double[]>();
    List<double[]> values = new ArrayList<double[]>();

其中titles为类名,即线条表示的类;x表示x轴坐标点,values为y轴坐标点

数据准备好了就来看看刷新方法:

/*************移除视图方法*************
        layoutChart.removeAllViews();
	aChart.cleardata();
			
	aChart = new AverageTemperatureChart(titles, x, values);
	view = aChart.execute(this);
	layoutChart.addView(view);
		
*/
		
/*************刷新数据,不移除视图(推荐)***************/
	aChart.refresh(titles, x, values, view);
提供了两个刷新方法:第一个是移除视图,再重新初始化,添加视图;第二个是通过将图标上的数据清楚,重新添加数据(数据量大的话,也可能效率不高),图标移动后的位置不会改变,只改变数据,推荐使用第二种。

相关代码如下:

package com.example.chartinuse;

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

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
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.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.view.View;

/**
 * Average temperature demo chart.
 */
public class AverageTemperatureChart extends AbstractDemoChart {
	
	private String[] titles;
	
	private List<double[]> x;
	
	private List<double[]> values;
	
	private XYMultipleSeriesDataset dataset;
	

	public AverageTemperatureChart(String [] titles, List<double[]> x, List<double[]> values){
		this.titles = titles;
		this.x = x;
		this.values = values;
	}
  /**
   * Returns the chart name.
   * 
   * @return the chart name
   */
  public String getName() {
    return "Average temperature";
  }

  /**
   * Returns the chart description.
   * 
   * @return the chart description
   */
  public String getDesc() {
    return "The average temperature in 4 Greek islands (line chart)";
  }

  /**
   * Executes the chart demo.
   * 
   * @param context the context
   * @return the built intent
   */
  public GraphicalView execute(Context context) {
    int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.CYAN, Color.YELLOW };
    PointStyle[] styles = new PointStyle[] { PointStyle.POINT, PointStyle.POINT, PointStyle.POINT, PointStyle.POINT };
    XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
    int length = renderer.getSeriesRendererCount();
    for (int i = 0; i < length; i++) {
      ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
      ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setLineWidth(3f);
    }
    
    setChartSettings(renderer, "Average temperature", "Month", "Temperature", 0.5, 12.5, -10, 40,
        Color.LTGRAY, Color.LTGRAY);
    renderer.setXLabels(12);
    renderer.setYLabels(10);
    renderer.setShowGrid(true);
//    renderer.setMargins(new int[]{40,50,20,30});// 设置边距,上左下右
//    renderer.setLegendHeight(115);// 设置图例的高度
    renderer.setXLabelsAlign(Align.RIGHT);
    renderer.setYLabelsAlign(Align.RIGHT);
    renderer.setYLabelsPadding(10);
    renderer.setZoomButtonsVisible(true);
//    renderer.setZoomEnabled(false, false);// 设置缩放效果
    renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
    renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
//    renderer.setApplyBackgroundColor(true);//设置内部背景
//    renderer.setBackgroundColor(Color.WHITE);// 设置内部背景
//    renderer.setMarginsColor(Color.WHITE);// 设置外部背景
    dataset = buildDataset(titles, x, values);
    XYSeries series = dataset.getSeriesAt(0);
    series.addAnnotation("Vacation", 6, 30);
    GraphicalView view = ChartFactory.getCubeLineChartView(context, dataset, renderer, 0.3f);
    return view;
  }

  public void refresh(String[] titles, List<double[]> xValues, List<double[]> yValues, GraphicalView view){
	 cleardata();
	 for (int i = 0; i < xValues.size(); 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);
	      view.repaint();
	 }
//	 view.repaint();
  }
  
  public void cleardata(){
	  while(dataset.getSeries().length > 0){
		  XYSeries series= dataset.getSeries()[0];
		  dataset.removeSeries(series);
		  series.clear();
	  }
  }
}



你可能感兴趣的:(achartengine刷新数据)