首页 > 编程 > .NET > 正文

asp.net画曲线图(折线图)代码 详细注释

2024-07-10 12:42:31
字体:
来源:转载
供稿:网友
代码如下:
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);
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表