asp.net畫曲線圖(折線圖)代碼 詳細注釋

來源:互聯網
上載者:User

複製代碼 代碼如下:using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//添加畫圖類
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Data.SqlClient;
public partial class Curve_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Get_CurveData();
}
}
//擷取資料
public void Get_CurveData()
{
SqlConnection conn = null;
try
{
conn = CommonFunction.CreateDBTest();
conn.Open();
SqlCommand cmd = conn.CreateCommand();
string sqlStr = "SELECT * FROM CURVE ORDER BY TESTDATE";
DataTable dt = CommonFunction.ExecuteDatable(conn, cmd, CommandType.Text, sqlStr, null);
draw(dt);
}
catch (Exception exp)
{
Response.Write(exp.Message);
}
finally
{
if (conn != null)
conn.Close();
}
}
public void draw(DataTable dt)
{
//取得記錄數量
int count = dt.Rows.Count;
//記算圖表寬度
int wd = 80 + 20 * (count - 1);
//設定最小寬度為800
if (wd < 600) wd = 600;
//產生Bitmap對像
Bitmap img = new Bitmap(wd, 400);
//產生繪圖對像
Graphics g = Graphics.FromImage(img);
//定義黑色畫筆
Pen Bp = new Pen(Color.Black);
//定義紅色畫筆
Pen Rp = new Pen(Color.Red);
//定義銀灰色畫筆
Pen Sp = new Pen(Color.Silver);
//定義大標題字型
Font Bfont = new Font("Arial", 12, FontStyle.Bold);
//定義一般字型
Font font = new Font("Arial", 6);
//定義大點的字型
Font Tfont = new Font("Arial", 9);
//定義橫座標間隔,(最佳值是總寬度-留空寬度[左右側都需要])/(記錄數量-1)
int xSpace = (wd - 100) / (count - 1);
//定義縱座標間隔,不能隨便修改,跟高度和橫座標線的條數有關,最佳值=(繪圖的高度-上面留空-下面留空)
int ySpace = 30;
//縱座標最大值和間隔值
int yMaxValue = 30;
//繪製底色
g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height);
//定義黑色過渡型筆刷
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);
//定義藍色過渡型筆刷
LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);
//繪製大標題
g.DrawString("測試曲線圖", Bfont, brush, 40, 5);
//繪製資訊簡報
string info = " 曲線圖產生時間:" + DateTime.Now.ToString();
g.DrawString(info, Tfont, Bluebrush, 40, 25);
//繪製圖片邊框
g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);
//繪製深度軸
g.DrawLine(Bp, 40, 55, 40, 360);
//繪製橫座標軸 x2的60是右側空出部分
g.DrawLine(Bp, 40, 360, 60 + xSpace * (count - 1), 360);
//繪製豎座標標題
g.DrawString("測試值", Tfont, brush, 5, 40);
//繪製橫座標標題
g.DrawString("測試時間", Tfont, brush, 40, 385);
//繪製豎座標線
for (int i = 0; i < count; i++)
{
g.DrawLine(Sp, 40 + xSpace * i, 60, 40 + xSpace * i, 360);
}
//繪製時間軸座標標籤
for (int i = 0; i < count; i++)
{
string st = Convert.ToDateTime(dt.Rows[i]["testdate"]).ToString("MM:dd");
g.DrawString(st, font, brush, 30 + xSpace * i, 370);
}
//繪製橫座標線
for (int i = 0; i < 10; i++)
{
g.DrawLine(Sp, 40, 60 + ySpace * i, 40 + xSpace * (count - 1), 60 + ySpace * i);
//橫座標軸的值間隔是最大值除以間隔數
int s = yMaxValue - i * (yMaxValue / 10);
//繪製發送量軸座標標籤
g.DrawString(s.ToString(), font, brush, 10, 60 + ySpace * i);
}
//定義縱座標單位元值=縱座標最大值/標量最大值(300/30)
int yAveValue = 10;
//定義曲線轉折點
Point[] p = new Point[count];
for (int i = 0; i < count; i++)
{
p[i].X = 40 + xSpace * i;
p[i].Y = 360 - Convert.ToInt32(dt.Rows[i]["testvalue"]) * yAveValue;
}
//繪製折線圖
//g.DrawLines(Rp, p);
//繪製曲線圖
//g.DrawCurve(Rp, p);
//繪製自訂張力的曲線圖(0.5F是張力值,預設就是這個值)
g.DrawCurve(Rp, p,0.5F);
//當需要在一個圖裡繪製多條曲線的時候,就多定義個point數組,然後畫出來就可以了。
for (int i = 0; i < count; i++)
{
//繪製發送記錄點的發送量
g.DrawString(dt.Rows[i]["testvalue"].ToString(), font, Bluebrush, p[i].X, p[i].Y - 10);
//繪製發送記錄點
g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
}
//儲存繪製的圖片
MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Jpeg);
//圖片輸出
Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(stream.ToArray());
}
}
資料表的內容很簡單,就兩個欄位:testValue和testDate,由於圖的縱座標有最大值,所以testValue的值不能超過30,當然,你可以調整座標軸的單位或者高度。
12 2008-12-1 0:00:00
9 2008-12-5 0:00:00
20 2008-12-10 0:00:00
18 2008-12-15 0:00:00
27 2008-12-20 0:00:00
8 2008-12-25 0:00:00
15 2008-12-30 0:00:00
25 2009-1-1 0:00:00
23 2009-1-5 0:00:00
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.