-
C#教程之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;/// <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);//设置最小宽度为640if (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;}}}}
栏目列表
最新更新
C#基于接口设计三层架构Unity篇
C#线程 入门
C#读取静态类常量属性和值
C# 插件式编程
C# 委托与事件有啥区别?
C#队列学习笔记:队列(Queue)和堆栈(Stack
linq 多表分组左连接查询查询统计
C#队列学习笔记:MSMQ入门一
C# 基础知识系列- 1 数据类型
二、C#入门—基础语法
C# 在Word中添加Latex 数学公式和符号
inncheck命令 – 检查语法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
SqlServer 利用游标批量更新数据
BOS只读状态修改
SQL Server等待事件—PAGEIOLATCH_EX
数据库多行转换为单一列
获取数据表最后最后访问,修改,更新,
计算经历的时间
SQL查询结果自定义排序
修改数据库默认位置
日期简单加或减
从日期获取年,月或日