LWUIT + ChartComponent 之二實現線形圖(LineChart)

來源:互聯網
上載者:User
  本文來自http://blog.csdn.net/hellogv/

本文原始碼:http://download.csdn.net/source/872671
本文就不再說多餘的開場白了,想看開場白?看這裡:http://blog.csdn.net/hellogv/archive/2008/12/15/3521119.aspx
直接貼出實現線形圖的代碼:

  1. /*
  2.  * LWUIT + ChartComponent,實現多種圖表
  3.  * 作者:張國威(咪當俺系嚕嚕)
  4.  * 本例實現的是“線形圖”
  5.  */
  6. package com.sun.lwuit.uidemo;
  7. import com.sun.lwuit.Button;
  8. import com.sun.lwuit.Command;
  9. import com.sun.lwuit.Font;
  10. import com.sun.lwuit.Form;
  11. import com.sun.lwuit.Image;
  12. import com.sun.lwuit.Label;
  13. import com.sun.lwuit.events.ActionEvent;
  14. import com.sun.lwuit.events.ActionListener;
  15. import com.sun.lwuit.layouts.FlowLayout;
  16. import org.beanizer.j2me.charts.ChartItem;
  17. import org.beanizer.j2me.charts.LineChart;
  18. public class LineChartDemo implements ActionListener {
  19.     public Form form = new Form("LineChartDemo");
  20.     private  Command backCommand = new Command("Back", 1);
  21.     final LineChart lineChart = new LineChart("");
  22.     LineChartDemo()
  23.     {
  24.         //線形圖說明
  25.         String chart_str[]={"█ A:你好嗎","█ B:早上好","█ C:中午好","█ D:晚上好","█ E:吃宵夜","█ F:睡懶覺"};
  26.         //線條顏色
  27.         int [][]color={{0,0,200},{0,200,0},{200,0,0},{200,0,200},{0,200,200},{200,100,200}};
  28.         //線條高度
  29.         int []percent={15,10,5,20,34,16};
  30.        
  31.         initChartInfo(chart_str,color);
  32.         lineChart.setFill(true);//填充地區
  33.         int width=form.getWidth();
  34.         int height=form.getHeight()-140;
  35.         Image img_hbarChart=drawLineChart(lineChart,width,height,"",color,percent);//繪製線性圖
  36.         Button button = new Button(img_hbarChart);
  37.         //button.getStyle().setBgTransparency(1);//透明背景,會非常消耗資源,速度減慢,注意使用
  38.         button.setBorderPainted(false);
  39.         form.addComponent(button);
  40.         form.addCommand(backCommand);
  41.         form.setCommandListener(this);
  42.         form.setLayout(new FlowLayout());//必須使用這種排列,FlowLayout最適合
  43.     }
  44.     private void initChartInfo(String []chart_str,int [][]color)
  45.     {
  46.         for(int i=0;i<chart_str.length;i++)//迴圈
  47.         {
  48.             Label chart_info = new Label(chart_str[i]);
  49.             chart_info.getStyle().setFgColor(UIDemoMIDlet.RGBtoInt(color[i][0],color[i][1],color[i][2]));
  50.             form.addComponent(chart_info);
  51.         }
  52.     }
  53.     private Image drawLineChart(ChartItem item,
  54.             int width,
  55.             int height,
  56.             String imagefile,
  57.             int [][]color,//線條顏色
  58.             int []percent)//線條高度(百分比)
  59.     {
  60.         item.setFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL);
  61.         item.setDrawAxis(true);
  62.         item.setPreferredSize(width,height);//設定chart控制項的大小
  63.         item.setMargins(5,3,10,15);
  64.         if(imagefile.length()>0)//需要使用背景時
  65.         {
  66.             try{
  67.                 javax.microedition.lcdui.Image img=javax.microedition.lcdui.Image.createImage(imagefile);//讀取背景圖
  68.                 item.setBackgroundImage(img);//設定背景圖
  69.             } catch(Exception ex){ex.printStackTrace();}
  70.         }
  71.         item.showShadow(true);//使用陰影特效
  72.         item.setShadowColor(20,20,20);//設定陰影顏色
  73.         item.setColor(40, 40, 200);
  74.         item.resetData();
  75.         for(int i=0;i<color.length;i++)//迴圈線條
  76.         {
  77.             item.addElement(String.valueOf((char)('a'+i)),percent[i],color[i][0],color[i][1],color[i][2]);
  78.         }
  79.         item.setMaxValue(100);//柱體代表數值的顯示範圍,100%
  80.         //這個是lcdui的Image
  81.         javax.microedition.lcdui.Image lcdui_img=
  82.                 javax.microedition.lcdui.Image.createImage(width,height);//線形圖大小,映像>控制項
  83.         //這個是lcdui的Graphics
  84.         javax.microedition.lcdui.Graphics lcdui_g= lcdui_img.getGraphics();
  85.         lineChart.drawChart(lcdui_g,width-40,height-20);//這裡設定的大小必須比width,height小,才能完全顯示
  86.  
  87.         return UIDemoMIDlet.lcdui2lwuit(lcdui_img);
  88.     }
  89.     public void actionPerformed(ActionEvent arg0) {
  90.         if(arg0.getCommand()==backCommand)
  91.         {
  92.             UIDemoMIDlet.backToMainMenu();
  93.         }
  94.     }
  95. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.