ASP.NET实现折线图的绘制
用到.Net中绘图类,实现折线图的绘制,生成图片,在页面的显示,代码如下:
/// <summary>
/// 获取数据
/// strChartName:图名称;
/// yName:纵坐标名称;
/// xName:横坐标名称;
/// iyMaxValue:纵坐标最大值;
/// dyAveValue:纵坐标单位值=(纵坐标最大值/标量30)
/// ----100 30 :3
/// ----200 30 :1.5;
/// xdbColumnName:横坐标绑定显示数据表值的列名;
/// ydbColumnName:纵坐标绑定显示数据表值得列名;
/// </summary>
public void Get_CurveData(string strSql,string strChartName,string yName,string xName,int iyMaxValue, double dyAveValue,string xdbColumnName,string ydbColumnName)
{
try
{
DataSet ds = sqlAccess.ReadFromDB(strSql);
draw(ds.Tables[], strChartName, yName, xName, iyMaxValue, dyAveValue, xdbColumnName, ydbColumnName);
}
catch (Exception exp)
{
Response.Write(sqlAccess.ExceptionMessage);
}
} public void draw(DataTable dt, string strChartName, string yName, string xName, int iyMaxValue, double dyAveValue, string xdbColumnName, string ydbColumnName)
{
//取得记录数量
int count = dt.Rows.Count;
//记算图表宽度
int wd = + * (count - );
//设置最小宽度为800
if (wd < ) wd = ;
//生成Bitmap对像
Bitmap img = new Bitmap(wd, );
//生成绘图对像
Graphics g = Graphics.FromImage(img);
//定义黑色画笔
Pen Bp = new Pen(Color.Black);
//定义红色画笔
Pen Rp = new Pen(Color.Red);
//定义银灰色画笔
Pen Sp = new Pen(Color.Silver);
//定义蓝色画笔
Pen Blp = new Pen(Color.Blue);
//定义大标题字体
Font Bfont = new Font("Arial", , FontStyle.Bold);
//定义一般字体
Font font = new Font("Arial", );
//定义大点的字体
Font Tfont = new Font("Arial", );
//定义横坐标间隔,(最佳值是总宽度-留空宽度[左右侧都需要])/(记录数量-1)
int xSpace = (wd - ) / (count - );
//定义纵坐标间隔,不能随便修改,跟高度和横坐标线的条数有关,最佳值=(绘图的高度-上面留空-下面留空)
int ySpace = ;
//纵坐标最大值和间隔值
int yMaxValue = iyMaxValue;
//绘制底色
g.DrawRectangle(new Pen(Color.White, ), , , img.Width, img.Height);
//定义黑色过渡型笔刷
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(, , img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);
//定义蓝色过渡型笔刷
LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(, , img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);
//绘制大标题
g.DrawString(strChartName, Bfont, brush, , );
//绘制信息简报
//string info = " 曲线图生成时间:" + DateTime.Now.ToString();
//g.DrawString(info, Tfont, Bluebrush, 40, 25);
//绘制图片边框
g.DrawRectangle(Bp, , , img.Width - , img.Height - );
//绘制竖坐标轴
g.DrawLine(Bp, , , , );
//绘制横坐标轴 x2的60是右侧空出部分
g.DrawLine(Bp, , , + xSpace * (count - ), );
//绘制竖坐标标题
g.DrawString(yName, Tfont, brush, , );
//绘制横坐标标题
g.DrawString(xName, Tfont, brush, , );
//绘制竖坐标线
for (int i = ; i < count; i++)
{
g.DrawLine(Sp, + xSpace * i, , + xSpace * i, );
}
//绘制时间轴坐标标签
for (int i = ; i < count; i++)
{
//string st = Convert.ToDateTime(dt.Rows[i]["testdate"]).ToString("MM:dd");
//string st = "第" + dt.Rows[i]["testdate"].ToString() + "周";
string st = dt.Rows[i][xdbColumnName].ToString();
g.DrawString(st, font, brush, + xSpace * i, );
}
//绘制横坐标线
for (int i = ; i < ; i++)
{
g.DrawLine(Sp, , + ySpace * i, + xSpace * (count - ), + ySpace * i);
//横坐标轴的值间隔是最大值除以间隔数
int s = yMaxValue - i * (yMaxValue / );
//绘制发送量轴坐标标签
g.DrawString(s.ToString(), font, brush, , + ySpace * i);
} //处理39.6%形式的数据
string[] strArr = new string[dt.Rows.Count];
for (int i = ; i < count; i++)
{
string strValue = dt.Rows[i][ydbColumnName].ToString();
if (strValue.Contains("%"))
{
strArr[i] = strValue.Split('%')[];
}
else
{
strArr[i] = strValue;
}
}
//200/30
//定义纵坐标单位数值=纵坐标最大值/标量最大值
double yAveValue = dyAveValue;
//定义曲线转折点
Point[] p = new Point[count];
for (int i = ; i < count; i++)
{
p[i].X = + xSpace * i;
p[i].Y = - Convert.ToInt32(Convert.ToDouble(strArr[i]) * yAveValue);
} //绘制折线图
//g.DrawLines(Rp, p);
//绘制曲线图
//g.DrawCurve(Rp, p);
//绘制自定义张力的曲线图(0.5F是张力值,默认就是这个值)
g.DrawCurve(Rp, p, 0.5F);
//g.DrawLines(Rp, p);
//当需要在一个图里绘制多条曲线的时候,就多定义个point数组,然后画出来就可以了。
for (int i = ; i < count; i++)
{
//绘制发送记录点的发送量
g.DrawString(strArr[i], font, Bluebrush, p[i].X, p[i].Y - );
//绘制发送记录点
g.DrawRectangle(Rp, p[i].X - , p[i].Y - , , );
} ///*******************画中值线///
//for (int i = 0; i < count; i++)
//{
// p[i].X = 40 + xSpace * i;
// p[i].Y = 360 - Convert.ToInt32("50") * yAveValue;
//}
//for (int i = 0; i < count; i++)
//{
// //绘制发送记录点的发送量
// g.DrawString("", font, Bluebrush, p[i].X, p[i].Y - 10);
// //绘制发送记录点
// g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
//}
//g.DrawLine(Blp, 40, 360 - Convert.ToInt32("50") * yAveValue, 60 + xSpace * (count - 1), 360 - Convert.ToInt32("50") * yAveValue);
///**************************/// //保存绘制的图片
MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Jpeg);
//图片输出
Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(stream.ToArray());
}
}
ASP.NET实现折线图的绘制的更多相关文章
- 使用python内置库matplotlib,实现折线图的绘制
环境准备: 需要安装matplotlib,安装方式: pip install matplotlib 直接贴代码喽: #引入模块 from matplotlib import pyplot,font_m ...
- 【带着canvas去流浪】(2)绘制折线图
目录 一. 任务说明 二. 重点提示 三. 示例代码 3.1 一般折线图 3.2 用贝塞尔曲线绘制平滑折线图 四. 大数据量场景 示例代码托管在:https://github.com/dashnowo ...
- 【Android开源框架】使用andbase开发框架实现绘制折线图
在Android中,当有绘制折线图的需求时.大多数人使用的AChartEngine,来进行折线图的绘制.AChartEngine图表引擎确实能够实现折线图的功能.除此之外,我们还能够使用andbase ...
- 带着canvas去流浪系列之二 绘制折线图
[摘要] 用canvasAPI实现echarts简易图表 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 任务说明 使用原生canvasAPI绘制 ...
- java代码实现highchart与数据库数据结合完整案例分析(二)---折线图
作者原创:未经博主允许不许转载 在上一篇的博客中,展示和分析了如何做一个饼状图,有疑问可以参考上一篇博客. 现在分析和展示折线图的绘制和案例分析, 先展示效果图: 与饼状图不同的是,折线图展现更多的数 ...
- IOS使用Core-Plot画折线图
关于Core-Plot的配置.大家能够參考我的上一篇博客:http://1.wildcat.sinaapp.com/?p=99 版权全部.转载请注明原文转自:http://blog.csdn.net/ ...
- pyhton matplotlib可视化图像基础(二维函数图、柱状图、饼图、直方图以及折线图)
//2019.07.22pyhton中matplotlib模块的应用pyhton中matplotlib是可视化图像库的第三方库,它可以实现图像的可视化,输出不同形式的图形1.可视化图形的输出和展示需要 ...
- Matplotlib数据可视化(4):折线图与散点图
In [1]: from matplotlib import pyplot as plt import numpy as np import matplotlib as mpl mpl.rcParam ...
- 用canvas绘制折线图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- JavaWeb学习篇之----容器Response详解
今天在来看一下Response容器的相关知识,其实这篇blog早就应该编写了,只是最近有点忙,所以被中断了.下面我们就来看一下Response容器的相关知识吧.Response和我们即将在后面说到的R ...
- 脚本命令高级Bash脚本编程指南(31):数学计算命令
题记:写这篇博客要主是加深自己对脚本命令的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 高等Bash脚本编程指南(31):数学盘算命令 成于坚持,败于止步 操作数字 factor ...
- Codeforces Gym 100418K Cards 暴力打表
CardsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action? ...
- POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19356 ...
- iOS开发——混编Swift篇&OC移植为swift
将Ojective-C代码移植转换为Swift代码 2015-03-09 15:07发布:yuhang浏览:201 相比于Objective-C,Swift语言更加简练.有时我们需要把原来写的一些 ...
- 免费的天气预报API--谷歌,雅虎,中央气象台
Google Weather API 仅仅支持美国地区使用邮政编码进行查询,比如: http://www.google.com/ig/api?hl=zh-cn&weather=94043 ...
- MYSQL router 自动均衡负载
配制文件: /etc/mysqlrouter/mysqlrouter.ini [DEFAULT] logging_folder = /var/log/mysql-router plugin_folde ...
- shared_ptr的简单实现
前面讲到auto_ptr有个很大的缺陷就是所有权的转移,就是一个对象的内存块只能被一个智能指针对象所拥有.但我们有些时候希望共用那个内存块.于是C++ 11标准中有了shared_ptr这样的智能指针 ...
- 关于设置android:imeOptions属性无效的解决办法
在对Android的EditText控件进行设置时,经常会限定一下输入法的属性,设置右下角为完成或者搜索等,一般都会想到android:imeOptions属性,但是仅仅这么设置通常是无效的,还要搭配 ...
- UTF8 与 UTF16 编码
Unicode 的发展,英文好的直接去 unicode.org 上去看吧,不好的可以移步到这里 看dengyunze的总结:<关于UTF8,UTF16,UTF32,UTF16-LE,UTF16- ...