achartengine 实现平行线 动态数据 x轴动态移动

移动开发 Android
achartengine做平行线的时候经常会遇到一下几个问题,然而本篇文章就是在stackoverflow中找到答案解决问题。

achartengine做平行线的时候经常会遇到:

 

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1

 

at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)

 

at org.achartengine.renderer.DefaultRenderer.getSeriesRendererAt(DefaultRenderer.java:189)

 

at org.achartengine.chart.XYChart.draw(XYChart.java:240)

 

at org.achartengine.GraphicalView.onDraw(GraphicalView.java:168)

 

***在stackoverflow找到了答案:1个数据就要用一个XYSeriesRenderer 所以两条平行线就要用2个XYSeriesRenderer

***效果图:

 

  1.  1 package com.example.achartparallel; 
  2.   2  
  3.   3  
  4.   4 import com.example.achartparallel.XYRenderer; 
  5.   5  
  6.   6  
  7.   7 import java.math.BigDecimal; 
  8.   8 import java.text.DecimalFormat; 
  9.   9  
  10.  10 import org.achartengine.ChartFactory; 
  11.  11 import org.achartengine.GraphicalView; 
  12.  12 import org.achartengine.chart.PointStyle; 
  13.  13 import org.achartengine.model.XYMultipleSeriesDataset; 
  14.  14 import org.achartengine.model.XYSeries; 
  15.  15 import org.achartengine.renderer.XYMultipleSeriesRenderer; 
  16.  16 import org.achartengine.renderer.XYSeriesRenderer; 
  17.  17  
  18.  18  
  19.  19 import android.app.Activity; 
  20.  20 import android.app.AlertDialog; 
  21.  21 import android.content.Context; 
  22.  22 import android.content.DialogInterface; 
  23.  23 import android.content.Intent; 
  24.  24 import android.graphics.Color; 
  25.  25 import android.os.Bundle; 
  26.  26 import android.os.Handler; 
  27.  27 import android.os.Message; 
  28.  28 import android.view.LayoutInflater; 
  29.  29 import android.view.Menu; 
  30.  30 import android.view.MenuItem; 
  31.  31 import android.view.View; 
  32.  32 import android.view.View.OnClickListener; 
  33.  33 import android.view.ViewGroup.LayoutParams; 
  34.  34 import android.view.Window; 
  35.  35 import android.view.WindowManager; 
  36.  36 import android.widget.Button; 
  37.  37 import android.widget.EditText; 
  38.  38 import android.widget.LinearLayout; 
  39.  39 import android.widget.TextView; 
  40.  40 import android.widget.Toast; 
  41.  41  
  42.  42 public class MainActivity extends Activity { 
  43.  43  
  44.  44      
  45.  45     private XYSeries xyseries;//数据 
  46.  46     private XYSeries xyseries_up;//上平行线 
  47.  47     private XYSeries xyseries_down;//下平行线 
  48.  48     private XYMultipleSeriesDataset dataset; 
  49.  49     private GraphicalView chartview; 
  50.  50     private XYMultipleSeriesRenderer renderer; 
  51.  51     private XYSeriesRenderer datarenderer; 
  52.  52     private XYSeriesRenderer datarenderer_up; 
  53.  53     private XYSeriesRenderer datarenderer_down; 
  54.  54     private Context context; 
  55.  55     
  56.  56     int i=0
  57.  57     Thread t; 
  58.  58     private int addX ; 
  59.  59     private double addY ; 
  60.  60     int X_MAX=10;//x轴***值 
  61.  61      
  62.  62     //标准和阈值 
  63.  63     double value=10 ;//平行线中间值 
  64.  64     double bound=0.5;//上下限 可以自己随意设定 
  65.  65     protected void onCreate(Bundle savedInstanceState) { 
  66.  66         super.onCreate(savedInstanceState); 
  67.  67         requestWindowFeature(Window.FEATURE_NO_TITLE); //无title 
  68.  68         setContentView(R.layout.activity_main); 
  69.  69         LinearLayout chart_layout1 = (LinearLayout)findViewById(R.id.chart_layout1); 
  70.  70         chart_layout1.setBackgroundColor(Color.BLACK); 
  71.  71          
  72.  72          
  73.  73          //No.1 设定大渲染器的属性 太多了 所以写了个类 
  74.  74         renderer=new XYRenderer("零件""X轴(单位:id)""Y轴(mm)"
  75.  75                 0,X_MAX, -11, Color.GRAY, Color.LTGRAY, 
  76.  76                 16,16,10,15,20,10,true); 
  77.  77         dataset=new XYMultipleSeriesDataset(); 
  78.  78         datarenderer=new XYSeriesRenderer(); 
  79.  79         datarenderer.setDisplayChartValues(true); 
  80.  80         xyseries=new XYSeries("零件"); 
  81.  81         //2 
  82.  82         xyseries.add(00);//先输入一个数据让它绘出renderer 
  83.  83         //3 
  84.  84         dataset.addSeries(0,xyseries); 
  85.  85         datarenderer.setColor(Color.GREEN); 
  86.  86         datarenderer.setPointStyle(PointStyle.POINT); 
  87.  87         //4 
  88.  88         renderer.addSeriesRenderer(datarenderer); 
  89.  89          
  90.  90         //5 
  91.  91         context = getApplicationContext(); 
  92.  92         chartview = ChartFactory.getLineChartView(context, dataset, renderer); 
  93.  93         chart_layout1.addView(chartview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
  94.  94          
  95.  95         //设置按钮 用来记录尺寸的标准(value)和阈值(bound),然后再主界面显示出红线 
  96.  96          
  97.  97                         //绘出两条红线 分别用新的XYSeriesRenderer要不然会报错 
  98.  98                         xyseries_up=new XYSeries("阈值1"); 
  99.  99                         xyseries_down=new XYSeries("阈值2"); 
  100. 100                         xyseries_up.add(0 , value+bound); 
  101. 101                         xyseries_up.add(X_MAX*2, value+bound); 
  102. 102                         xyseries_down.add(0, value-bound); 
  103. 103                         xyseries_down.add(X_MAX*2,value-bound); 
  104. 104                         dataset.addSeries(1,xyseries_up); 
  105. 105                         dataset.addSeries(2,xyseries_down); 
  106. 106                         datarenderer_up = new XYSeriesRenderer(); 
  107. 107                         datarenderer_down = new XYSeriesRenderer(); 
  108. 108                         datarenderer_up.setColor(Color.RED); 
  109. 109                         datarenderer_down=datarenderer_up; 
  110. 110                         renderer.addSeriesRenderer(datarenderer_up); 
  111. 111                         renderer.addSeriesRenderer(datarenderer_down); 
  112. 112                         renderer.setYAxisMin(value-(4*bound));// Y最小值 
  113. 113                         renderer.setYAxisMax(value+(4*bound));// Y最小值 
  114. 114                         //设置chart的视图范围  参数//1x->start 2max 3y->start 4max  
  115. 115                         renderer.setRange(new double[]{0d, (double)X_MAX, value-(4*bound), value+(4*bound)}); 
  116. 116                       //  chartview.repaint(); 
  117. 117                
  118. 118                         
  119. 119  
  120. 120  
  121. 121          
  122. 122         
  123. 123          
  124. 124      
  125. 125          t=new Thread (r1);  
  126. 126              t.start();  
  127. 127          
  128. 128     }//oncreate end 
  129. 129  
  130. 130     //handler处理UI更新 
  131. 131     Handler myhandler=new Handler() 
  132. 132     { 
  133. 133  
  134. 134         @Override 
  135. 135         public void handleMessage(Message msg) { 
  136. 136             super.handleMessage(msg); 
  137. 135        //可以处理些数据 
  138. 142              chartview.repaint(); 
  139. 143         } 
  140. 144  
  141. 145          
  142. 146     }; 
  143. 147      
  144. 148  
  145. 149         Runnable r1=new Runnable() { 
  146. 150             BigDecimal b; 
  147. 151  
  148. 152         public void run(){ 
  149. 153             try { 
  150. 154                 while(true
  151. 155                 { 
  152. 156                 i++; 
  153. 157                 Thread.sleep(500); 
  154. 158                 addX = i; 
  155. 159                  //设置好下一个需要增加的节点 随机产生 在value+-bound左右 
  156. 160                 b =new BigDecimal(((Math.random())*bound*4)-(2*bound));  //转换 
  157. 161                 addY=b.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); 
  158. 162                 //double+double再转一次 
  159. 163                 b =new BigDecimal(addY+value); //保留三位小数 四舍五入 
  160. 164                 addY=b.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); 
  161. 165                      
  162. 166                 //传给hanlder 
  163. 167                  Message message=new Message();   
  164. 168                  Bundle bundle=new Bundle();   
  165. 169                  bundle.putString("part_id1""编号:"+addX);   
  166. 170                  bundle.putString("part_size1""尺寸:"+addY);  
  167. 171                  message.setData(bundle);//bundle传值,耗时,效率低   
  168. 172                  myhandler.sendMessage(message);//发送message信息   
  169. 173                  message.what=1;//标志是哪个线程传数据   
  170. 174                   
  171. 175                   
  172. 176                  //步骤不能变 1先清除数据 2添加源数据 3添加数据 
  173. 177                  dataset.removeSeries(xyseries); 
  174. 178                  xyseries.add(addX, addY); 
  175. 179                  dataset.addSeries(0,xyseries); 
  176. 180                  //chartview.repaint(); 
  177. 181                  chartview.postInvalidate(); 
  178. 182                   
  179. 183                   
  180. 184                  if(i*2>X_MAX)//延长X_MAX造成右移效果 
  181. 185                  { 
  182. 186                      X_MAX*=2;//按2倍速度延长 可以设置成speed 
  183. 187                      renderer.setXAxisMax(X_MAX);// 设置X***值 
  184. 188                      dataset.removeSeries(xyseries_up); 
  185. 189                      dataset.removeSeries(xyseries_down); 
  186. 190                      xyseries_up.add(X_MAX/2 , value+bound); 
  187. 191                      xyseries_up.add(X_MAX, value+bound); 
  188. 192                      xyseries_down.add(X_MAX/2, value-bound); 
  189. 193                      xyseries_down.add(X_MAX,value-bound); 
  190. 194                      dataset.addSeries(1,xyseries_up); 
  191. 195                      dataset.addSeries(2,xyseries_down); 
  192. 196 //                     datarenderer_up = new XYSeriesRenderer(); 
  193. 197 //                     datarenderer_down = new XYSeriesRenderer(); 
  194. 198 //                     datarenderer_up.setColor(Color.RED); 
  195. 199 //                     datarenderer_down=datarenderer_up; 
  196. 200                      renderer.addSeriesRenderer(datarenderer_up); 
  197. 201                      renderer.addSeriesRenderer(datarenderer_down); 
  198. 202                  } 
  199. 203                   
  200. 204                   
  201. 205                 } 
  202. 206             } catch (InterruptedException e) { 
  203. 207                 e.printStackTrace(); 
  204. 208             } 
  205. 209            
  206. 210          } 
  207. 211         }; 
  208. 212         @Override 
  209. 213         protected void onDestroy() { 
  210. 214             t.interrupt(); 
  211. 215             System.out.println("destory"); 
  212. 216             super.onDestroy(); 
  213. 217         } 
  214. 218       
  215. 219      
  216. 220 } 

 

  1.  1 package com.example.achartparallel; 
  2.  2  
  3.  3 import org.achartengine.renderer.XYMultipleSeriesRenderer; 
  4.  4  
  5.  5 public class XYRenderer extends XYMultipleSeriesRenderer  
  6.  6 { 
  7.  7     //图表样式构造器 
  8.  8     public XYRenderer() 
  9.  9     { 
  10. 10          
  11. 11     } 
  12. 12     public XYRenderer( 
  13. 13             String title, String xTitle, String yTitle, double xMin, 
  14. 14             double xMax, double yMin, double yMax, int axesColor, 
  15. 15             int labelsColor,int XTitleTextSize,int ChartTitleTextSize, 
  16. 16             int LabelsTextSize,int LegendTextSize,int Xshow,int Yshow, 
  17. 17             boolean ShowGrid)  
  18. 18     { 
  19. 19         this.setChartTitle(title); 
  20. 20         this.setXTitle(xTitle);// X轴标题 
  21. 21         this.setYTitle(yTitle);// Y轴标题 
  22. 22         this.setXAxisMin(xMin);// X最小值 
  23. 23         this.setXAxisMax(xMax);// X***值 
  24. 24         this.setYAxisMin(yMin);// Y最小值 
  25. 25         this.setYAxisMax(yMax);// Y最小值 
  26. 26         this.setAxesColor(axesColor);// X轴颜色 
  27. 27         this.setLabelsColor(labelsColor);// Y轴颜色 
  28. 28         this.setAxisTitleTextSize(XTitleTextSize); // 坐标轴标题字体大小:16 
  29. 29         this.setChartTitleTextSize(ChartTitleTextSize); // 图表标题字体大小:20 
  30. 30         this.setLabelsTextSize(LabelsTextSize); // 轴标签字体大小:10 
  31. 31         this.setLegendTextSize(LegendTextSize); // 图例字体大小:15 
  32. 32         this.setXLabels(Xshow);//设置X轴显示的刻度标签的个数 
  33. 33         this.setYLabels(Yshow);//设置Y轴显示的刻度标签的个数 
  34. 34         this.setShowGrid(ShowGrid); // 设置网格显示 
  35. 35      
  36. 36          
  37. 37     } 
  38. 38  
  39. 39 }
  1.  1  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.  2    xmlns:tools="http://schemas.android.com/tools" 
  3.  3     android:layout_width="match_parent" 
  4.  4     android:layout_height="match_parent" 
  5.  5     android:paddingBottom="@dimen/activity_vertical_margin" 
  6.  6     android:paddingLeft="@dimen/activity_horizontal_margin" 
  7.  7     android:paddingRight="@dimen/activity_horizontal_margin" 
  8.  8     android:paddingTop="@dimen/activity_vertical_margin" 
  9.  9     tools:context="com.example.achartparallel.MainActivity" 
  10. 10      android:id="@+id/chart_layout1" > 
  11. 11  
  12. 12      
  13. 13  
  14. 14 </LinearLayout> 

