數據庫代碼同 柱狀統計圖。
Default.aspx頁面代碼

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;using System.Configuration;using System.Drawing;using System.IO;using System.Drawing.Drawing2D;using System.Data;public partial class LineTJImage : System.Web.UI.Page{ public string connStr = ConfigurationManager.ConnectionStrings["VisitCountConnectionString"].ToString(); PRotected void Page_Load(object sender, EventArgs e) { DrawLinearGradient(); } //訪問人數統計 public int Total() { int result = -1; string sql = "select count(1) from VisiteCount"; SqlConnection conn = new SqlConnection(connStr); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); result = Convert.ToInt32(cmd.ExecuteScalar()); cmd.Dispose(); conn.Close(); return result; } //柱形圖 public void DrawLinearGradient() { int width = 600, height = 400; Bitmap image = new Bitmap(width,height); Graphics g = Graphics.FromImage(image); g.Clear(Color.White); //畫矩形 g.FillRectangle(Brushes.WhiteSmoke, new Rectangle(0, 0, width, height)); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0,0,width,height),Color.Blue,Color.BlueViolet,1.2f,true); Font font = new Font("Arial",9,FontStyle.Regular); Font font1 = new Font("宋體",20,FontStyle.Bold); //寫標題 g.DrawString("2013網站瀏覽次數統計",font1,brush,new PointF(120,30)); Pen pen = new Pen(Color.Blue); Pen pen1 = new Pen(Color.Blue,2); //畫邊框 g.DrawRectangle(pen, 0, 0, width-1, height - 1); //設定橫向起始 int x = 100; for (int i = 0; i < 11; i++) { g.DrawLine(pen, x, 80, x, 340); x += 40; } //畫y軸線 int y = 106; for (int i = 0; i <9; i++) { g.DrawLine(pen, 60, y,540,y); y += 26; } g.DrawLine(pen1, 60, y, 540, y); //畫X軸線條 g.DrawLine(pen1,x-480,80,x-480,340); //X軸 string[] n = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"}; x = 62; for (int i = 0; i < 12; i++) { g.DrawString(n[i],font,Brushes.Black,new PointF(x,348)); x += 40; } g.DrawString("人/月", new Font("宋體",10,FontStyle.Italic), Brushes.Black, new PointF(35, 348)); //y軸 string[] m = { "100", "90", "80", "70", "60", "50", "40", "30", "20", "10", "0" }; y = 85; for (int i = 0; i < 11; i++) { g.DrawString(m[i].ToString(), font, Brushes.Black, 25, y);//設置文字內容以及輸出位置 y = y + 25; } //將檢索出的數據按一定比例繪制到圖像中 int[] count = new int[12]; string sql = ""; SqlConnection conn = new SqlConnection(connStr); conn.Open(); SqlDataAdapter da; DataSet ds = new DataSet(); for (int i = 0; i < 12; i++) { sql = @"select count(1) as count,Month(loginTime) as month from VisiteCount where YEAR(loginTime)=2013 and MONTH(loginTime)=" + (i + 1) + " group by MONTH(loginTime)"; da = new SqlDataAdapter(sql, conn); da.Fill(ds, i.ToString()); if (ds.Tables[i].Rows.Count == 0) { count[i] = 0; } else { //count[i] = Convert.ToInt32(ds.Tables[i].Rows[0][0].ToString())*100/Total(); count[i] = Convert.ToInt32(ds.Tables[i].Rows[0][0].ToString()); } } x = 70; Point[] points = new Point[12]; for (int i = 0; i < 12; i++) { //g.DrawLine(pen //g.DrawLine(pen, x, 340 - count[i] * 26 / 10, 20, count[i] * 26 / 10); points[i] = new Point(x, 340 - count[i] * 26 / 10); x += 40; } g.DrawLines(pen,points); MemoryStream ms = new MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ClearContent(); Response.ContentType = "image/Jpeg"; Response.BinaryWrite(ms.ToArray()); }}View Code 新聞熱點
疑難解答