ASP.NET实现折线图的绘制

   2023-02-09 学习力0
核心提示:用到.Net中绘图类,实现折线图的绘制,生成图片,在页面的显示,代码如下:1/// summary2 /// 获取数据 3 /// strChartName:图名称;4 /// yName:纵坐标名称;5 /// xName:横坐标名称;6 /// iyMaxValue:纵坐标最大值;7 /// dyAveValue:纵坐标单位值=(纵

用到.Net中绘图类,实现折线图的绘制,生成图片,在页面的显示,代码如下:

  1  /// <summary>
  2     /// 获取数据 
  3     /// strChartName:图名称;
  4     /// yName:纵坐标名称;
  5     /// xName:横坐标名称;
  6     /// iyMaxValue:纵坐标最大值;
  7     /// dyAveValue:纵坐标单位值=(纵坐标最大值/标量30)
  8     /// ----100   30    :3
  9     /// ----200   30    :1.5;
 10     /// xdbColumnName:横坐标绑定显示数据表值的列名;
 11     /// ydbColumnName:纵坐标绑定显示数据表值得列名;
 12     /// </summary>
 13     public void Get_CurveData(string strSql,string strChartName,string yName,string xName,int iyMaxValue, double dyAveValue,string xdbColumnName,string ydbColumnName)
 14     {
 15         try
 16         {
 17             DataSet ds = sqlAccess.ReadFromDB(strSql);
 18             draw(ds.Tables[0], strChartName, yName, xName, iyMaxValue, dyAveValue, xdbColumnName, ydbColumnName);
 19         }
 20         catch (Exception exp)
 21         {
 22             Response.Write(sqlAccess.ExceptionMessage);
 23         }
 24     }
 25 
 26     public void draw(DataTable dt, string strChartName, string yName, string xName, int iyMaxValue, double dyAveValue, string xdbColumnName, string ydbColumnName)
 27     {
 28         //取得记录数量 
 29         int count = dt.Rows.Count;
 30         //记算图表宽度 
 31         int wd = 80 + 20 * (count - 1);
 32         //设置最小宽度为800 
 33         if (wd < 600) wd = 600;
 34         //生成Bitmap对像 
 35         Bitmap img = new Bitmap(wd, 400);
 36         //生成绘图对像 
 37         Graphics g = Graphics.FromImage(img);
 38         //定义黑色画笔 
 39         Pen Bp = new Pen(Color.Black);
 40         //定义红色画笔 
 41         Pen Rp = new Pen(Color.Red);
 42         //定义银灰色画笔 
 43         Pen Sp = new Pen(Color.Silver);
 44         //定义蓝色画笔
 45         Pen Blp = new Pen(Color.Blue);
 46         //定义大标题字体 
 47         Font Bfont = new Font("Arial", 12, FontStyle.Bold);
 48         //定义一般字体 
 49         Font font = new Font("Arial", 9);
 50         //定义大点的字体 
 51         Font Tfont = new Font("Arial", 9);
 52         //定义横坐标间隔,(最佳值是总宽度-留空宽度[左右侧都需要])/(记录数量-1) 
 53         int xSpace = (wd - 100) / (count - 1);
 54         //定义纵坐标间隔,不能随便修改,跟高度和横坐标线的条数有关,最佳值=(绘图的高度-上面留空-下面留空) 
 55         int ySpace = 30;
 56         //纵坐标最大值和间隔值 
 57         int yMaxValue = iyMaxValue;
 58         //绘制底色 
 59         g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height);
 60         //定义黑色过渡型笔刷 
 61         LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);
 62         //定义蓝色过渡型笔刷 
 63         LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);
 64         //绘制大标题 
 65         g.DrawString(strChartName, Bfont, brush, 40, 5);
 66         //绘制信息简报 
 67         //string info = " 曲线图生成时间:" + DateTime.Now.ToString();
 68         //g.DrawString(info, Tfont, Bluebrush, 40, 25);
 69         //绘制图片边框 
 70         g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);
 71         //绘制竖坐标轴 
 72         g.DrawLine(Bp, 40, 55, 40, 360);
 73         //绘制横坐标轴 x2的60是右侧空出部分 
 74         g.DrawLine(Bp, 40, 360, 60 + xSpace * (count - 1), 360);
 75         //绘制竖坐标标题 
 76         g.DrawString(yName, Tfont, brush, 5, 40);
 77         //绘制横坐标标题 
 78         g.DrawString(xName, Tfont, brush, 40, 385);
 79         //绘制竖坐标线 
 80         for (int i = 0; i < count; i++)
 81         {
 82             g.DrawLine(Sp, 40 + xSpace * i, 60, 40 + xSpace * i, 360);
 83         }
 84         //绘制时间轴坐标标签 
 85         for (int i = 0; i < count; i++)
 86         {
 87             //string st = Convert.ToDateTime(dt.Rows[i]["testdate"]).ToString("MM:dd");
 88             //string st = "第" + dt.Rows[i]["testdate"].ToString() + "周";
 89             string st = dt.Rows[i][xdbColumnName].ToString();
 90             g.DrawString(st, font, brush, 30 + xSpace * i, 370);
 91         }
 92         //绘制横坐标线 
 93         for (int i = 0; i < 10; i++)
 94         {
 95             g.DrawLine(Sp, 40, 60 + ySpace * i, 40 + xSpace * (count - 1), 60 + ySpace * i);
 96             //横坐标轴的值间隔是最大值除以间隔数 
 97             int s = yMaxValue - i * (yMaxValue / 10);
 98             //绘制发送量轴坐标标签 
 99             g.DrawString(s.ToString(), font, brush, 10, 60 + ySpace * i);
100         }
101 
102         //处理39.6%形式的数据
103         string[] strArr = new string[dt.Rows.Count];
104         for (int i = 0; i < count; i++)
105         {
106             string strValue = dt.Rows[i][ydbColumnName].ToString();
107             if (strValue.Contains("%"))
108             {
109                 strArr[i] = strValue.Split('%')[0];
110             }
111             else
112             {
113                 strArr[i] = strValue;
114             }
115         }
116         //200/30
117         //定义纵坐标单位数值=纵坐标最大值/标量最大值
118         double yAveValue = dyAveValue;
119         //定义曲线转折点 
120         Point[] p = new Point[count];
121         for (int i = 0; i < count; i++)
122         {
123             p[i].X = 40 + xSpace * i;
124             p[i].Y = 360 - Convert.ToInt32(Convert.ToDouble(strArr[i]) * yAveValue);
125         }
126 
127         //绘制折线图 
128         //g.DrawLines(Rp, p); 
129         //绘制曲线图 
130         //g.DrawCurve(Rp, p); 
131         //绘制自定义张力的曲线图(0.5F是张力值,默认就是这个值) 
132         g.DrawCurve(Rp, p, 0.5F);
133         //g.DrawLines(Rp, p); 
134         //当需要在一个图里绘制多条曲线的时候,就多定义个point数组,然后画出来就可以了。 
135         for (int i = 0; i < count; i++)
136         {
137             //绘制发送记录点的发送量 
138             g.DrawString(strArr[i], font, Bluebrush, p[i].X, p[i].Y - 10);
139             //绘制发送记录点 
140             g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
141         }
142 
143         ///*******************画中值线///
144         //for (int i = 0; i < count; i++)
145         //{
146         //    p[i].X = 40 + xSpace * i;
147         //    p[i].Y = 360 - Convert.ToInt32("50") * yAveValue;
148         //}
149         //for (int i = 0; i < count; i++)
150         //{
151         //    //绘制发送记录点的发送量 
152         //    g.DrawString("", font, Bluebrush, p[i].X, p[i].Y - 10);
153         //    //绘制发送记录点 
154         //    g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
155         //}
156         //g.DrawLine(Blp, 40, 360 - Convert.ToInt32("50") * yAveValue, 60 + xSpace * (count - 1), 360 - Convert.ToInt32("50") * yAveValue);
157         ///**************************///
158 
159         //保存绘制的图片 
160         MemoryStream stream = new MemoryStream();
161         img.Save(stream, ImageFormat.Jpeg);
162         //图片输出 
163         Response.Clear();
164         Response.ContentType = "image/jpeg";
165         Response.BinaryWrite(stream.ToArray());
166     } 
167 }

 

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 使用WebClient自动填写并提交ASP.NET页面表单的源代码
    使用WebClient自动填写并提交ASP.NET页面表单的
    转自:http://www.cnblogs.com/anjou/archive/2007/03/07/667253.html 在.NET中通过程序填写和提交表单还是比较简单。比如,要提交一个如下图所示的登录表单:           填写和提交以上表单的代码如下:       // 要提交表单的URI字符串
    02-09
  • asp.net mvc多条件+分页查询解决方案
    


            
