asp.net畫(huà)曲線(xiàn)圖(折線(xiàn)圖)代碼 詳細(xì)注釋
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;
//添加畫(huà)圖類(lèi)
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();
}
}
//獲取數(shù)據(jù)
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)
{
//取得記錄數(shù)量
int count = dt.Rows.Count;
//記算圖表寬度
int wd = 80 + 20 * (count - 1);
//設(shè)置最小寬度為800
if (wd < 600) wd = 600;
//生成Bitmap對(duì)像
Bitmap img = new Bitmap(wd, 400);
//生成繪圖對(duì)像
Graphics g = Graphics.FromImage(img);
//定義黑色畫(huà)筆
Pen Bp = new Pen(Color.Black);
//定義紅色畫(huà)筆
Pen Rp = new Pen(Color.Red);
//定義銀灰色畫(huà)筆
Pen Sp = new Pen(Color.Silver);
//定義大標(biāo)題字體
Font Bfont = new Font("Arial", 12, FontStyle.Bold);
//定義一般字體
Font font = new Font("Arial", 6);
//定義大點(diǎn)的字體
Font Tfont = new Font("Arial", 9);
//定義橫坐標(biāo)間隔,(最佳值是總寬度-留空寬度[左右側(cè)都需要])/(記錄數(shù)量-1)
int xSpace = (wd - 100) / (count - 1);
//定義縱坐標(biāo)間隔,不能隨便修改,跟高度和橫坐標(biāo)線(xiàn)的條數(shù)有關(guān),最佳值=(繪圖的高度-上面留空-下面留空)
int ySpace = 30;
//縱坐標(biāo)最大值和間隔值
int yMaxValue = 30;
//繪制底色
g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height);
//定義黑色過(guò)渡型筆刷
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);
//定義藍(lán)色過(guò)渡型筆刷
LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);
//繪制大標(biāo)題
g.DrawString("測(cè)試曲線(xiàn)圖", Bfont, brush, 40, 5);
//繪制信息簡(jiǎn)報(bào)
string info = " 曲線(xiàn)圖生成時(shí)間:" + DateTime.Now.ToString();
g.DrawString(info, Tfont, Bluebrush, 40, 25);
//繪制圖片邊框
g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);
//繪制豎坐標(biāo)軸
g.DrawLine(Bp, 40, 55, 40, 360);
//繪制橫坐標(biāo)軸 x2的60是右側(cè)空出部分
g.DrawLine(Bp, 40, 360, 60 + xSpace * (count - 1), 360);
//繪制豎坐標(biāo)標(biāo)題
g.DrawString("測(cè)試值", Tfont, brush, 5, 40);
//繪制橫坐標(biāo)標(biāo)題
g.DrawString("測(cè)試時(shí)間", Tfont, brush, 40, 385);
//繪制豎坐標(biāo)線(xiàn)
for (int i = 0; i < count; i++)
{
g.DrawLine(Sp, 40 + xSpace * i, 60, 40 + xSpace * i, 360);
}
//繪制時(shí)間軸坐標(biāo)標(biāo)簽
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);
}
//繪制橫坐標(biāo)線(xiàn)
for (int i = 0; i < 10; i++)
{
g.DrawLine(Sp, 40, 60 + ySpace * i, 40 + xSpace * (count - 1), 60 + ySpace * i);
//橫坐標(biāo)軸的值間隔是最大值除以間隔數(shù)
int s = yMaxValue - i * (yMaxValue / 10);
//繪制發(fā)送量軸坐標(biāo)標(biāo)簽
g.DrawString(s.ToString(), font, brush, 10, 60 + ySpace * i);
}
//定義縱坐標(biāo)單位數(shù)值=縱坐標(biāo)最大值/標(biāo)量最大值(300/30)
int yAveValue = 10;
//定義曲線(xiàn)轉(zhuǎn)折點(diǎn)
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;
}
//繪制折線(xiàn)圖
//g.DrawLines(Rp, p);
//繪制曲線(xiàn)圖
//g.DrawCurve(Rp, p);
//繪制自定義張力的曲線(xiàn)圖(0.5F是張力值,默認(rèn)就是這個(gè)值)
g.DrawCurve(Rp, p,0.5F);
//當(dāng)需要在一個(gè)圖里繪制多條曲線(xiàn)的時(shí)候,就多定義個(gè)point數(shù)組,然后畫(huà)出來(lái)就可以了。
for (int i = 0; i < count; i++)
{
//繪制發(fā)送記錄點(diǎn)的發(fā)送量
g.DrawString(dt.Rows[i]["testvalue"].ToString(), font, Bluebrush, p[i].X, p[i].Y - 10);
//繪制發(fā)送記錄點(diǎn)
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());
}
}
數(shù)據(jù)表的內(nèi)容很簡(jiǎn)單,就兩個(gè)字段:testValue和testDate,由于圖的縱坐標(biāo)有最大值,所以testValue的值不能超過(guò)30,當(dāng)然,你可以調(diào)整坐標(biāo)軸的單位或者高度。
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
- Android開(kāi)發(fā)之天氣趨勢(shì)折線(xiàn)圖
- MPAndroidChart開(kāi)源圖表庫(kù)的使用介紹之餅狀圖、折線(xiàn)圖和柱狀圖
- jQuery實(shí)現(xiàn)折線(xiàn)圖的方法
- php下實(shí)現(xiàn)折線(xiàn)圖效果的代碼
- Android自定義View實(shí)現(xiàn)打字機(jī)效果
- Android自定義View實(shí)現(xiàn)圓環(huán)交替效果
- Android自定義view實(shí)現(xiàn)水波紋進(jìn)度球效果
- android自定義進(jìn)度條漸變色View的實(shí)例代碼
- Android 自定義View的使用介紹
- Android自定義View實(shí)現(xiàn)折線(xiàn)圖效果
相關(guān)文章
一個(gè)事半功倍的c#方法 動(dòng)態(tài)注冊(cè)按鈕事件
前幾天在網(wǎng)上看見(jiàn)一個(gè)制作計(jì)算器的c#程序,其中有一個(gè)動(dòng)態(tài)注冊(cè)按鈕事件,覺(jué)的很有用。于是實(shí)際操作了一哈, 確實(shí)比較好。2010-04-04FileUpload上傳圖片前實(shí)現(xiàn)圖片預(yù)覽功能(附演示動(dòng)畫(huà))
FileUpload控件上傳圖片前實(shí)現(xiàn)預(yù)覽,很多網(wǎng)友都希望實(shí)現(xiàn)這樣的功能,本人總結(jié)了一下,感興趣的朋友可以參考一下,希望對(duì)您有幫助2013-01-01C#設(shè)置本地網(wǎng)絡(luò)如DNS、網(wǎng)關(guān)、子網(wǎng)掩碼、IP等等
手動(dòng)設(shè)置本地網(wǎng)絡(luò)的方法顯然很不可取,所以我們要讓程序幫我們完成,需要的朋友可以參考下2014-03-03關(guān)于多對(duì)多關(guān)系表無(wú)法更新與插入的問(wèn)題
這篇文章主要介紹了關(guān)于多對(duì)多關(guān)系表無(wú)法更新與插入的問(wèn)題 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07ASP.NET通過(guò)byte正確安全的判斷上傳文件格式
本文介紹一種更安全的方式上傳圖片,他能有效的防止一些通過(guò)修改文件后綴或MIME來(lái)偽造的圖片的上傳,從而保證服務(wù)器的安全,希望對(duì)大家有所幫助。2016-03-03asp.net(c#)實(shí)現(xiàn)從sqlserver存取二進(jìn)制圖片的代碼
有一個(gè)員工表Employee,需要保存員工照片(Photo)到數(shù)據(jù)庫(kù)(sql server)上。員工照片對(duì)應(yīng)的字段是varbinary(max),也就是要存成二進(jìn)制文件類(lèi)型(這和以前討巧地存圖片文件路徑就不相同了),默認(rèn)可以為空。2011-09-09.NET實(shí)現(xiàn)在網(wǎng)頁(yè)中預(yù)覽Office文件的3個(gè)方法
這篇文章主要介紹了.NET實(shí)現(xiàn)在網(wǎng)頁(yè)中預(yù)覽Office文件的3個(gè)方法,本文最終采用了ASPOSE+pdf2swf+FlexPaper的方式解決了這個(gè)需求,需要的朋友可以參考下2014-10-10vb 中的MD5加密在asp.net中的實(shí)現(xiàn)
給定標(biāo)識(shí)哈希類(lèi)型的密碼和字符串,該例程產(chǎn)生一個(gè)適合存儲(chǔ)在配置文件中的哈希密碼,感興趣的朋友可以參考下本文2013-04-04