c# 曲线图生成代码

时间:2023-03-27 07:52:55 


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;
}

}
}
}
标签:曲线图
0
投稿

猜你喜欢

  • springmvc 中dao层和service层的区别说明

    2022-07-26 00:55:41
  • 关于Spring Boot内存泄露排查的记录

    2023-06-27 18:46:19
  • SpringBoot解决Required String parameter xxx is not present问题

    2021-08-08 12:53:15
  • java中String字符串删除空格的七种方式

    2023-03-11 05:54:24
  • Javaweb动态开发最重要的Servlet详解

    2023-04-09 20:11:17
  • Java中synchronized的优化措施

    2022-10-09 18:19:05
  • 解析Java中未被捕获的异常以及try语句的嵌套使用

    2022-10-18 20:03:48
  • Java读取本地json文件及相应处理方法

    2023-10-16 16:37:34
  • C#调用WebService的方法介绍

    2022-06-05 01:10:30
  • C#中把任意类型的泛型集合转换成SQLXML数据格式的实例

    2022-12-24 08:16:21
  • C#之set与get方法的用法案例

    2021-08-09 01:17:18
  • android线程消息机制之Handler详解

    2023-07-28 11:31:52
  • SpringBoot接口如何统一异常处理

    2023-08-10 15:06:20
  • c# 使用计时器和观察者模式实现报警推送需求

    2022-05-24 02:16:46
  • MyBatis-Plus拦截器实现数据权限控制的示例

    2022-12-10 05:10:17
  • 用java等语言仿360首页拼音输入全模糊搜索和自动换肤

    2022-12-28 18:32:01
  • SpringBoot实现redis缓存菜单列表

    2023-11-24 07:39:20
  • Java中Lombok常用注解分享

    2023-06-13 01:32:48
  • springboot使用log4j2异步日志提升性能的实现方式

    2021-05-31 16:54:55
  • Java 接口和抽象类的区别详解

    2021-07-26 13:30:21
  • asp之家 软件编程 m.aspxhome.com