首页 > 编程 > .NET > 正文

asp.net 绘制曲线图

2024-07-10 13:05:42
字体:
来源:转载
供稿:网友

asp.net 绘制曲线图首先定义drawclass类

 

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.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.drawing.drawing2d;
using system.drawing.imaging;
using system.drawing;
using system.io;

/**//// <summary>
/// drawclass 的摘要说明
/// </summary>
public class drawclass
...{
    public drawclass()
    ...{
        //
        // todo: 在此处添加构造函数逻辑
        //
    }

    public memorystream draw(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(new rectangle(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]["sendmum"]);
        }
        //绘制信息简报
        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]["sendmum"]) / 5 * 3 / 5;
        }
        //绘制发送曲线
        g.drawlines(rp, p);

        for (int i = 0; i < count; i++)
        ...{
            //绘制发送记录点的发送量
            g.drawstring(ds.tables[0].rows[i]["sendmum"].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);
        return stream;
       

    }
}
 

然后在页面文件中调用输出

 

drawclass dc = new drawclass();
        oracleconnection conn = new oracleconnection(con);
        string sql = "select * from atest";
        oracledataadapter da = new oracledataadapter(sql, conn);
        dataset ds = new dataset();
        da.fill(ds, "atest");
        memorystream ss=dc.draw(ds, 6);
        response.contenttype = "image/jpeg";
        response.binarywrite(ss.toarray());
 图片输出到page页面中也有另外一种方式

 

        filestream fs = new filestream(@"c:curve.jpg", filemode.open, fileaccess.read);
        byte[] mydata = new byte[fs.length];
        int length = convert.toint32(fs.length);
        fs.read(mydata, 0, length);
        fs.close();
        this.response.outputstream.write(mydata, 0, length);
        this.response.end();
 

这样就行了。

 

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表