LWUIT + J4LChart 之 多种Chart

本文来自http://blog.csdn.net/hellogv/
LWUIT + J4LChart 之 多种Chart

本文例子代码,可以到这里下载:http://download.csdn.net/source/886261
Lwuit就不必介绍了,至于J4LChart,就不得不介绍一下了。。。。。。 J4LChart,提供了完整的Chart图表,从饼、线、柱等都一网打尽,并且其使用也比较简单,Chart的式样可以从TXT文本中读取,当然,也可以在代码里修改啦,不过,用TXT文件读取,可以做成模板那样,想换就换。。。。。。
下面就贴出J4LChart的使用代码,也好简单的:
  1. packagecom.sun.lwuit.uidemo;
  2. importcom.sun.lwuit.Command;
  3. importcom.sun.lwuit.Form;
  4. importcom.sun.lwuit.events.ActionEvent;
  5. importcom.sun.lwuit.events.ActionListener;
  6. importcom.java4less.rchart.Chart;
  7. importcom.java4less.rchart.ChartLoader;
  8. importcom.java4less.rchart.gc.GraphicsProvider;
  9. importcom.java4less.rchart.lwuit.ChartViewer;
  10. importcom.java4less.rchart.util.Formats;
  11. importcom.sun.lwuit.Display;
  12. importcom.sun.lwuit.animations.Transition3D;
  13. importcom.sun.lwuit.geom.Dimension;
  14. importcom.sun.lwuit.layouts.BorderLayout;
  15. importjava.util.Date;


  16. publicclassChartDemoimplementsActionListener{
  17. publicFormform=newForm("J4LChart");
  18. Displaydisplay;
  19. CommandnextExample=newCommand("Next");
  20. CommandpreviousExample=newCommand("Previous");
  21. ChartViewerchartCanvas;
  22. ChartLoaderloader;

  23. String[]examples={"areaChart.txt","areaCurves.txt","axisOnTop.txt","barChart3D.txt","barChart3D.txt","barChart.txt",
  24. "barImages.txt","bubbleChart.txt","candleStick.txt","combined2Positions.txt","combinedChart.txt","event.txt","linechart3D.txt",
  25. "linechart.txt","linechartClipping.txt","linechartNULLS.txt","MultiAxis.txt","MultiCharts.txt","patternFilling.txt",
  26. "piechart3D.txt","PieDrillDown.txt","radarChart.txt","realtime.txt","scatterChart.txt","stackedBar.txt","StackedMultiAxis.txt",
  27. "subpie1.txt","subpie2.txt","subpie3.txt","targetZones.txt"};

  28. intcurrentExample=0;

  29. /*
  30. *初始化Chart,从文本读取chart图结构并显示
  31. */
  32. ChartDemo()
  33. {
  34. GraphicsProvider.setMode(GraphicsProvider.MODE_LWUIT);

  35. form.addCommand(previousExample);//上一个
  36. form.addCommand(nextExample);//下一个

  37. form.setCommandListener(this);
  38. chartCanvas=newChartViewer();//chart视图
  39. chartCanvas.setPreferredSize(newDimension(200,200));//设置大小
  40. loadChartFromFile("/txt/"+examples[currentExample]);//读取文本,设置chart
  41. form.setLayout(newBorderLayout());
  42. form.addComponent(BorderLayout.CENTER,chartCanvas);
  43. form.refreshTheme();
  44. }

  45. /*
  46. *在读取TXT文件的同时,同时设置Chart里面的数值
  47. */
  48. privatevoidcreateRealtimedata(ChartLoaderloader){

  49. java.util.Datenow=java.util.Calendar.getInstance().getTime();
  50. loader.setParameter("XAXIS_INITIAL_DATE",Formats.formatDate(now,"dd/MM/yyyyhh:mm:ss"));
  51. //createrandomdatabasedonthetime
  52. java.util.Calendarcal=java.util.Calendar.getInstance();
  53. Stringdata1="";
  54. Stringdata2="";
  55. for(inti=0;i<9;i++){
  56. cal.setTime(newDate(cal.getTime().getTime()-2000));
  57. if(data1.length()>0)data1="|"+data1;
  58. data1=""+cal.get(java.util.Calendar.SECOND)+data1;
  59. if(data2.length()>0)data2="|"+data2;
  60. data2=""+(cal.get(java.util.Calendar.SECOND)/6)+data2;
  61. }

  62. loader.setParameter("SERIE_DATA_1",data1);
  63. loader.setParameter("SERIE_DATA_2",data2);

  64. }
  65. /*
  66. *读取TXT文件并设置chart
  67. */
  68. publicvoidloadChartFromFile(Stringfilename){
  69. ChartLoadercha=newChartLoader();
  70. cha.loadFromFile(this.getClass().getResourceAsStream(filename),true);//读取文本
  71. //this.loader=cha;
  72. //********设置Chart数值
  73. //createRealtimedata(cha);
  74. //********设置Chart数值
  75. Chartc=cha.build(false,false);//建立chart
  76. chartCanvas.setChart(c);
  77. chartCanvas.getChart().setSize(chartCanvas.getWidth(),chartCanvas.getHeight());
  78. chartCanvas.repaintChart();
  79. form.setTitle(filename);
  80. }

  81. publicvoidactionPerformed(ActionEventarg0){
  82. if(arg0.getCommand()==nextExample)//读取上一个chart(TXT文本)
  83. {
  84. chartCanvas.getChart().stopUpdater();
  85. currentExample++;
  86. if(currentExample>=examples.length-1)
  87. currentExample=0;
  88. form.replace(chartCanvas,chartCanvas,Transition3D.createRotation(500,true));
  89. loadChartFromFile("/txt/"+examples[currentExample]);
  90. }
  91. elseif(arg0.getCommand()==previousExample)//读取下一个chart(TXT文本)
  92. {
  93. chartCanvas.getChart().stopUpdater();
  94. currentExample--;
  95. if(currentExample<=0)
  96. currentExample=examples.length-1;
  97. form.replace(chartCanvas,chartCanvas,Transition3D.createRotation(500,false));
  98. loadChartFromFile("/txt/"+examples[currentExample]);

  99. }
  100. }
  101. }


你可能感兴趣的:(.net,Blog,sun)