ASP.NET实现折线图的绘制

用到.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 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。