asp.net mvc多条件+分页查询解决方案
    asp.net mvc多条件+分页查询解决方案
    http://www.cnblogs.com/nickppa/p/3232535.html开发环境vs2010css:bootstrapjs:jquery    bootstrap paginator原先只是想做个mvc的分页,但是一般的数据展现都需要检索条件,而且是多个条件,所以就变成了MVC多条件+分页查询因为美工不是很好,所以用的是
    02-09
  • ASP.NET操作Cookies的问题(Bug or Not)
    以下存和取都是在不同的页面中,如果是在同一个页面也没必要用cookies了。 Test1: 给Cookies赋值: const string AAA="aaa"; Response.Cookies[AAA].Value = "111;222;333"; 取值: string value = Request.Cookies[AAA].Value; // value为111 Test2: 给Cooki
    02-09
  • Asp.Net Core 自定义验证属性
      很多时候,在模型上的验证需要自己定义一些特定于我们需求的验证属性。所以这一篇我们就来介绍一下怎么自定义验证属性。  我们来实现一个验证邮箱域名的自定义验证属性,当然,最重要的是需要定义一个继承自ValidationAttribute的类,然后在实现其IsVal
    02-09
  • Asp.Net 之 枚举类型的下拉列表绑定
    有这样一个学科枚举类型:/// 学科 /// /summary public enum Subject {None = 0,[Description("语文")]Chinese = 1,[Description("数学")]Mathematics = 2,[Description("英语")]English = 3,[Description("政治")]Politics = 4,[Description("物理&qu
    02-09
  • [ASP.NET笔记] 1.Web基础知识
         1:http协议:     2:web服务器:     3:静态网页的概念     4:动态网页的概念       http协议:http(hypertext transfer protocol) 即超文本传输协议,这个协议是在internet上进行信息传送的协议任何网页之间要相互沟通,必须要尊循
    02-09
  • ASP.NET邮件发送 .net 发送邮件
      今天做了个ASP.NET做发送邮件功能,发现QQ邮箱好奇怪,当你用QQ邮箱做服务器的时候什么邮件都发送不出去(QQ邮箱除外)。而且爆出这样的错误:"邮箱不可用。 服务器响应为: Error: content rejected.http://mail.qq.com/zh_CN/help/content/rejectedmail.ht
    02-09
  • 由ASP.NET Core根据路径下载文件异常引发的探究
    前言    最近在开发新的项目,使用的是ASP.NET Core6.0版本的框架。由于项目中存在文件下载功能,没有使用类似MinIO或OSS之类的分布式文件系统,而是下载本地文件,也就是根据本地文件路径进行下载。这其中遇到了一个问题,是关于如何提供文件路径的,通
    02-09
  • ASP.NET的运行原理与运行机制 ASP.NET的开发模式包括
    ASP.NET的运行原理与运行机制 ASP.NET的开发模
    在Asp.net4和4.5中,新增了WebPages Framework,编写页面代码使用了新的Razor语法,代码更加的简洁和符合Web标准,编写方式更接近于PHP和以前的Asp,和使用WebForms这种模仿Windows Form编程方式有了很大不同,不再有大量控件和控件生成的大量不够灵活的代码
    02-09
  • ASP.NET 后台接收前台POST过来的json数据方法
     ASP.NET前后台交互之JSON数据 https://www.cnblogs.com/ensleep/p/3319756.html
    02-09
点击排行