android下使用RChart画曲线图动态刷新

 

画曲线图的activity:

Java代码
  1. import java.io.IOException;   
  2. import java.io.InputStream;   
  3.   
  4. import android.app.Activity;   
  5. import android.os.Bundle;   
  6. import android.util.Log;   
  7.   
  8. import com.java4less.rchart.Chart;   
  9. import com.java4less.rchart.ChartAdapter;   
  10. import com.java4less.rchart.ChartListener;   
  11. import com.java4less.rchart.ChartLoader;   
  12. import com.java4less.rchart.android.ChartPanel;   
  13. import com.java4less.rchart.gc.android.ChartAndroidImage;   
  14.   
  15. public class ShowChart extends Activity {   
  16.     
  17.  private String data = "";   
  18.  private int cishu=0;   
  19.  private static final int xnum=9;    
  20.  ChartLoader cha=new ChartLoader();   
  21.  ChartPanel chartPanel;   
  22.     
  23.     public void onCreate(Bundle icicle) {   
  24.       cha=new ChartLoader();   
  25.       // this is to be able to load the images   
  26.          ChartAndroidImage.assets=getAssets();       
  27.          String chartfile=this.getIntent().getStringExtra("com.java4less.rchart.samples.file");   
  28.          super.onCreate(icicle);   
  29.          chartPanel = new ChartPanel(this);   
  30.          try {   
  31.     chartPanel.setChart(loadFromAsset(chartfile));   
  32.    } catch (Exception e) {   
  33.     Log.e(ChartPanel.TAG,"Could not lod chart from file.",e);   
  34.    }         
  35.          setContentView(chartPanel);           
  36.      }   
  37.        
  38.     protected void onDestroy() {   
  39.          
  40.       if (chartPanel!=null)    
  41.         if (chartPanel.getChart()!=null) chartPanel.getChart().stopUpdater();   
  42.          
  43.       super.onDestroy();   
  44.      }    
  45.     
  46.  // create chart listener for the drill down chart曲线图的刷新方法   
  47.  ChartAdapter chartAdapterRealtime=new ChartAdapter() {   
  48.   public void chartEvent(Chart c,int type) {   
  49.    // load subchart   
  50.    if (type==ChartListener.EVENT_BEFORE_UPDATE) createRealtimedata(cha);   
  51.    //设置刷新时间!!   
  52.    c.msecs=1000;   
  53.   }   
  54.  };    
  55.                public Chart loadFromAsset(String name) throws IOException {   
  56.             
  57.             InputStream is = getAssets().open(name);                  
  58.             cha.loadFromFile(is, true);        
  59.              Log.i(ChartPanel.TAG,"Building chart ...");   
  60.             Chart c=cha.build(false,false);    
  61.              //c.msecs=5000;在此处尝试设置刷新时间,失败!   
  62.             c.addChartListener(chartAdapterRealtime);   
  63.             c.startUpdater();    
  64.             Log.i(ChartPanel.TAG,"Build ok");        
  65.             return c;        
  66.       
  67.         }   
  68.        
  69.      //刷新时调用该方法   
  70.      private void createRealtimedata(ChartLoader loader) {   
  71.    // create chart   
  72.   
  73.    // set initial value of the X Axis based on the time   
  74.    java.util.Date now=java.util.Calendar.getInstance().getTime();   
  75.    java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("dd/MM/yy hh:mm:ss");   
  76.   
  77.    loader.setParameter("XAXIS_INITIAL_DATE",formatter.format(now));   
  78.   
  79.    //右边为最新数据   
  80.    java.util.Random r=new java.util.Random();    
  81.    if(cishu<xnum)   
  82.    {    
  83.     if (data.length()>0) data=data+ "|" ;   
  84.     data=data+r.nextInt(50);   
  85.     cishu++;   
  86.    }   
  87.    else  
  88.    {   
  89.     data=data.substring(data.indexOf("|")+1)+"|"+r.nextInt(50);   
  90.    }   
  91.    loader.setParameter("SERIE_DATA_1",data);   
  92.   
  93.     }       
  94.         
  95. }   
  96.   

调用上面activity的类:

Java代码
  1. import java.util.ArrayList;   
  2. import java.util.HashMap;   
  3. import java.util.List;   
  4. import java.util.Map;   
  5. import android.app.ListActivity;   
  6. import android.content.Intent;   
  7. import android.os.Bundle;   
  8. import android.view.View;   
  9. import android.widget.ListView;   
  10. import android.widget.SimpleAdapter;   
  11.   
  12. import com.util.ShowChart;   
  13.   
  14. public class sensor extends ListActivity {   
  15. //private static final String TAG = "J4LChartDemos";   
  16.     
  17.  String[] examples={"combined2Positions.txt","patternFilling.txt","linechart.txt","barChart3D.txt","targetZones.txt","MultiCharts.txt","radarChart.txt","scatterChart.txt","areaChart.txt","areaCurves.txt","axisOnTop.txt","barChart.txt","barImages.txt","bubbleChart.txt","candleStick.txt",   
  18.    "combinedChart.txt","event.txt","gauge.txt","linechart3D.txt","linechartClipping.txt","linechartNULLS.txt",   
  19.    "pieChart2D.txt","piechart3D.txt","realtime.txt","stackedBar.txt",   
  20.    "StackedMultiAxis.txt","MultiAxis.txt"};     
  21.     
  22.  String[] labels={"Combined","Curves and notes","Linechart","Barchart 3D (update)","Target zones","Multicharts","Radarchart","Scatterchart","Areachart","Curves","Axis on Top","Barchart","Bars and Images","BubbleChart","Candlestick",   
  23.    "Combined 2","Events","Gauge","Linechart 3D","Linechart & Clipping","Linechart and nulls",   
  24.    "Piechart 2D","Piechart 3D","Realtime","Stacked bars",   
  25.    "Stacked Multiaxis","MultiAxis"};   
  26.   
  27.     
  28.     /** Called when the activity is first created. */  
  29.     @Override  
  30.     public void onCreate(Bundle icicle) {   
  31.   
  32.         super.onCreate(icicle);      
  33.         List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();        
  34.         for (int i=0; i<examples.length;i++)   
  35.             addItem(list, labels[i], activityIntent(examples[i]));    
  36.         setListAdapter(new SimpleAdapter(this,list,android.R.layout.simple_list_item_1,new String[] { "title" },new int[] { android.R.id.text1 }));       
  37.        }   
  38.   
  39.     protected Intent activityIntent(String fileName) {   
  40.         Intent result = new Intent();   
  41.         result.setClass(this.getApplication(),ShowChart.class);   
  42.         result.putExtra("com.java4less.rchart.samples.file", fileName);   
  43.         return result;   
  44.     }   
  45.   
  46.     protected void addItem(List<Map<String,Object>> data, String name, Intent intent) {   
  47.         Map<String, Object> temp = new HashMap<String, Object>();   
  48.         temp.put("title", name);   
  49.         temp.put("intent", intent);   
  50.         data.add(temp);   
  51.     }   
  52.     protected void onListItemClick(ListView l, View v, int position, long id) {   
  53.         Map map = (Map) l.getItemAtPosition(position);   
  54.   
  55.         Intent intent = (Intent) map.get("intent");   
  56.         startActivity(intent);   
  57.     }       
  58.        
  59. }   
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值