Windows Mobile中实现统计图形的绘制(C#版,柱状图)
生活随笔
收集整理的這篇文章主要介紹了
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控件中。
轉載于:https://www.cnblogs.com/eagle927183/archive/2010/04/08/1706964.html
總結
以上是生活随笔為你收集整理的Windows Mobile中实现统计图形的绘制(C#版,柱状图)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么使用继承的一个实例
- 下一篇: 如何当好PM?请求大家积极讨论