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
{
///
/// ZedGraph通用操作类,封装各种常见操作方法 Leomon
///
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);
///
/// 在ZedGraphControl上添加文字,默认使用宋体9号黑色字体
///
/// 要绘出的文字
/// 绘制文字的左上起始点x坐标
/// 绘制文字的左上起始点y坐标
/// ZedGraphControl
/// 控件的标签名
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);
}
///
/// 在ZedGraphControl上添加不显示的文字
///
/// 要绘出的文字
/// 绘制文字的左上起始点x坐标
/// 绘制文字的左上起始点y坐标
/// ZedGraphControl
/// 控件的标签名
/// /// 是否显示
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);
}
///
/// 在ZedGraphControl上添加文字,默认使用宋体9号黑色字体
///
/// 要绘出的文字
/// 绘制文字的左上起始点x坐标
/// 绘制文字的左上起始点y坐标
/// ZedGraphControl
/// 字体大小
/// 控件的标签名
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);
}
///
/// 在ZedGraphControl Chart上添加文字,默认使用宋体5号黑色字体
///
/// 要绘出的文字
/// 绘制文字的左上起始点x坐标
/// 绘制文字的左上起始点y坐标
/// ZedGraphControl
/// 自定义的字体设置
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);
}
///
/// 在ZedGraphControl上添加文字,默认使用宋体9号黑色字体
///
/// 要绘出的文字
/// 绘制文字的左上起始点x坐标
/// 绘制文字的左上起始点y坐标
/// ZedGraphControl
/// 控件的标签名
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);
}
///
/// 画出带标签的线
///
/// 起点X坐标
/// 起点Y坐标
/// 终点X坐标
/// 终点Y坐标
/// ZedGraphControl
/// 线标签
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);
}
///
/// 绘制自定义颜色和字体大小的Arrow,判断是否带箭头,并设置标签
///
/// 箭头颜色
/// 箭头大小
/// 起点X坐标
/// 起点Y坐标
/// 终点X坐标
/// 终点Y坐标
/// ZedGraphControl
/// 是否带箭头
/// 标签名
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);
}
}
///
/// 绘制隐藏的箭头控件
///
/// 起点X坐标
/// 起点Y坐标
/// 终点X坐标
/// 终点Y坐标
/// ZedGraphControl
/// 标签名
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);
}
///
/// 在Zedgraph上绘图
///
/// 图片在资源文件中的名称
/// X坐标
/// y坐标
/// 图宽度
/// 图高度
/// ZedGraphControl
/// 标签
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);
}
///
/// 在Zedgraph上绘图
///
/// 已定义好的ImageObj对象
/// ZedGraphControl
public static void DrawImage(ImageObj imgObj, ZedGraphControl zedGraph)
{
zedGraph.MasterPane.GraphObjList.Add(imgObj);
}
///
/// 根据自定义好的TextObj绘制文字
///
/// 已定义的TextObj
/// ZedGraphControl
public static void DrawText(TextObj text, ZedGraphControl zedGraph)
{
zedGraph.MasterPane.GraphObjList.Add(text);
}
///
/// 获取TextObj字体的相对宽度
///
///
///
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;
}
///
/// 获取TextObj字体的相对高度
///
///
///
///
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;
}
///
/// 绘制矩形
///
/// 已定义的BoxObj对象
/// ZedGraphControl
public static void DrawBox(BoxObj box, ZedGraphControl zedGraph)
{
zedGraph.MasterPane.GraphObjList.Add(box);
}
///
/// 绘制曲线,并用制定的图片资源文件填充曲线上的坐标点
///
/// 曲线的显示名称
/// 曲线的点集合,如果只要显示Legend图标,则只要传入一个空的PointPairList即可
/// 曲线和Label的显示颜色
/// 指定图标在资源文件中的名字
/// ZedGraphControl
/// 曲线标签
///
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;
}
///
/// 用图片填充曲线上的坐标点
///
/// 要填充的LineItem
/// 填充的图片文件在资源文件中的名字
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;
}
}
}