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事件,
只是模拟了几个数据。
这代码很水,望指教
推荐阅读
-
js加载之使用DOM方法动态加载Javascript文件_javascript技巧
-
ajax动态获取数据库中的数据方法
-
请问一个网站首页中的动态数据获取的流程大概分几步?
-
一个简单的动态加载js和css的jquery代码_jquery
-
Extjs入门之动态加载树代码_extjs
-
jQuery结合ajax实现动态加载文本内容_jquery
-
react通过axios请求数据,用一个bol来判断数据是否加载完成后,渲染在页面上,不用async await和定时器的更好方法
-
Vue 动态加载 Vuex
-
动态分区表--严格模式处理===&amp;amp;&amp;amp;===导出数据到本地或HDFS
-
动态创建form提交数据