使用Flash MX和Java classes作实时的动态图表(台湾 陈建文)

2014-11-23 23:19:09 · 作者: · 浏览: 0
说明:本文是来自台湾的陈建文朋友(gd8936@seed.net.tw)投寄给JR的一篇文章,他希望能够把自己的一些“平时研究的心得和大家分享”。为了方便大家阅读,也应作者之约,本人将这篇文章转换为简体中文版,转换不当的地方还希望大家多多指教。在这儿,我也代表JR的所有朋友感谢建文朋友的热心奉献,希望能够读到建文更多的好文章!

繁体原版>>>
(注意在浏览器中设定为Big5码以正常浏览)

环境设定:

Flash MX + Flash Remoting Components + Flash Charting Compontnts
AP Server(笔者使用BEA的Weblogic 7) + Flash Remoting

前端部分:



将Flash Charting Compontnts中的BarChart组件拖放到场景中,并将其实体名称命名为chart, 在该影格中加入 Action Script内容如下:
  1. #include "NetDebug.as"
  2. #include "NetServices.as"
  3. //设定Flash Remoting Gateway 所在的URL路径
  4. NetServices.setDefaultGatewayURL("http://localhost:7001/flashservices/gateway");
  5. gatewayConnnection = NetServices.createGatewayConnection();
  6. //设定要调用的Java Class
  7. flashtestService = gatewayConnnection.getService("my_flash_remoting.myServer", this);
  8. //开始调用 getInformation 这个java method
  9. flashtestService.getInformation();
  10. var theData = new DataProviderClass();
  11. initChartData();
  12. //初始化chart中的数据
  13. function initChartData() {
  14. var items = new Array({label:"0#", value:0},{label:"1#",value:0},
  15. {label:"2#", value:0},{label:"3#", value:0},{label:"4#", value:0},
  16. {label:"5#", value:0},{label:"6#", value:0},{label:"7#", value:0},
  17. {label:"8#", value:0},{label:"9#", value:0});
  18. for (var i = 0; ilength; i++) {
  19. theData.addItem(items[i]);
  20. }
  21. chart.setDataProvider(theData);
  22. }
  23. //这是 getInformation 这个java method 完成后会触发的 funtion
  24. function getInformation_Result(result) {
  25. theData.replaceItemAt(result["no"], result);
  26. //数据更新后,再次调用 getInformation 这个java method,这样才会持续更新数据
  27. flashtestService.getInformation();
  28. }

后端部分:

将以下的代码存成 myServer.java,编译后保存在my_flash_remoting的目录下,然后将my_flash_remoting的上一层目录设到Ap Server 的 classes path中使Ap Server 能存取到我们写的class。(当然也可以打包成jar档案文件,加到classes path中)
  1. package my_flash_remoting;
  2. import java.util.*;
  3. public class myServer {
  4. public myServer() {}
  5. public Map getInformation(){
  6. Map resources = new HashMap();
  7. //以随机数取得要等待的时间(0ms~4000ms)
  8. int delay = (new Double( Math.random()*5).intValue())*1000;
  9. //以随机数取得要更新的数据(0~9)
  10. Integer no = new Integer((new Double( Math.random()*10).intValue()) );
  11. //以随机数取得要更新的值 (0~99)
  12. Integer value = new Integer((new Double( Math.random()*100).intValue()));
  13. //将no,label,value置入类型为HashMap的resource中
  14. resources.put("no",no);
  15. resources.put("label",no+"#");
  16. resources.put("value",value);
  17. //将相关信息输出到console中,以方便观察
  18. System.out.println("Delay Time="