打印

[asp.net教程] 自己写的一个asp.net 的生成曲线图的过程

自己写的一个asp.net 的生成曲线图的过程

这里是从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(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["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["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.x = 40 + 20 * i;
   p.y = 360- convert.toint32(ds.tables[0].rows["sendnum"]) / 5*3/5;
   }
   //绘制发送曲线
   g.drawlines(rp, p);
   
   for (int i = 0; i < count; i++)
   {
   //绘制发送记录点的发送量
   g.drawstring(ds.tables[0].rows["sendnum"].tostring(), font, bluebrush, p.x, p.y - 10);
   //绘制发送记录点
   g.drawrectangle(rp, p.x - 1, p.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.contenttype = "image/jpeg";
   page.response.binarywrite(stream.toarray());
   
   }
  }
  嘿嘿!发表这个,就是一个图片的绘制方法的记录了.这里面还有很多不当之久,望指教.
  http://www.cnblogs.com/aowind/archive/2006/11/23/569536.html

TOP

返回顶部
AYBlue

Processed in 0.055558 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-10 11:56 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP