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
投稿
猜你喜欢
- 引言其实对于分库分表这块的场景,目前市场上有很多成熟的开源中间件,eg:MyCAT,Cobar,sharding-JDBC等。本文主要是介绍
- 一个围绕统计分析功能的系统,在最后制作统计分析时需要一个批量点击的功能,用以批量制作echarts图形后生成图片并保存图形和图片。方便后续导
- 实践过程效果代码public partial class frmSend : Form{ public frmSe
- 把最近听的写的一些题目做下笔记!1.下列程序的执行,说法错误的是 ( ABC )public class MultiCatch
- 前言:前面总结学习了图片的使用以及Lru算法,今天来学习一下比较优秀的图片缓存开源框架。技术本身就要不断的更迭,从最初的自己使用SoftRe
- 本章是后续学习的基石,只有充分理解了分布式系统的概念和面临的问题,以及ZooKeeper内部的概念,才能懂得ZooKeeper是如何对分布式
- 前言总是觉得对HashMap很熟悉,但最近连续被问到几个关于它的问题,才发现它其实并不简单。这里对关于它的一些问题做个总结,也希望能够大家一
- 前言 OFD是国家标准版式文档格式,于2016年生效。OFD文档国家标准参见《电子文件存储与交换格式版式文档》。既然是国家标准,O
- 最近在开发一个项目,需要写一个后管系统,Bootstrap是美国Twitter公司的设计师Mark Otto和Jacob Thornton合
- Springboot导入本地jar后 打包依赖无法加入1.本地jar包2.pom文件添加自定义jar包导入<!--自定义jar--&g
- 一、常见游戏规则从扑克中每次取出4张牌。使用加减乘除,第一个能得出24者为赢。(其中,J代表11,Q代表12,K代表13,A代表1),按照要
- 前言Java泛型这个特性是从JDK 1.5才开始加入的,因此为了兼容之前的版本,Java泛型的实现采取了“伪泛型”的策略,即Java在语法上
- HashMap的get()方法的NullPointerException今天写代码发现一个 bug,HashMap的 get() 方法一直报
- 加密代码using System;using System.IO;using System.Security.Cryptography;pu
- 本文实例讲述了Java基本数据类型与类型转换。分享给大家供大家参考,具体如下:相关内容:基本数据类型整型浮点型字符型布尔型数据类型转换数组首
- 前言在前后端分离开发的时候我们需要用到参数校验,前端需要进行参数校验,后端接口同样的也需要,以防传入不合法的数据。1、首先还是先导包,导入p
- 本文介绍如何通过C#程序代码方法将XML文件转换为Word文档,包括转为.doc /.docx等格式。并附VB.NET代码,有需要可供参考。
- 大致流程客户端根据远程服务的地址,客户端发送请求至服务端,服务端解析信息并找到对应的实现类,进行方法调用,之后将调用结果原路返回,客户端解析
- 1. 介绍这个项目让你可以去读取并解析一个PDF文件,并将其内部结构展示出来. PDF文件的格式标准文档可以从Adobe那儿获取到. 这个项
- 读写锁:分为读锁和写锁,多个读锁不互斥,读锁与写锁互斥,这是由jvm自己控制的,你只要上好相应的锁即可。如果你的代码只读数据,可以很多人同时