c# .net 生成曲线图的代码
本文导语: 过程如下: public void draw(Page page,DataSet ds,int Tnum){} 说明: page用来传递引用这个过程的页面,这样让页面是JPG方式直接向客户端输出生成的曲线图。 ds就是取出来的数据集。 Tnum只是要用到的一个参数,不想让这个类去接触读...
过程如下:
public void draw(Page page,DataSet ds,int Tnum){}
说明:
page用来传递引用这个过程的页面,这样让页面是JPG方式直接向客户端输出生成的曲线图。
ds就是取出来的数据集。
Tnum只是要用到的一个参数,不想让这个类去接触读取过程,所以把订单的总量直接取出后传递给它的。
完整代码:
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(newRectangle(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[i]["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[i]["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[i].X = 40 + 20 * i;
p[i].Y = 360- Convert.ToInt32(ds.Tables[0].Rows[i]["sendnum"]) / 5*3/5;
}
//绘制发送曲线
g.DrawLines(Rp, p);
for (int i = 0; i < count; i++)
{
//绘制发送记录点的发送量
g.DrawString(ds.Tables[0].Rows[i]["sendnum"].ToString(), font, Bluebrush, p[i].X, p[i].Y - 10);
//绘制发送记录点
g.DrawRectangle(Rp, p[i].X - 1, p[i].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.C;
page.Response.BinaryWrite(stream.ToArray());
}
}
附,另一个版本的c#生成曲线图的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
using System.Collections;
namespace Curve
{
public class CurveDrawing
{
string title, title2, ytitle, xtitle;
///
/// X坐标的标题
///
public string Xtitle
{
get { return xtitle; }
set { xtitle = value; }
}
///
/// Y坐标的标题
///
public string Ytitle
{
get { return ytitle; }
set { ytitle = value; }
}
///
/// 副标题
///
public string Title2
{
get { return title2; }
set { title2 = value; }
}
///
/// 主标题
///
public string Title
{
get { return title; }
set { title = value; }
}
double yMax, yMin;
List itemlist;
public CurveDrawing(List itemlist, string title, string title2 = "")
{
this.itemlist = itemlist;
this.title = title;
this.title2 = title2;
yMax = -100000000;
yMin = 100000000;
for (int i = 0; i < itemlist.Count; i++)
{
if (Convert.ToDouble(itemlist[i][1]) > yMax)
yMax = Convert.ToDouble(itemlist[i][1]);
if (Convert.ToDouble(itemlist[i][1]) < yMin)
yMin = Convert.ToDouble(itemlist[i][1]);
}
}
///
/// 创建并输出图片
///
/// 生成的文件路径
public string Draw()
{
#region 基础定义
//取得记录数量
int count = itemlist.Count;
//记算图表宽度
int wd = 80 + 50 * (count - 1);
//设置最小宽度为640
if (wd < 640) wd = 640;
//生成Bitmap对像
Bitmap img = new Bitmap(wd, 400);
//定义黑色画笔
Pen Bp = new Pen(Color.Black);
//加粗的黑色
Pen BBp = new Pen(Color.Black, 2);
//定义红色画笔
Pen Rp = new Pen(Color.Red);
//定义银灰色画笔
Pen Sp = new Pen(Color.Silver);
//定义大标题字体
Font Bfont = new Font("黑体", 12, FontStyle.Bold);
//定义一般字体
Font font = new Font("Arial", 8);
//定义大点的字体
Font Tfont = new Font("Arial", 9);
//定义黑色过渡型笔刷
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);
LinearGradientBrush Silverbrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Silver, Color.Silver, 1.2F, true);
#endregion
//生成绘图对像
try
{
using (Graphics g = Graphics.FromImage(img))
{
#region 绘制图表
//绘制底色
g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height);
//绘制大标题
g.DrawString(title, Bfont, brush, wd / 2 - title.Length * 10, 5);
//绘制小标题
g.DrawString(title2, Tfont, Silverbrush, wd / 2 - title.Length * 10 + 40, 25);
//绘制图片边框
g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);
//绘制Y坐标线
for (int i = 0; i < (count < 12 ? 12 : count); i++)
g.DrawLine(Sp, 40 + 50 * i, 60, 40 + 50 * i, 360);
//绘制X轴坐标标签
for (int i = 0; i < count; i++)
g.DrawString(itemlist[i][0].ToString(), font, brush, 30 + 50 * i, 370);
//绘制X坐标线
for (int i = 0; i < 11; i++)
{
g.DrawLine(Sp, 40, 60 + 30 * i, 40 + 50 * ((count < 12 ? 12 : count) - 1), 60 + 30 * i);
double s = yMax - (yMax + Math.Abs(yMin)) / 10 * i;//最大的Y坐标值
g.DrawString(Math.Floor(s).ToString(), font, brush, 10, 55 + 30 * i);
}
//绘制Y坐标轴
g.DrawLine(BBp, 40, 50, 40, 360);
//绘制X坐标轴
g.DrawLine(BBp, 40, 360, 40 + 50 * ((count < 12 ? 12 : count) - 1) + 10, 360);
#endregion
#region 绘制曲线
//定义曲线转折点
Point[] p = new Point[count];
for (int i = 0; i < count; i++)
{
p[i].X = 40 + 50 * i;
p[i].Y = 360 - (int)(((Convert.ToDouble(itemlist[i][1]) + Math.Abs(yMin)) / ((yMax + Math.Abs(yMin)) / 10)) * 30);
}
//绘制发送曲线
g.DrawLines(Rp, p);
for (int i = 0; i < count; i++)
{
//绘制发送记录点的数值
g.DrawString(itemlist[i][1].ToString(), font, Bluebrush, p[i].X + 5, p[i].Y - 10);
//绘制发送记录点
g.DrawRectangle(Rp, p[i].X - 2, p[i].Y - 2, 4, 4);
}
#endregion
//绘制Y坐标标题
g.DrawString(ytitle, Tfont, brush, 10, 40);
//绘制X坐标标题
g.DrawString(xtitle, Tfont, brush, 30, 385);
//图片质量
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//保存绘制的图片
string basePath = HttpContext.Current.Server.MapPath("/Curve/"),
fileName = Guid.NewGuid() + ".jpg";
using (FileStream fs = new FileStream(basePath + fileName, FileMode.CreateNew))
{
if (!System.IO.Directory.Exists(basePath))
System.IO.Directory.CreateDirectory(basePath);
img.Save(fs, ImageFormat.Jpeg);
return "/Curve/" + fileName;
}
}
}
catch (Exception)
{
throw;
}
}
}
}