本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。这里是从dataset里的数据生成曲线图.
我的dataset是从表sendrec里读取的数据,分别有id,sendid(订单号),sendtime(记录时间),sendnum(单位时间发送量/我这里是五分钟)几个字段
过程如下:
public void draw(page page,dataset ds,int tnum){}
其中page是用来传递引用这个过程的页面,这样让页面是jpg方式直接向客户端输出生成的曲线图.
ds就是取出来的数据集了
tnum只是我这里要用到的一个参数,不想让这个类去接触读取过程,所以把订单的总量直接取出后传递给它的.
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.drawing.drawing2d;
using system.drawing.imaging;
using system.drawing;
using system.io;
public class imgdraw
{
public imgdraw()
{
}
public void draw(page page,dataset ds,int tnum)
{
//取得记录数量
int count = ds.tables[0].rows.count;
//记算图表宽度
int wd = 80 + 20 * (count - 1);
//设置最小宽度为800
if (wd < 800) wd = 800;
//生成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);
//绘制底色
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(newrectangle(0, 0, img.width, img.height), color.blue, color.blue, 1.2f,true);
//绘制大标题
g.drawstring(ds.tables[0].rows[0]["sendid"].tostring() + "号订单发送情况曲线图", bfont, brush, 40, 5);
//取得当前发送量
int nums=0;
for (int i = 0; i < count; i++)
{
nums+=convert.toint32(ds.tables[0].rows[i]["sendnum"]);
}
//绘制信息简报
string info="订单发送时间:"+ds.tables[0].rows[0]["sendtime"].tostring()+" 曲线图生成时间:"+datetime.now.tostring()+" 订单总量:"+tnum.tostring()+" 当前发送总量:"+nums.tostring();
g.drawstring(info, tfont, bluebrush, 40, 25);
//绘制图片边框
g.drawrectangle(bp, 0, 0, img.width - 1, img.height - 1);
//绘制竖坐标线
for (int i = 0; i < count; i++)
{
g.drawline(sp, 40+20 * i, 60, 40+20 * i, 360);
}
//绘制时间轴坐标标签
for (int i = 0; i < count; i+=2)
{
string st = convert.todatetime(ds.tables[0].rows[i]["sendtime"]).tostring("hh:mm");
g.drawstring(st, font, brush, 30 + 20 * i, 370);
}
//绘制横坐标线
for (int i = 0; i < 10; i++)
{
g.drawline(sp, 40, 60+30*i, 40+20*(count-1), 60+30*i);
int s = 2500 - 50 * i * 5;
//绘制发送量轴坐标标签
g.drawstring(s.tostring(), font, brush, 10, 60 + 30 * i);
}
//绘制竖坐标轴
g.drawline(bp, 40, 55, 40, 360);
//绘制横坐标轴
g.drawline(bp, 40, 360, 45 + 20 * (count - 1), 360);
//定义曲线转折点
point[] p = new point[count];
for (int i = 0; i < count; i++)
{
p[i].x = 40 + 20 * i;
p[i].y = 360- convert.toint32(ds.tables[0].rows[i]["sendnum"]) / 5*3/5;
}
//绘制发送曲线
g.drawlines(rp, p);
for (int i = 0; i < count; i++)
{
//绘制发送记录点的发送量
g.drawstring(ds.tables[0].rows[i]["sendnum"].tostring(), font, bluebrush, p[i].x, p[i].y - 10);
//绘制发送记录点
g.drawrectangle(rp, p[i].x - 1, p[i].y - 1, 2, 2);
}
//绘制竖坐标标题
g.drawstring("发送量", tfont, brush, 5, 40);
//绘制横坐标标题
g.drawstring("发送时间", tfont, brush, 40, 385);
//保存绘制的图片
memorystream stream = new memorystream();
img.save(stream, imageformat.jpeg);
//图片输出
page.response.clear();
page.response.c;
page.response.binarywrite(stream.toarray());
}
}