Windows Mobile中实现统计图形的绘制(C#版,柱状图)
首先,建立绘图类,如下:
1 /// <summary> 2 /// 绘制柱状图 3 /// </summary> 4 /// <param name="ds">DataTable中第一列为数据列,第二列为对应的名称</param> 5 /// <param name="xName">X轴名称</param> 6 /// <param name="yName">Y轴名称</param> 7 /// <returns>位图对象</returns> 8 public Bitmap GetDrawRectangleForStat(DataSet ds, string xName, string yName) 9 { 10 Bitmap cBmp = new Bitmap(471, 367); //创建画布宽度为471,高度为367的Bitmap实例 11 Graphics cGraphic; 12 Color[] cColor = new Color[] { Color.Red, Color.Blue, Color.Green, Color.Gray, Color.LightCoral, Color.Gold, Color.Brown, Color.Cyan, Color.Lime, Color.Peru, Color.Magenta, Color.Sienna, Color.Chocolate, Color.PeachPuff, Color.Orange, Color.DarkGoldenrod, Color.Olive, Color.DarkSeaGreen, Color.SpringGreen, Color.Teal, Color.CadetBlue, Color.RoyalBlue, Color.DeepPink, Color.LightGray, Color.MediumVioletRed, Color.Orchid, Color.MediumSlateBlue, Color.White, Color.LightSteelBlue, Color.DarkSlateGray, Color.GreenYellow, Color.DarkKhaki, Color.RosyBrown, Color.Navy, Color.Indigo, Color.HotPink }; //36种颜色 13 14 Pen cPen; 15 SolidBrush cSolidBrush; 16 Font bFont = new Font("Tahoma", 14, FontStyle.Bold); //轴标字体 17 Font sFont = new Font("Tahoma", 14, FontStyle.Bold); //统计数值字体 18 Font fFont = new Font("Tahoma", 14, FontStyle.Bold); //统计项值字体 19 20 StringFormat cFormat = new StringFormat(); 21 RectangleF cRect; 22 int RowNum = 0; 23 int iLoop = 0; 24 int i = 0; 25 int cValue = 0; 26 int MaxValue = 0; 27 28 //定义Y轴,Y上(42,15)~Y下(42,317),Y轴长从17到317共300 29 int xPu = 42; //Y轴上(Up)点X坐标 30 int yPu = 15; //Y轴上(Up)点Y坐标 31 int xPd = 42; //Y轴下(Down)点X坐标 32 int yPd = 317; //Y轴下(Down)点Y坐标 33 34 //定义X轴,X左(42,317)~X右(448,317),X轴上从42到448共406 35 int xPl = 42; //X轴左(Left)点X坐标 36 int yPl = 317; //X轴左(Left)点Y坐标 37 int xPr = 448; //X轴右(Right)点X坐标 38 int yPr = 317; //X轴右(Right)点Y坐标 39 40 //定义柱图 41 //int xStart = 57; //首根柱图开始的X轴坐标 42 //int cWidth = 20; //柱图宽度 43 //int cSpace = 15; //柱图间距 44 int xStart = 67; //首根柱图开始的X轴坐标 45 int cWidth = 40; //柱图宽度 46 int cSpace = 25; //柱图间距 47 48 cGraphic = Graphics.FromImage(cBmp); 49 cGraphic.Clear(Color.Snow); 50 51 //画轴线 52 cPen = new Pen(Color.Black, 3); 53 cSolidBrush = new SolidBrush(Color.Black); 54 cGraphic.DrawLine(cPen, xPu, yPu, xPd, yPd); //Y轴 55 cGraphic.DrawLine(cPen, xPl, yPl, xPr, yPr); //X轴 56 57 //画轴向 58 cGraphic.DrawLine(cPen, xPu, yPu - 3, xPu - 4, yPu + 3); //Y轴向 59 cGraphic.DrawLine(cPen, xPu, yPu - 3, xPu + 4, yPu + 3); 60 cGraphic.DrawLine(cPen, xPr + 3, yPr, xPr - 4, yPr - 3); //X轴向 61 cGraphic.DrawLine(cPen, xPr + 3, yPr, xPr - 4, yPr + 3); 62 63 //画轴标 64 cFormat.FormatFlags = StringFormatFlags.NoClip; 65 cGraphic.DrawString(yName, bFont, cSolidBrush, 5, 45, cFormat); //Y轴标 66 cGraphic.DrawString(xName, bFont, cSolidBrush, 392, 332, cFormat); //X轴标 67 68 //画轴心值 69 cGraphic.DrawString("0", sFont, cSolidBrush, xPd - 5, yPd + 3, cFormat); 70 71 RowNum = ds.Tables[0].Rows.Count - 1; 72 73 for (i = 0; i <= RowNum; i++) 74 { 75 if (MaxValue < Int32.Parse(ds.Tables[0].Rows[i][0].ToString())) 76 { 77 MaxValue = Int32.Parse(ds.Tables[0].Rows[i][0].ToString()); 78 } 79 80 //画柱图 81 cPen = new Pen(cColor[i], 3); 82 cSolidBrush = new SolidBrush(cColor[i]); 83 cValue = Int32.Parse(ds.Tables[0].Rows[i][0].ToString()) * ((yPd - yPu) / MaxValue); 84 cGraphic.DrawRectangle(cPen, xStart + (cWidth + cSpace) * iLoop, yPd - cValue, cWidth, cValue - 2); //减2的目的:使柱图的下边沿不挡住X轴 85 cGraphic.FillRectangle(cSolidBrush, xStart + (cWidth + cSpace) * iLoop, yPd - cValue, cWidth, cValue - 2); 86 87 //画柱图统计Y轴值 88 cGraphic.DrawString(ds.Tables[0].Rows[i][0].ToString(), sFont, cSolidBrush, xStart + (cWidth + cSpace) * iLoop - 2, yPd - cValue - 20, cFormat); 89 90 //画柱图统计X轴值 91 cRect = new RectangleF(xStart + (cWidth + cSpace) * iLoop, yPd + 1, cWidth + 10, 40); 92 cGraphic.DrawString(ds.Tables[0].Rows[i][1].ToString(), fFont, cSolidBrush, cRect, cFormat); 93 94 iLoop += 1; 95 } 96 97 return cBmp; 98 }
然后,在Windows Mobile的窗体中加入PictureBox控件,调用绘图方法,将图形展现到PictureBox控件中。
循自然之道,抚浮躁之心