AIMS/DrawGraph/Graph/ZUtil.cs
leomon 2c7f4cdab2 器械单默认打印左斜杠
器械清点单应用调试
2022-11-16 23:11:59 +08:00

337 lines
16 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <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);
}
/// <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, 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);
}
public static void DrawLine1(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;
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);
}
public static void DrawLine(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 = 2;
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 = 2;
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)
{
DrawLine(x1, y1 + 0.004, x1, y1 - 0.004, zedGraph, "A" + tag, c);
DrawLine(x2, y1 + 0.004, x2, y2 - 0.004, zedGraph, "B" + tag, c);
}
else
{
DrawLine(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>
/// 绘制矩形
/// </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)
{
LineItem curve = zedGraph.GraphPane.AddCurve(label, pointList, color, SymbolType.None);
if (!hasLine)
{
curve.Line.IsVisible = false;
}
curve.Tag = tag;
new ZUtil().FillCurveSymbol(curve, symbolResName);
return curve;
}
/// <summary>
/// 用图片填充曲线上的坐标点
/// </summary>
/// <param name="curve">要填充的LineItem</param>
/// <param name="symbolResName">填充的图片文件在资源文件中的名字</param>
public void FillCurveSymbol(LineItem curve, string symbolResName)
{
curve.Symbol.Type = SymbolType.Circle;
curve.Symbol.Size = 6;
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;
}
}
}