433 lines
20 KiB
C#
433 lines
20 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Reflection;
|
||
using System.Resources;
|
||
using System.Drawing.Imaging;
|
||
using System.Drawing.Drawing2D;
|
||
using System.Globalization;
|
||
using System.Linq;
|
||
using System.Collections;
|
||
|
||
namespace DrawGraph
|
||
{
|
||
/// <summary>
|
||
/// ZedGraph通用操作类,封装各种常见操作方法 Leomon
|
||
/// </summary>
|
||
public class ZUtil
|
||
{
|
||
public static FontSpec Font14 = new FontSpec("宋体", 14.0f, Color.Black, true, false, false);
|
||
public static FontSpec Font16 = new FontSpec("宋体", 16.0f, Color.Black, true, false, false);
|
||
public static FontSpec Font18 = new FontSpec("宋体", 18.0f, Color.Black, true, false, false);
|
||
|
||
/// <summary>
|
||
/// 在ZedGraphControl上添加文字,默认使用宋体9号黑色字体
|
||
/// </summary>
|
||
/// <param name="letter">要绘出的文字</param>
|
||
/// <param name="x">绘制文字的左上起始点x坐标</param>
|
||
/// <param name="y">绘制文字的左上起始点y坐标</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
/// <param name="tag">控件的标签名</param>
|
||
public static void DrawText(string letter, double x, double y, ZedGraphControl zedGraph, string tag, float fontSize = 8.0f)
|
||
{
|
||
TextObj text = new TextObj(letter, x, y, CoordType.PaneFraction);
|
||
text.FontSpec = new FontSpec("微软雅黑", fontSize, Color.Black, false, false, false);
|
||
text.FontSpec.Border.IsVisible = false;
|
||
text.FontSpec.Fill.IsVisible = false;
|
||
text.Location.AlignH = AlignH.Left;
|
||
text.Location.AlignV = AlignV.Top;
|
||
text.ZOrder = ZOrder.B_BehindLegend;
|
||
text.Tag = tag;
|
||
zedGraph.MasterPane.GraphObjList.Add(text);
|
||
}
|
||
public static void DrawText(string letter, double x, double y, ZedGraphControl zedGraph, string tag, Color fontColor, float fontSize = 8.0f, bool isBold = false)
|
||
{
|
||
TextObj text = new TextObj(letter, x, y, CoordType.PaneFraction);
|
||
text.FontSpec = new FontSpec("微软雅黑", fontSize, fontColor, isBold, false, false);
|
||
text.FontSpec.Border.IsVisible = false;
|
||
text.FontSpec.Fill.IsVisible = false;
|
||
text.Location.AlignH = AlignH.Left;
|
||
text.Location.AlignV = AlignV.Top;
|
||
text.ZOrder = ZOrder.B_BehindLegend;
|
||
text.Tag = tag;
|
||
zedGraph.MasterPane.GraphObjList.Add(text);
|
||
}
|
||
/// <summary>
|
||
/// 在ZedGraphControl上添加不显示的文字
|
||
/// </summary>
|
||
/// <param name="letter">要绘出的文字</param>
|
||
/// <param name="x">绘制文字的左上起始点x坐标</param>
|
||
/// <param name="y">绘制文字的左上起始点y坐标</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
/// <param name="tag">控件的标签名</param>
|
||
/// /// <param name="isShow">是否显示</param>
|
||
public static void DrawText(string letter, double x, double y, ZedGraphControl zedGraph, string tag, bool isShow)
|
||
{
|
||
TextObj text = new TextObj(letter, x, y, CoordType.PaneFraction);
|
||
text.FontSpec = new FontSpec("微软雅黑", 8.0f, Color.Black, false, false, false);
|
||
text.FontSpec.Border.IsVisible = false;
|
||
text.FontSpec.Fill.IsVisible = false;
|
||
text.Location.AlignH = AlignH.Left;
|
||
text.Location.AlignV = AlignV.Top;
|
||
text.ZOrder = ZOrder.B_BehindLegend;
|
||
text.Tag = tag;
|
||
text.IsVisible = isShow;
|
||
zedGraph.MasterPane.GraphObjList.Add(text);
|
||
}
|
||
/// <summary>
|
||
/// 在ZedGraphControl上添加文字,默认使用宋体9号黑色字体
|
||
/// </summary>
|
||
/// <param name="letter">要绘出的文字</param>
|
||
/// <param name="x">绘制文字的左上起始点x坐标</param>
|
||
/// <param name="y">绘制文字的左上起始点y坐标</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
/// <param name="fontSize">字体大小</param>
|
||
/// <param name="tag">控件的标签名</param>
|
||
public static void DrawText(string letter, double x, double y, ZedGraphControl zedGraph, float fontSize, string tag, bool isBorder = false, ZOrder order = ZOrder.B_BehindLegend)
|
||
{
|
||
TextObj text = new TextObj(letter, x, y, CoordType.PaneFraction, AlignH.Left, AlignV.Top);
|
||
text.FontSpec = new FontSpec("微软雅黑", fontSize, Color.DarkBlue, false, false, false);
|
||
text.FontSpec.Border.IsVisible = isBorder;
|
||
text.FontSpec.Fill.IsVisible = isBorder;
|
||
text.FontSpec.StringAlignment = StringAlignment.Near; //加上他文本就左对齐了
|
||
text.FontSpec.Angle = 0;
|
||
text.ZOrder = order;
|
||
text.Tag = tag;
|
||
zedGraph.MasterPane.GraphObjList.Add(text);
|
||
}
|
||
/// <summary>
|
||
/// 在ZedGraphControl Chart上添加文字,默认使用宋体5号黑色字体
|
||
/// </summary>
|
||
/// <param name="letter">要绘出的文字</param>
|
||
/// <param name="x">绘制文字的左上起始点x坐标</param>
|
||
/// <param name="y">绘制文字的左上起始点y坐标</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
/// <param name="font">自定义的字体设置</param>
|
||
public static void DrawText(string letter, double x, double y, ZedGraphControl zedGraph, string tag, float fontSize, bool b)
|
||
{
|
||
TextObj text = new TextObj(letter, (float)x, (float)y);
|
||
text.Location.CoordinateFrame = CoordType.PaneFraction;
|
||
text.FontSpec.Size = fontSize;
|
||
text.FontSpec.IsBold = true;
|
||
text.Location.AlignH = AlignH.Center;
|
||
text.Location.AlignV = AlignV.Top;
|
||
text.FontSpec.Border.IsVisible = false;
|
||
text.FontSpec.Fill.IsVisible = b;
|
||
text.ZOrder = ZOrder.A_InFront;
|
||
text.Tag = tag;
|
||
zedGraph.MasterPane.GraphObjList.Add(text);
|
||
}
|
||
/// <summary>
|
||
/// 在ZedGraphControl上添加文字,默认使用宋体9号黑色字体
|
||
/// </summary>
|
||
/// <param name="letter">要绘出的文字</param>
|
||
/// <param name="x">绘制文字的左上起始点x坐标</param>
|
||
/// <param name="y">绘制文字的左上起始点y坐标</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
/// <param name="tag">控件的标签名</param>
|
||
public static void DrawDrugText(string letter, double x, double y, ZedGraphControl zedGraph, string tag, Color color, float fontSize = 8.0f)
|
||
{
|
||
TextObj text = new TextObj(letter, x, y, CoordType.PaneFraction);
|
||
text.FontSpec = new FontSpec("微软雅黑", fontSize, color, false, false, false);
|
||
text.FontSpec.Border.IsVisible = false;
|
||
text.FontSpec.Fill.IsVisible = true;
|
||
text.Location.AlignH = AlignH.Left;
|
||
text.Location.AlignV = AlignV.Top;
|
||
text.ZOrder = ZOrder.B_BehindLegend;
|
||
text.Tag = tag;
|
||
zedGraph.MasterPane.GraphObjList.Add(text);
|
||
}
|
||
|
||
public static void DrawText(string letter, double x, double y, ZedGraphControl zedGraph, FontSpec font)
|
||
{
|
||
TextObj text = new TextObj(letter, x, y, CoordType.PaneFraction);
|
||
text.FontSpec = font;
|
||
text.FontSpec.Border.IsVisible = false;
|
||
text.FontSpec.Fill.IsVisible = false;
|
||
text.Location.AlignH = AlignH.Left;
|
||
text.Location.AlignV = AlignV.Top;
|
||
text.ZOrder = ZOrder.A_InFront;
|
||
zedGraph.MasterPane.GraphObjList.Add(text);
|
||
}
|
||
|
||
public static void DrawLine(double x1, double y1, double x2, double y2, ZedGraphControl zedGraph, string tag, Color LineColor)
|
||
{
|
||
LineObj lineObj = new LineObj(LineColor, x1, y1, x2, y2);
|
||
lineObj.IsVisible = true;
|
||
lineObj.ZOrder = ZOrder.A_InFront;
|
||
lineObj.Location.AlignH = AlignH.Left;
|
||
lineObj.Location.AlignV = AlignV.Top;
|
||
lineObj.Location.CoordinateFrame = CoordType.PaneFraction;
|
||
lineObj.Tag = tag;
|
||
zedGraph.MasterPane.GraphObjList.Add(lineObj);
|
||
}
|
||
/// <summary>
|
||
/// 画出带标签的线
|
||
/// </summary>
|
||
/// <param name="x1">起点X坐标</param>
|
||
/// <param name="y1">起点Y坐标</param>
|
||
/// <param name="x2">终点X坐标</param>
|
||
/// <param name="y2">终点Y坐标</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
/// <param name="tag">线标签</param>
|
||
public static void DrawLine(double x1, double y1, double x2, double y2, ZedGraphControl zedGraph, string tag, Color LineColor, DashStyle style = DashStyle.Solid)
|
||
{
|
||
LineObj line = new LineObj(LineColor, x1, y1, x2, y2);
|
||
line.IsVisible = true;
|
||
line.ZOrder = ZOrder.B_BehindLegend;
|
||
line.Location.AlignH = AlignH.Left;
|
||
line.Location.AlignV = AlignV.Top;
|
||
line.Location.CoordinateFrame = CoordType.PaneFraction;
|
||
line.Tag = tag;
|
||
line.Line.Style = style;
|
||
zedGraph.MasterPane.GraphObjList.Add(line);
|
||
}
|
||
|
||
/////////////////////////////////////////////////////////////////////////
|
||
public static void DrawLineWidth(double x1, double y1, double x2, double y2, ZedGraphControl zedGraph, string tag, Color LineColor, int width = 1)
|
||
{
|
||
LineObj line = new LineObj(LineColor, x1, y1, x2, y2);
|
||
line.IsVisible = true;
|
||
line.ZOrder = ZOrder.A_InFront;
|
||
line.Location.AlignH = AlignH.Left;
|
||
line.Location.AlignV = AlignV.Top;
|
||
line.Location.CoordinateFrame = CoordType.PaneFraction;
|
||
line.Tag = tag;
|
||
line.Line.Style = DashStyle.Solid;
|
||
line.Line.Width = width;
|
||
zedGraph.MasterPane.GraphObjList.Add(line);
|
||
}
|
||
|
||
public static void DrawLine2(double x1, double y1, double x2, double y2, ZedGraphControl zedGraph, string tag, Color LineColor)
|
||
{
|
||
LineObj line = new LineObj(LineColor, x1, y1, x2, y2);
|
||
line.Line.Width = 1.5f;
|
||
line.IsVisible = true;
|
||
line.ZOrder = ZOrder.A_InFront;
|
||
line.Location.AlignH = AlignH.Left;
|
||
line.Location.AlignV = AlignV.Top;
|
||
line.Location.CoordinateFrame = CoordType.PaneFraction;
|
||
line.Tag = tag;
|
||
line.Line.Style = DashStyle.Solid;
|
||
zedGraph.MasterPane.GraphObjList.Add(line);
|
||
}
|
||
/// <summary>
|
||
/// 绘制自定义颜色和字体大小的Arrow,判断是否带箭头,并设置标签
|
||
/// </summary>
|
||
/// <param name="c">箭头颜色</param>
|
||
/// <param name="fontSize">箭头大小</param>
|
||
/// <param name="x1">起点X坐标</param>
|
||
/// <param name="y1">起点Y坐标</param>
|
||
/// <param name="x2">终点X坐标</param>
|
||
/// <param name="y2">终点Y坐标</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
/// <param name="hasArrowHead">是否带箭头</param>
|
||
/// <param name="tag">标签名</param>
|
||
public static void DrawArrow(Color c, float fontSize, double x1, double y1, double x2, double y2, ZedGraphControl zedGraph, bool hasArrowHead, string tag, bool IsArrowHead)
|
||
{
|
||
ArrowObj arrow = new ArrowObj(c, 7f, x1, y1, x2, y2);
|
||
arrow.Line.Width = 1.5f;
|
||
arrow.ZOrder = ZOrder.A_InFront;
|
||
arrow.Location.AlignH = AlignH.Left;
|
||
arrow.Location.AlignV = AlignV.Top;
|
||
arrow.IsArrowHead = IsArrowHead;
|
||
arrow.Location.CoordinateFrame = CoordType.PaneFraction;
|
||
arrow.ZOrder = ZOrder.B_BehindLegend;
|
||
arrow.Tag = tag;
|
||
zedGraph.MasterPane.GraphObjList.Add(arrow);
|
||
|
||
if (x1 != x2 && arrow.IsArrowHead == false)
|
||
{
|
||
DrawLine2(x1, y1 + 0.004, x1, y1 - 0.004, zedGraph, "A" + tag, c);
|
||
DrawLine2(x2, y1 + 0.004, x2, y2 - 0.004, zedGraph, "B" + tag, c);
|
||
}
|
||
else
|
||
{
|
||
DrawLine2(x1, y1 + 0.004, x1, y1 - 0.004, zedGraph, "A" + tag, c);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 绘制隐藏的箭头控件
|
||
/// </summary>
|
||
/// <param name="x1">起点X坐标</param>
|
||
/// <param name="y1">起点Y坐标</param>
|
||
/// <param name="x2">终点X坐标</param>
|
||
/// <param name="y2">终点Y坐标</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
/// <param name="tag">标签名</param>
|
||
public static void DrawArrow(double x1, double y1, double x2, double y2, ZedGraphControl zedGraph, string tag)
|
||
{
|
||
ArrowObj arrow = new ArrowObj(Color.Blue, 6.0f, x1, y1, x2, y2);
|
||
arrow.ZOrder = ZOrder.A_InFront;
|
||
arrow.Location.AlignH = AlignH.Left;
|
||
arrow.Location.AlignV = AlignV.Top;
|
||
arrow.Location.CoordinateFrame = CoordType.PaneFraction;
|
||
arrow.ZOrder = ZOrder.B_BehindLegend;
|
||
arrow.Tag = tag;
|
||
arrow.IsVisible = false;
|
||
zedGraph.MasterPane.GraphObjList.Add(arrow);
|
||
}
|
||
|
||
public static void DrawTextRemark(string letter, double x, double y, ZedGraphControl zedGraph, float fontSize, string tag, bool isBorder = false, ZOrder order = ZOrder.B_BehindLegend)
|
||
{
|
||
TextObj text = new TextObj(letter, x, y, CoordType.PaneFraction, AlignH.Left, AlignV.Top);
|
||
text.FontSpec = new FontSpec("微软雅黑", fontSize, Color.Black, false, false, false);
|
||
text.FontSpec.Border.IsVisible = isBorder;
|
||
text.FontSpec.Fill.IsVisible = isBorder;
|
||
text.FontSpec.StringAlignment = StringAlignment.Near; //加上他文本就左对齐了
|
||
text.FontSpec.Angle = 0;
|
||
text.ZOrder = order;
|
||
text.Tag = tag;
|
||
zedGraph.MasterPane.GraphObjList.Add(text);
|
||
}
|
||
/// <summary>
|
||
/// 在Zedgraph上绘图
|
||
/// </summary>
|
||
/// <param name="image">图片在资源文件中的名称</param>
|
||
/// <param name="left">X坐标</param>
|
||
/// <param name="top">y坐标</param>
|
||
/// <param name="width">图宽度</param>
|
||
/// <param name="height">图高度</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
/// <param name="tag">标签</param>
|
||
public static void DrawImage(string imageResName, double left, double top, double width, double height, ZedGraphControl zedGraph, string tag)
|
||
{
|
||
ImageObj imgObj = new ImageObj(new ZUtil().getImage(imageResName), left, top, width, height);
|
||
imgObj.IsVisible = true;
|
||
imgObj.ZOrder = ZOrder.A_InFront;
|
||
imgObj.Location.CoordinateFrame = CoordType.PaneFraction;
|
||
imgObj.Location.AlignH = AlignH.Left;
|
||
imgObj.Location.AlignV = AlignV.Top;
|
||
imgObj.IsVisible = true;
|
||
imgObj.ZOrder = ZOrder.A_InFront;
|
||
imgObj.Tag = tag;
|
||
zedGraph.MasterPane.GraphObjList.Add(imgObj);
|
||
}
|
||
public static void DrawImage(Image imageResName, double left, double top, double width, double height, ZedGraphControl zedGraph, string tag)
|
||
{
|
||
ImageObj imgObj = new ImageObj(imageResName, left, top, width, height);
|
||
imgObj.IsVisible = true;
|
||
imgObj.ZOrder = ZOrder.A_InFront;
|
||
imgObj.Location.CoordinateFrame = CoordType.PaneFraction;
|
||
imgObj.Location.AlignH = AlignH.Left;
|
||
imgObj.Location.AlignV = AlignV.Top;
|
||
imgObj.IsVisible = true;
|
||
imgObj.ZOrder = ZOrder.A_InFront;
|
||
imgObj.Tag = tag;
|
||
zedGraph.MasterPane.GraphObjList.Add(imgObj);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 在Zedgraph上绘图
|
||
/// </summary>
|
||
/// <param name="imgObj">已定义好的ImageObj对象</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
public static void DrawImage(ImageObj imgObj, ZedGraphControl zedGraph)
|
||
{
|
||
zedGraph.MasterPane.GraphObjList.Add(imgObj);
|
||
}
|
||
/// <summary>
|
||
/// 根据自定义好的TextObj绘制文字
|
||
/// </summary>
|
||
/// <param name="text">已定义的TextObj</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
public static void DrawText(TextObj text, ZedGraphControl zedGraph)
|
||
{
|
||
zedGraph.MasterPane.GraphObjList.Add(text);
|
||
}
|
||
/// <summary>
|
||
/// 获取TextObj字体的相对宽度
|
||
/// </summary>
|
||
/// <param name="text"></param>
|
||
/// <param name="zgc"></param>
|
||
public static float GetColWidthFraction(TextObj text, ZedGraphControl zgc)
|
||
{
|
||
GraphPane pane = zgc.GraphPane;
|
||
float ratio = pane.CalcScaleFactor();
|
||
Graphics g = zgc.CreateGraphics();
|
||
float absoluteWidth = text.FontSpec.GetWidth(g, text.Text, ratio);
|
||
float paneWidth = pane.Rect.Width;
|
||
return absoluteWidth / paneWidth;
|
||
}
|
||
/// <summary>
|
||
/// 获取TextObj字体的相对高度
|
||
/// </summary>
|
||
/// <param name="oTxt"></param>
|
||
/// <param name="baseZed"></param>
|
||
/// <returns></returns>
|
||
public static float GetColHeightFraction(TextObj text, ZedGraphControl zgc)
|
||
{
|
||
GraphPane pane = zgc.GraphPane;
|
||
float ratio = pane.CalcScaleFactor();
|
||
Graphics g = zgc.CreateGraphics();
|
||
float absoluteHeight = text.FontSpec.GetHeight(ratio);
|
||
float paneHeight = pane.Rect.Height;
|
||
return absoluteHeight / paneHeight;
|
||
}
|
||
/// <summary>
|
||
/// 绘制矩形
|
||
/// </summary>
|
||
/// <param name="box">已定义的BoxObj对象</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
public static void DrawBox(BoxObj box, ZedGraphControl zedGraph)
|
||
{
|
||
zedGraph.MasterPane.GraphObjList.Add(box);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 绘制曲线,并用制定的图片资源文件填充曲线上的坐标点
|
||
/// </summary>
|
||
/// <param name="label">曲线的显示名称</param>
|
||
/// <param name="pointList">曲线的点集合,如果只要显示Legend图标,则只要传入一个空的PointPairList即可</param>
|
||
/// <param name="color">曲线和Label的显示颜色</param>
|
||
/// <param name="symbolResName">指定图标在资源文件中的名字</param>
|
||
/// <param name="zedGraph">ZedGraphControl</param>
|
||
/// <param name="tag">曲线标签</param>
|
||
/// <returns></returns>
|
||
public static LineItem AddCurve(string label, IPointList pointList, Color color, string symbolResName, bool hasLine, ZedGraphControl zedGraph, string tag,float SymbolSize)
|
||
{
|
||
LineItem curve = zedGraph.GraphPane.AddCurve(label, pointList, color, SymbolType.None);
|
||
if (!hasLine)
|
||
{
|
||
curve.Line.IsVisible = false;
|
||
}
|
||
curve.Tag = tag;
|
||
new ZUtil().FillCurveSymbol(curve, symbolResName,SymbolSize);
|
||
return curve;
|
||
}
|
||
/// <summary>
|
||
/// 用图片填充曲线上的坐标点
|
||
/// </summary>
|
||
/// <param name="curve">要填充的LineItem</param>
|
||
/// <param name="symbolResName">填充的图片文件在资源文件中的名字</param>
|
||
public void FillCurveSymbol(LineItem curve, string symbolResName,float SymbolSize)
|
||
{
|
||
curve.Symbol.Type = SymbolType.Circle;
|
||
curve.Symbol.Size = SymbolSize;
|
||
curve.Symbol.Border.IsVisible = false;
|
||
|
||
curve.Symbol.Fill = new Fill(getImage(symbolResName), System.Drawing.Drawing2D.WrapMode.Clamp);
|
||
|
||
}
|
||
public Image getImage(string key)
|
||
{
|
||
Image image = null;
|
||
string backimgpath = "DrawGraph.Resources." + key + ".png";
|
||
Assembly assem = this.GetType().Assembly;
|
||
foreach (string resourceName in assem.GetManifestResourceNames())
|
||
{
|
||
if (backimgpath.ToLower() == resourceName.ToLower())
|
||
{
|
||
Stream stream = assem.GetManifestResourceStream(backimgpath);
|
||
image = new Bitmap(stream);
|
||
}
|
||
}
|
||
return image;
|
||
}
|
||
|
||
|
||
}
|
||
}
|