c# 曲线图生成代码
程序员文章站
2023-08-17 11:25:08
复制代码 代码如下: using system; using system.collections.generic; using system.linq; using sy...
复制代码 代码如下:
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;
/// <summary>
/// x坐标的标题
/// </summary>
public string xtitle
{
get { return xtitle; }
set { xtitle = value; }
}
/// <summary>
/// y坐标的标题
/// </summary>
public string ytitle
{
get { return ytitle; }
set { ytitle = value; }
}
/// <summary>
/// 副标题
/// </summary>
public string title2
{
get { return title2; }
set { title2 = value; }
}
/// <summary>
/// 主标题
/// </summary>
public string title
{
get { return title; }
set { title = value; }
}
double ymax, ymin;
list<arraylist> itemlist;
public curvedrawing(list<arraylist> 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]);
}
}
/// <summary>
/// 创建并输出图片
/// </summary>
/// <returns>生成的文件路径</returns>
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;
}
}
}
}