利用.NET Compact Framework的绘制功能还可以绘制图形报表,例如:饼图、柱形图或折线图。下面以饼图为例,介绍如何绘制饼图的方法。绘制饼图的代码如清单7-8所示。
清单7-8:绘制饼图
<group id="_x0000_s1026" style="WIDTH: 414pt; HEIGHT: 678.6pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" coordsize="7200,11835" coordorigin="2526,7642" editas="canvas"><lock aspectratio="t" v:ext="edit"></lock><shapetype id="_x0000_t75" coordsize="21600,21600" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_s1027" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 11835px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><fill o:detectmouseclick="t"></fill><path o:connecttype="none" o:extrusionok="t"></path><lock v:ext="edit" text="t"></lock></font></shape><rect id="_x0000_s1028" style="LEFT: 2526px; WIDTH: 7200px; POSITION: absolute; TOP: 7642px; HEIGHT: 11835px" strokecolor="#eaeaea" fillcolor="#eaeaea"><textbox style="mso-next-textbox: #_x0000_s1028"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; BORDER-TOP-COLOR: #d4d0c8; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">……</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim cBmp As New Bitmap(236, 184) ' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">创建画布宽度为</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">236</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,高度为</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">184</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Bitmap</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">实例</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim cGraphic As Graphics<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim cColor() As Color = {Color.Red, Color.Blue, Color.Green, Color.Gray, _<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Color.LightCoral, Color.Gold} ' 5</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">种颜色</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim cPen As Pen<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim cSolidBrush As SolidBrush<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim cPoints() As Point<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim RowNum As Integer = 3<span style="mso-tab-count: 1"> </span>' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">定义扇区块数</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim i As Integer = 0<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim j As Integer = 0<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim cAngle As Integer = 0<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim tmp As Integer = 0<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim xCircleCenter As Integer = 100<span style="mso-tab-count: 1"> </span>' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">定义圆心</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">X</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">坐标</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Dim yCircleCenter As Integer = 100<span style="mso-tab-count: 1"> </span>' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">定义圆心</span><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Y</font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">坐标</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cGraphic = Graphics.FromImage(cBmp)<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cGraphic.Clear(Color.Snow)<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">For j = 0 To RowNum<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cAngle = 90 <span style="mso-tab-count: 1"> </span>' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">定义扇区角度</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">ReDim cPoints(cAngle + 1)<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">用数学公式画圆</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cPen = New Pen(cColor(j), 3)<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cSolidBrush = New SolidBrush(cColor(j))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">For i = tmp To tmp + cAngle<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cPoints(i - tmp) = New Point<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 63pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cPoints(i - tmp).X = (1 - System.Math.Sin(i / 360 * 2 * System.Math.PI)) * (xCircleCenter - 30) + 10<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 63pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cPoints(i - tmp).Y = (1 - System.Math.Cos(i / 360 * 2 * System.Math.PI)) * (yCircleCenter - 30) + 10<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Next i<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">加入圆心坐标点</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cPoints(cAngle + 1) = New Point<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cPoints(cAngle + 1).X = xCircleCenter - 20<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cPoints(cAngle + 1).Y = yCircleCenter - 20<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">' </font></span><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">绘制扇区对应的区域</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cPen = New Pen(cColor(j), 3)<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cSolidBrush = New SolidBrush(cColor(j))<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cGraphic.DrawPolygon(cPen, cPoints)<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">cGraphic.FillPolygon(cSolidBrush, cPoints)<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">tmp = tmp + cAngle<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman">Next<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; LINE-HEIGHT: 14pt"><span lang="EN-US" style="FONT-SIZE: 9pt"><font face="Times New Roman"><span style="mso-spacerun: yes"> </span>pb_graphic.Image = cBmp<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14pt"><span style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">……</span><span lang="EN-US" style="FONT-SIZE: 9pt"><p></p></span></p> </div> </td> </tr></tbody></table></textbox></rect><wrap type="none"></wrap><anchorlock></anchorlock></group>
在清单7-8中,首先定义一个指定大小的画布,画布也就是一个Bitmap实例。之后定义圆心点坐标。确定圆心点坐标则可以使用计算公式获得组成每个扇区弧线的点集合,弧线点集合与圆心点形成一个封闭的扇形区域。确定扇形区域后则可以开始绘制扇形图形并使用不同的颜色填充扇形区域。最后把绘制好的Bitmap实例通过PictureBox控件显示。清单7-8的执行结果如图7-6所示。
<shape id="_x0000_i1026" style="WIDTH: 155.25pt; HEIGHT: 206.25pt" type="#_x0000_t75"><imagedata o:title="" src="file:///D:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image002.png"><font face="Times New Roman" size="3"></font></imagedata></shape>
图7-6:绘制饼图
利用相同的原理,还可以实现柱图,如图7-7所示。
图7-7:绘制柱图