代碼如下:
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);