HardBirch

LWUIT + J4LChart 之 多种Chart

时间:08-12-19 栏目:安卓入门与提高 作者:张飞不张,文采横飞 评论:15 点击: 6,007 次




本文例子代码,可以到这里下载: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. }

声明: 本文由( 张飞不张,文采横飞 )原创编译,转载请保留链接: LWUIT + J4LChart 之 多种Chart

LWUIT + J4LChart 之 多种Chart:目前有15 条留言

  1. 15楼
    xZeus:

    不错的功能,看到官网的推荐,中国人顶一个
    thendmx@gmail.com

    2008-12-21 16:31 [回复]
  2. 14楼
    xZeus:

    不错的功能,看到官网的推荐,中国人顶一个
    thendmx@gmail.com

    2008-12-21 16:33 [回复]
  3. 13楼
    thend:

    # import com.java4less.rchart.lwuit.ChartViewer;
    # import com.java4less.rchart.util.Formats;
    这两个类在下载的java4less的jar里没有这个方法

    2008-12-21 19:20 [回复]
  4. 12楼
    hellogv:

    先确定是否针对J2ME版本的java4less的jar

    2008-12-21 19:29 [回复]
  5. 11楼
    hellogv:

    先确定是否针对J2ME版本的java4less的jar

    2008-12-21 19:29 [回复]
  6. 10楼
    gf771115:

    非常了不起,我在LWUIT FORUM的帖子上找到了这个链接,没想到美国佬也会浏览GOOGLE出来的中文帖子呀!

    2008-12-24 10:30 [回复]
  7. 了不起严重顶一�?d=0.28164000482014867

    2009-04-14 05:58 [回复]
  8. 8楼
    mmc0531:

    这个例子所需要的jar在哪下呀�?d=0.13649527971071956

    2009-04-15 14:57 [回复]
  9. 7楼
    mmc0531:

    GraphicsProvider.MODE_LWUIT 怎么没有呀�?d=0.6420466251532079

    2009-04-15 15:22 [回复]
  10. 6楼
    vincentlh:

    LZ 下了rchad2那个包文件 但是里面没找到
    # import com.java4less.rchart.lwuit.ChartViewer;
    # import com.java4less.rchart.util.Formats;
    这两个方法 确实是J2ME版本的jar啊 怎么没有呢??? 直接给个下载链接吧 感激不尽!!!

    2009-10-29 09:43 [回复]
  11. [e01]

    2010-03-29 11:57 [回复]
  12. [e01][e01][e04]

    2010-06-09 12:39 [回复]
  13. 地板
    clearshao:

    在我使用J4Less时遇到一个问题,就是在legend附近有一个“Rchart Demo”,请问LZ有没有办法将其去掉(我是用Android做的)

    2010-07-30 09:58 [回复]
  14. 板凳
    http832:

    回复 http832:大虾们,帮帮忙

    2010-10-21 17:06 [回复]
  15. 沙发
    http832:

    回复 clearshao:[e01]我也是遇到的

    2010-10-21 17:06 [回复]

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