LWUIT + ChartComponent 之三实现柱体图(HBarChar)

本文来自http://blog.csdn.net/hellogv/
LWUIT + ChartComponent 之三实现柱体图(HBarChar)
本文源代码下载地址: 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. packagecom.sun.lwuit.uidemo;
  7. importcom.sun.lwuit.Button;
  8. importcom.sun.lwuit.Command;
  9. importcom.sun.lwuit.Font;
  10. importcom.sun.lwuit.Form;
  11. importcom.sun.lwuit.Image;
  12. importcom.sun.lwuit.Label;
  13. importcom.sun.lwuit.events.ActionEvent;
  14. importcom.sun.lwuit.events.ActionListener;
  15. importcom.sun.lwuit.layouts.FlowLayout;
  16. importorg.beanizer.j2me.charts.ChartItem;
  17. importorg.beanizer.j2me.charts.HBarChart;
  18. publicclassHBarChartDemoimplementsActionListener{
  19. publicFormform=newForm("HBarChartDemo");
  20. privateCommandbackCommand=newCommand("Back",1);
  21. finalHBarCharthbarChart=newHBarChart("");//定义HBarChart
  22. HBarChartDemo()
  23. {
  24. //柱体说明
  25. Stringchart_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. intwidth=form.getWidth();
  33. intheight=form.getHeight()-140;
  34. Imageimg_hbarChart=drawHBarChart(hbarChart,width,height,"",color,percent);//绘制柱体图
  35. Buttonbutton=newButton(img_hbarChart);
  36. //button.getStyle().setBgTransparency(1);//透明背景,会非常消耗资源,速度减慢,注意使用
  37. button.setBorderPainted(false);
  38. form.addComponent(button);
  39. form.addCommand(backCommand);
  40. form.setCommandListener(this);
  41. form.setLayout(newFlowLayout());//必须使用这种排列,FlowLayout最适合
  42. }
  43. privatevoidinitChartInfo(String[]chart_str,int[][]color)
  44. {
  45. for(inti=0;i<chart_str.length;i++)//循环
  46. {
  47. Labelchart_info=newLabel(chart_str[i]);
  48. chart_info.getStyle().setFgColor(UIDemoMIDlet.RGBtoInt(color[i][0],color[i][1],color[i][2]));
  49. form.addComponent(chart_info);
  50. }
  51. }
  52. privateImagedrawHBarChart(ChartItemitem,
  53. intwidth,
  54. intheight,
  55. Stringimagefile,
  56. int[][]color,//柱体颜色
  57. int[]percent)//柱体长度(百分比)
  58. {
  59. item.setFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL);
  60. item.setDrawAxis(true);
  61. item.setPreferredSize(width,height);//设置chart控件的大小
  62. if(imagefile.length()>0)//需要使用背景时
  63. {
  64. try{
  65. javax.microedition.lcdui.Imageimg=javax.microedition.lcdui.Image.createImage(imagefile);//读取背景图
  66. item.setBackgroundImage(img);//设置背景图
  67. }catch(Exceptionex){ex.printStackTrace();}
  68. }
  69. item.showShadow(true);//使用阴影特效
  70. item.setShadowColor(20,20,20);//设置阴影颜色
  71. item.setColor(40,40,200);
  72. item.resetData();
  73. for(inti=0;i<color.length;i++)//循环绘画柱体
  74. {
  75. item.addElement(String.valueOf((char)('a'+i)),percent[i],color[i][0],color[i][1],color[i][2]);
  76. }
  77. item.setMaxValue(100);//柱体代表数值的显示范围,100%
  78. //这个是lcdui的Image
  79. javax.microedition.lcdui.Imagelcdui_img=
  80. javax.microedition.lcdui.Image.createImage(width,height);//柱体大小,图像>控件
  81. //这个是lcdui的Graphics
  82. javax.microedition.lcdui.Graphicslcdui_g=lcdui_img.getGraphics();
  83. hbarChart.drawChart(lcdui_g,width-40,height-20);//这里设置的大小必须比width,height小,才能完全显示
  84. returnUIDemoMIDlet.lcdui2lwuit(lcdui_img);
  85. }
  86. publicvoidactionPerformed(ActionEventarg0){
  87. if(arg0.getCommand()==backCommand)
  88. {
  89. UIDemoMIDlet.backToMainMenu();
  90. }
  91. }
  92. }

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