工程下载:http://download.csdn.net/detail/u011991258/7723011

本文链接:http://www.cnblogs.com/sweetculiji/archive/2014/08/06/3895622.html

责任编辑:chenqingxiang 来源: cnblogs
相关推荐

2023-04-04 08:01:00

java坐标距离

2009-01-03 14:54:40

ibmdwXML

2022-07-08 10:11:04

探查数据治理数据质量

2017-05-11 21:30:01

Android动态代理ServiceHook

2015-10-28 10:37:13

动态数据屏蔽Dynamic datSQL Server

2011-04-07 14:04:28

SQL动态交叉表

2011-01-26 08:59:11

jQueryjavascriptweb

2020-06-19 12:59:33

动态脚本Java

2015-07-23 15:15:06

动态弹出

2024-07-09 08:48:38

2018-06-04 16:20:56

Linux动态路由Quagga

2009-07-22 13:04:41

iBATIS动态查询

2009-09-14 19:14:51

LINQ动态查询

2024-08-06 14:29:37

2018-01-26 07:53:46

数据脱敏数据安全信息安全

2011-05-31 11:05:16

ListView 数据

2019-09-10 13:58:57

动态路由路由器网络

2017-01-19 17:37:00

iOS移动端架构设计

2015-09-28 15:59:00

Java动态代理机制

2009-02-27 10:04:25

动态基础架构NEDC
点赞
收藏

51CTO技术栈公众号