最近正在android上使用RChart画曲线图,由于数据要动态更新,所以要设置更新的时间间隔。摸索了很长时间,今天终于可以了。引用的assets都是RChart demo例子中的东西,我只是修改了刷新时间。RChart demo在我的资源里,关于动态显示数据,大家要记得在Listener里面添加时间的语句,例如:
ChartAndroidImage.assets=getAssets();
chartPanel = new ChartPanel(view[i].getContext());
Chart chart=loadFromAsset("温度");
chart.addChartListener(new ChartAdapter() {
@Override
public void chartEvent(Chart c, int type) {
// TODO Auto-generated method stub
super.chartEvent(c, type);
c.msecs=2000;
if (type==ChartListener.EVENT_BEFORE_UPDATE)
{
createRealtimedata(cha,"8","temperature");
}
if(type==ChartListener.EVENT_AFTER_UPDATE)
{
view[kk].postInvalidate();//我的这个rchart是在pupopwindow中显示的,所以要调用postInvalidate
}
}
});
chart.startUpdater();
chartPanel.setChart(chart);
private void createRealtimedata(ChartLoader loader,String id,String type) {
Sensor sensor=WebserviceMethod.getSensorDataById(id);
if(sensor!=null)
{
double zuigao=Double.parseDouble(sensor.getValueByType(type))+2;
double pingjun=Double.parseDouble(sensor.getValueByType(type))-2;
//右边为最新数据
maxData=maxData.substring(maxData.indexOf("|")+1)+"|"+zuigao;
System.out.println(maxData);
loader.setParameter("SERIE_DATA_1",maxData);
avgData=avgData.substring(avgData.indexOf("|")+1)+"|"+pingjun;
System.out.println(avgData);
loader.setParameter("SERIE_DATA_2",avgData);
}
loadFromAsset:初始化曲线图
public Chart loadFromAsset(String type) throws IOException {
System.out.println("Creating loader ...");
cha.setParameter("WIDTH","300");
cha.setParameter("HEIGHT","250");
cha.setParameter("TITLECHART","节点"+type+"显示");
cha.setParameter("TITLE_FONT","Serif|BOLD|12");
cha.setParameter("YLABEL","摄氏度");
cha.setParameter("YSCALE_MIN","0");
cha.setParameter("TICK_INTERVALY","2");//Y轴两个小trick之间的距离,
cha.setParameter("YAXIS_TICKATBASE","TRUE");//Y轴trick从0开始
cha.setParameter("XAXIS_GRID","0.2|0xffffff|DASHED");
cha.setParameter("YAXIS_GRID","0.2|0xffffff|DASHED");
cha.setParameter("GRIDY","TRUE");//仅在大trick处有网格
cha.setParameter("BIG_TICK_INTERVALX","1");//X轴两个大trick之间的小trick的数目
cha.setParameter("XAXIS_LABELS","1|2|3|4|5|6");
cha.setParameter("XAXIS_START_WITH_BIG_TICK","TRUE");
cha.setParameter("YAXIS_START_WITH_BIG_TICK","TRUE");
cha.setParameter("LEGEND_VERTICAL","false");//legend的显示方式,true为竖着显示
cha.setParameter("SERIE_1","最高值");
cha.setParameter("SERIE_2","平均值");
cha.setParameter("SERIE_FONT_1","Arial|PLAIN|8");
cha.setParameter("SERIE_FONT_2","Arial|PLAIN|8");
cha.setParameter("SERIE_COLOR_1","RED");
cha.setParameter("SERIE_COLOR_2","BLUE");
cha.setParameter("SERIE_STYLE_1","2|RED|LINE");
cha.setParameter("SERIE_STYLE_2","0.2|BLUE|LINE");
cha.setParameter("SERIE_POINT_1","true");
cha.setParameter("SERIE_POINT_2","true");
cha.setParameter("SERIE_POINT_COLOR_1","RED");
cha.setParameter("SERIE_POINT_COLOR_2","BLUE");
cha.setParameter("LEGEND_POSITION","BOTTOM");
cha.setParameter("LEFT_MARGIN","0.15");
cha.setParameter("RIGHT_MARGIN","0.05");
cha.setParameter("TOP_MARGIN","0.15");
cha.setParameter("BOTTOM_MARGIN","0.09");
cha.setParameter("CHART_BORDER","0.2|0x0|NORMAL");
cha.setParameter("CHART_FILL","0xeeffff");
cha.setParameter("CHART_LINKS_TARGET","_self");
cha.setParameter("SERIE_DATA_1",maxData);
cha.setParameter("SERIE_DATA_2",avgData);
Log.i(ChartPanel.TAG,"Building chart ...");
Chart c=cha.build(false,false);
Log.i(ChartPanel.TAG,"Build ok");
return c;
}