ASP.NET實現折線圖的繪制


用到.Net中繪圖類,實現折線圖的繪制,生成圖片,在頁面的顯示,代碼如下:

  1  /// <summary>
  2     /// 獲取數據 
  3     /// strChartName:圖名稱;
  4     /// yName:縱坐標名稱;
  5     /// xName:橫坐標名稱;
  6     /// iyMaxValue:縱坐標最大值;
  7     /// dyAveValue:縱坐標單位值=(縱坐標最大值/標量30)
  8     /// ----100   30    :3
  9     /// ----200   30    :1.5;
 10     /// xdbColumnName:橫坐標綁定顯示數據表值的列名;
 11     /// ydbColumnName:縱坐標綁定顯示數據表值得列名;
 12     /// </summary>
 13     public void Get_CurveData(string strSql,string strChartName,string yName,string xName,int iyMaxValue, double dyAveValue,string xdbColumnName,string ydbColumnName)
 14     {
 15         try
 16         {
 17             DataSet ds = sqlAccess.ReadFromDB(strSql);
 18             draw(ds.Tables[0], strChartName, yName, xName, iyMaxValue, dyAveValue, xdbColumnName, ydbColumnName);
 19         }
 20         catch (Exception exp)
 21         {
 22             Response.Write(sqlAccess.ExceptionMessage);
 23         }
 24     }
 25 
 26     public void draw(DataTable dt, string strChartName, string yName, string xName, int iyMaxValue, double dyAveValue, string xdbColumnName, string ydbColumnName)
 27     {
 28         //取得記錄數量 
 29         int count = dt.Rows.Count;
 30         //記算圖表寬度 
 31         int wd = 80 + 20 * (count - 1);
 32         //設置最小寬度為800 
 33         if (wd < 600) wd = 600;
 34         //生成Bitmap對像 
 35         Bitmap img = new Bitmap(wd, 400);
 36         //生成繪圖對像 
 37         Graphics g = Graphics.FromImage(img);
 38         //定義黑色畫筆 
 39         Pen Bp = new Pen(Color.Black);
 40         //定義紅色畫筆 
 41         Pen Rp = new Pen(Color.Red);
 42         //定義銀灰色畫筆 
 43         Pen Sp = new Pen(Color.Silver);
 44         //定義藍色畫筆
 45         Pen Blp = new Pen(Color.Blue);
 46         //定義大標題字體 
 47         Font Bfont = new Font("Arial", 12, FontStyle.Bold);
 48         //定義一般字體 
 49         Font font = new Font("Arial", 9);
 50         //定義大點的字體 
 51         Font Tfont = new Font("Arial", 9);
 52         //定義橫坐標間隔,(最佳值是總寬度-留空寬度[左右側都需要])/(記錄數量-1) 
 53         int xSpace = (wd - 100) / (count - 1);
 54         //定義縱坐標間隔,不能隨便修改,跟高度和橫坐標線的條數有關,最佳值=(繪圖的高度-上面留空-下面留空) 
 55         int ySpace = 30;
 56         //縱坐標最大值和間隔值 
 57         int yMaxValue = iyMaxValue;
 58         //繪制底色 
 59         g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height);
 60         //定義黑色過渡型筆刷 
 61         LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);
 62         //定義藍色過渡型筆刷 
 63         LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);
 64         //繪制大標題 
 65         g.DrawString(strChartName, Bfont, brush, 40, 5);
 66         //繪制信息簡報 
 67         //string info = " 曲線圖生成時間:" + DateTime.Now.ToString();
 68         //g.DrawString(info, Tfont, Bluebrush, 40, 25);
 69         //繪制圖片邊框 
 70         g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);
 71         //繪制豎坐標軸 
 72         g.DrawLine(Bp, 40, 55, 40, 360);
 73         //繪制橫坐標軸 x2的60是右側空出部分 
 74         g.DrawLine(Bp, 40, 360, 60 + xSpace * (count - 1), 360);
 75         //繪制豎坐標標題 
 76         g.DrawString(yName, Tfont, brush, 5, 40);
 77         //繪制橫坐標標題 
 78         g.DrawString(xName, Tfont, brush, 40, 385);
 79         //繪制豎坐標線 
 80         for (int i = 0; i < count; i++)
 81         {
 82             g.DrawLine(Sp, 40 + xSpace * i, 60, 40 + xSpace * i, 360);
 83         }
 84         //繪制時間軸坐標標簽 
 85         for (int i = 0; i < count; i++)
 86         {
 87             //string st = Convert.ToDateTime(dt.Rows[i]["testdate"]).ToString("MM:dd");
 88             //string st = "第" + dt.Rows[i]["testdate"].ToString() + "周";
 89             string st = dt.Rows[i][xdbColumnName].ToString();
 90             g.DrawString(st, font, brush, 30 + xSpace * i, 370);
 91         }
 92         //繪制橫坐標線 
 93         for (int i = 0; i < 10; i++)
 94         {
 95             g.DrawLine(Sp, 40, 60 + ySpace * i, 40 + xSpace * (count - 1), 60 + ySpace * i);
 96             //橫坐標軸的值間隔是最大值除以間隔數 
 97             int s = yMaxValue - i * (yMaxValue / 10);
 98             //繪制發送量軸坐標標簽 
 99             g.DrawString(s.ToString(), font, brush, 10, 60 + ySpace * i);
100         }
101 
102         //處理39.6%形式的數據
103         string[] strArr = new string[dt.Rows.Count];
104         for (int i = 0; i < count; i++)
105         {
106             string strValue = dt.Rows[i][ydbColumnName].ToString();
107             if (strValue.Contains("%"))
108             {
109                 strArr[i] = strValue.Split('%')[0];
110             }
111             else
112             {
113                 strArr[i] = strValue;
114             }
115         }
116         //200/30
117         //定義縱坐標單位數值=縱坐標最大值/標量最大值
118         double yAveValue = dyAveValue;
119         //定義曲線轉折點 
120         Point[] p = new Point[count];
121         for (int i = 0; i < count; i++)
122         {
123             p[i].X = 40 + xSpace * i;
124             p[i].Y = 360 - Convert.ToInt32(Convert.ToDouble(strArr[i]) * yAveValue);
125         }
126 
127         //繪制折線圖 
128         //g.DrawLines(Rp, p); 
129         //繪制曲線圖 
130         //g.DrawCurve(Rp, p); 
131         //繪制自定義張力的曲線圖(0.5F是張力值,默認就是這個值) 
132         g.DrawCurve(Rp, p, 0.5F);
133         //g.DrawLines(Rp, p); 
134         //當需要在一個圖里繪制多條曲線的時候,就多定義個point數組,然后畫出來就可以了。 
135         for (int i = 0; i < count; i++)
136         {
137             //繪制發送記錄點的發送量 
138             g.DrawString(strArr[i], font, Bluebrush, p[i].X, p[i].Y - 10);
139             //繪制發送記錄點 
140             g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
141         }
142 
143         ///*******************畫中值線///
144         //for (int i = 0; i < count; i++)
145         //{
146         //    p[i].X = 40 + xSpace * i;
147         //    p[i].Y = 360 - Convert.ToInt32("50") * yAveValue;
148         //}
149         //for (int i = 0; i < count; i++)
150         //{
151         //    //繪制發送記錄點的發送量 
152         //    g.DrawString("", font, Bluebrush, p[i].X, p[i].Y - 10);
153         //    //繪制發送記錄點 
154         //    g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
155         //}
156         //g.DrawLine(Blp, 40, 360 - Convert.ToInt32("50") * yAveValue, 60 + xSpace * (count - 1), 360 - Convert.ToInt32("50") * yAveValue);
157         ///**************************///
158 
159         //保存繪制的圖片 
160         MemoryStream stream = new MemoryStream();
161         img.Save(stream, ImageFormat.Jpeg);
162         //圖片輸出 
163         Response.Clear();
164         Response.ContentType = "image/jpeg";
165         Response.BinaryWrite(stream.ToArray());
166     } 
167 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM