LWUIT + J4LChart 之 多种Chart

  本文来自http://blog.csdn.net/hellogv/


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


  16. public class ChartDemo implements ActionListener {
  17.     public Form form = new Form("J4LChart");
  18.  
  19.   Display display;
  20.   Command nextExample= new Command("Next");
  21.   Command previousExample= new Command("Previous");
  22.   ChartViewer chartCanvas;
  23.   ChartLoader loader;

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

  29.   int currentExample=0;

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

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

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

  46.   /*
  47.    * 在读取TXT文件的同时,同时设置Chart里面的数值
  48.    */
  49.     private void createRealtimedata(ChartLoader loader) {

  50.         java.util.Date now=java.util.Calendar.getInstance().getTime();
  51.         loader.setParameter("XAXIS_INITIAL_DATE",Formats.formatDate(now,"dd/MM/yyyy hh:mm:ss"));
  52.         //  create random data based on the time
  53.         java.util.Calendar cal=java.util.Calendar.getInstance();
  54.         String data1="";
  55.         String data2="";
  56.         for (int i=0;i<9;i++) {
  57.                 cal.setTime(new Date(cal.getTime().getTime()-2000));
  58.                 if (data1.length()>0) data1="|" + data1;
  59.                 data1=""+cal.get(java.util.Calendar.SECOND)+ data1;
  60.                 if (data2.length()>0) data2="|" + data2;
  61.                 data2=""+(cal.get(java.util.Calendar.SECOND)/6)+ data2;
  62.         }

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

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

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

  101.         }
  102.     }
  103. }


你可能感兴趣的:(c,Date,transition)