首页 > 开发 > 综合 > 正文

C#画折线图

2024-07-21 02:26:38
字体:
来源:转载
供稿:网友

using system;
using system.data;
using system.configuration;
using system.collections;
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;
using system.drawing.imaging;
public partial class default5 : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {
        this.pic();


    }
    private void pic()
    {
        //测试数据
        datatable table = new datatable("data");
        datarow dr;
        datacolumn dc = new datacolumn("id",type.gettype("system.int32"));
        datacolumn dc2 = new datacolumn("num",type.gettype("system.int32"));
        datacolumn dc3 = new datacolumn("name", type.gettype("system.string"));
        table.columns.add(dc);
        table.columns.add(dc2);
        table.columns.add(dc3);
        random rnd=new random();
        for (int n = 0; n < 61; n++)
        {
            dr = table.newrow();
            dr[0] = n;
            dr[1] = rnd.next(10, 140);
            dr[2] = n.tostring();
            table.rows.add(dr);
        }
        //画图参数
        int bg_width = 450;
        int bg_height = 180;
        int pic_width = 450;
        int pic_height = 180;
        int pic_x = 6;
        int pic_h = 1;
        int pic_tr=5;
        int pic_td = 12;
        rectangle rec = new rectangle(50, 15, 360, 150);
        pen pic_bolder = new pen(color.black, 1);
        pen pic_line = new pen(color.gray, 1);
        pen pic_data = new pen(color.red,2);
        solidbrush brusth = new solidbrush(color.blue);
        point[] datapt = new point[table.rows.count];
        int x;
        int y;
        for (int n = 0; n < table.rows.count; n++)
        {
            dr=table.rows[n];
            x=(int)dr[0] * pic_x + rec.x;
            y=(int)dr[1] * pic_h + rec.y;
            datapt[n] = new point(x,y);
        }
        bitmap bg = new bitmap(bg_width, bg_height, pixelformat.format24bpprgb);
        graphics ph = graphics.fromimage(bg);
        ph.clear(color.white);      
        ph.drawrectangle(pic_bolder, rec);
        //画折线
        ph.drawcurve(pic_data, datapt);
        //rec.
        point spoint=new point();
        point epoint=new point();
        //画横线
        for (int n = 1; n < pic_tr; n++)
        {
           //cell[0] = new point(rec.x);
            spoint.x = 0 + rec.x;
            spoint.y = n * 30 + rec.y;
            epoint.x = rec.width + rec.x;
            epoint.y = n * 30 + rec.y;
            ph.drawline(pic_line,spoint,epoint);
        }
        //画竖线
        for (int n = 1; n < pic_td; n++)
        {
            spoint.x = n * 30 +rec.x;
            spoint.y = rec.y;
            epoint.x = n * 30 + rec.x;
            epoint.y = rec.height+ rec.y;
            ph.drawline(pic_line, spoint, epoint);
        }
        //画标题
        string title = "画折线测试";       
        solidbrush brush=new solidbrush(color.royalblue);
        ph.drawstring(title, new font("franklin gothic demi", 12, fontstyle.italic), brush, new point(200, 0));
        ph.save();
        bg.save(response.outputstream, imageformat.gif);
    }
}

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