LWUIT + ChartComponent 之二实现线形图(LineChart)

本文来自http://blog.csdn.net/hellogv/
LWUIT + ChartComponent 之二实现线形图(LineChart)

本文源代码下载地址: 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.LineChart;
  18. publicclassLineChartDemoimplementsActionListener{
  19. publicFormform=newForm("LineChartDemo");
  20. privateCommandbackCommand=newCommand("Back",1);
  21. finalLineChartlineChart=newLineChart("");
  22. LineChartDemo()
  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. initChartInfo(chart_str,color);
  31. lineChart.setFill(true);//填充区域
  32. intwidth=form.getWidth();
  33. intheight=form.getHeight()-140;
  34. Imageimg_hbarChart=drawLineChart(lineChart,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. privateImagedrawLineChart(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. item.setMargins(5,3,10,15);
  63. if(imagefile.length()>0)//需要使用背景时
  64. {
  65. try{
  66. javax.microedition.lcdui.Imageimg=javax.microedition.lcdui.Image.createImage(imagefile);//读取背景图
  67. item.setBackgroundImage(img);//设置背景图
  68. }catch(Exceptionex){ex.printStackTrace();}
  69. }
  70. item.showShadow(true);//使用阴影特效
  71. item.setShadowColor(20,20,20);//设置阴影颜色
  72. item.setColor(40,40,200);
  73. item.resetData();
  74. for(inti=0;i<color.length;i++)//循环线条
  75. {
  76. item.addElement(String.valueOf((char)('a'+i)),percent[i],color[i][0],color[i][1],color[i][2]);
  77. }
  78. item.setMaxValue(100);//柱体代表数值的显示范围,100%
  79. //这个是lcdui的Image
  80. javax.microedition.lcdui.Imagelcdui_img=
  81. javax.microedition.lcdui.Image.createImage(width,height);//线形图大小,图像>控件
  82. //这个是lcdui的Graphics
  83. javax.microedition.lcdui.Graphicslcdui_g=lcdui_img.getGraphics();
  84. lineChart.drawChart(lcdui_g,width-40,height-20);//这里设置的大小必须比width,height小,才能完全显示
  85. returnUIDemoMIDlet.lcdui2lwuit(lcdui_img);
  86. }
  87. publicvoidactionPerformed(ActionEventarg0){
  88. if(arg0.getCommand()==backCommand)
  89. {
  90. UIDemoMIDlet.backToMainMenu();
  91. }
  92. }
  93. }

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