场景
Winforn中设置ZedGraph曲线图的属性、坐标轴属性、刻度属性:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100112573
当ZedGraph的图形渲染完成后,添加设置所有曲线颜色功能。
注:
博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
首先在ZedGraph的右键菜单中添加设置曲线颜色选项,在面板初始化的方法中订阅右键事件。
zgc.ContextMenuBuilder -= zgc_ContextMenuBuilder; //上下文菜单生成事件订阅 zgc.ContextMenuBuilder += zgc_ContextMenuBuilder; //上下文菜单生成事件订阅
然后在订阅的方法中
private static void zgc_ContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip, Point mousePt, ZedGraphControl.ContextMenuObjectState objState) { #region 扩展上下文菜单 #region 图形选项菜单 ToolStripMenuItem mnuChartOption = new ToolStripMenuItem(); //新建菜单项对象 mnuChartOption.Name = "set_curve_color"; mnuChartOption.Text = "设置曲线颜色"; //点击弹出图形选项对话框 mnuChartOption.Click += delegate(object sender, EventArgs e) { if (curveModelList == null || curveModelList.Count == 0) { XtraMessageBox.Show("当前没有曲线"); } else { FrmSetCurveColor setCurveColor = new FrmSetCurveColor(); DialogResult result = setCurveColor.ShowDialog(); //显示Dialog if (result == DialogResult.OK) { TriggerSetCurveColor(); XtraMessageBox.Show("设置曲线颜色成功"); } } }; menuStrip.Items.Add(mnuChartOption); //把图形选项对话框添加到上下文菜单中 #endregion #endregion #region 汉化上下文菜单中的菜单项 foreach (ToolStripMenuItem item in menuStrip.Items) { switch (item.Name) { case "copied_to_clip": item.Text = @"复制到剪贴板"; break; case "copy": item.Text = @"复制"; break; case "page_setup": item.Text = @"页面设置..."; break; case "print": item.Text = @"打印..."; break; case "save_as": item.Text = @"另存图表..."; break; case "set_default": item.Text = @"恢复默认大小"; break; case "show_val": item.Text = @"显示节点数值"; break; case "title_def": item.Text = @"标题"; break; case "undo_all": item.Text = @"还原所有缩放"; break; case "unpan": item.Text = @"还原上一步缩放"; break; case "unzoom": item.Text = @"还原缩放"; break; case "set_curve_color": item.Text = @"设置曲线颜色"; break; } } #endregion }
此时会汉化右键菜单并添加右键选项,并且其点击事件是打开设置曲线颜色面板。
在设置曲线颜色面板的代码中,在其load方法中调用初始化面板的方法,
此时要根据传递的曲线的Model的list去生成当前已有曲线的设置的panel。
那么当前已经有多少个曲线是如何传递的?
在前面ZedGraph生成曲线的地方所在的工具类中声明一个全局的list
public static List<CurveModel> curveModelList= new List<CurveModel>();
其中CurveModel是自定义的存取曲线属性的Model类
public class CurveModel { /// <summary> /// 曲线序号 /// </summary> private int curveIndex; public int CurveIndex { get { return curveIndex; } set { curveIndex = value; } } /// <summary> /// 源文件 /// </summary> private string originFile; public string OriginFile { get { return originFile; } set { originFile = value; } } /// <summary> /// 源文件完整路径 /// </summary> private string originFileFullPath; public string OriginFileFullPath { get { return originFileFullPath; } set { originFileFullPath = value; } } /// <summary> /// 属性名 /// </summary> private string attributeName; public string AttributeName { get { return attributeName; } set { attributeName = value; } } /// <summary> /// 颜色 /// </summary> private string color; public string Color { get { return color; } set { color = value; } } }
声明完list后,其中生成曲线的地方也在这个工具类中
curveModelList.Clear(); int curveIndex = 0; for (int k = 0; k < compTestDataList.Count; k++) { //循环添加曲线 for (int i = 0; i < yList.Count; i++) { SymbolType symbolType = GetCurveSimple(yList[i].CurveType); //根据配置文件中曲线符号类型返回曲线符号标识 list = SetCurveText(interval, xAttribute.TitleKey, yList[i].TitleKey, compTestDataList[k]); LineItem myCurve = myPane.AddCurve(yList[i].Title, list, System.Drawing.ColorTranslator.FromHtml(yList[i].Color), symbolType); myCurve = SetCurveType(myCurve,symbolType, yList[i].Type, yList[i].Color); //根据配置文件设置曲线类型 myCurve.YAxisIndex = i; //很关键,对应使用那个坐标值 CurveModel curveModel = new CurveModel(); string originFile = compTestDataList[k].ThisDataFile; curveModel.OriginFile = originFile.Substring(originFile.LastIndexOf("\") + 1); curveModel.CurveIndex = curveIndex; curveModel.OriginFileFullPath = originFile; curveModel.AttributeName = yList[i].Title; curveModel.Color = yList[i].Color; curveModelList.Add(curveModel); curveIndex++; } }
这样在生成曲线时就将当前图形的所有曲线的属性存到一个工具类中全局的一个list中,然后在设置曲线颜色面板中获取这个list。
private void FrmSetCurveColor_Load(object sender, EventArgs e) { curveModelList.Clear(); curveModelList = ChartCompareHelper.curveModelList; initSetCurveColorPane(curveModelList); }
然后在使用这个list去初始化设置颜色的面板
private void initSetCurveColorPane(List<CurveModel> curveModelList) { int panelControlHeight = 20; int panelControlWidth = 50; foreach(CurveModel curve in curveModelList) { PanelControl panelControl = new PanelControl(); #region 生成一个Label标识X轴 //生成一个Label标识X轴 LabelControl label = new LabelControl(); label.Text = "第" + (curve.CurveIndex + 1).ToString() + "条曲线(" + curve.OriginFile + "的" + curve.AttributeName + ")的颜色为:"; label.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE, Global.GROUP_VERTICAL_DISTANCE); label.Parent = panelControl; #endregion #region 生成一个颜色选择器 //添加颜色选择器 ColorEdit colorEditX = new ColorEdit(); colorEditX.Text = curve.Color; colorEditX.Width = Global.CHART_OPTION_WIDTH; colorEditX.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE * 2 + label.Width, Global.GROUP_VERTICAL_DISTANCE); colorEditX.Parent = panelControl; colorEditX.Name = "colorEdit"+curve.CurveIndex; #endregion #region 设置X轴Panel的位置以及属性 //设置panel的宽度 panelControl.Width = label.Width + colorEditX.Width + Global.NOGROUP_HORIZONTAL_DISTANCE * 3; //设置panel的高度 panelControl.Height = label.Height + Global.GROUP_VERTICAL_DISTANCE * 2; panelControlHeight = panelControl.Height; panelControlWidth = panelControl.Width; //设置panel的位置 panelControl.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE, (panelControl.Height * curve.CurveIndex) + (Global.GROUP_VERTICAL_DISTANCE *(curve.CurveIndex + 1))); //设置Name panelControl.Name = "panelControl" + curve.CurveIndex; //将panel添加到当前窗体 this.Controls.Add(panelControl); #endregion } this.Width = panelControlWidth + Global.NOGROUP_HORIZONTAL_DISTANCE * 4; this.Height = (curveModelList.Count + 1) * panelControlHeight + (curveModelList.Count + 2) * Global.GROUP_VERTICAL_DISTANCE + +this.panelControl1.Height; }
然后设置颜色后,点击保存后在确定按钮的点击事件中
private void simpleButton2_Click_1(object sender, EventArgs e) { for (int i = 0; i < curveModelList.Count; i++) { ColorEdit colorEditY = this.Controls.Find("colorEdit" + i, true)[0] as ColorEdit; curveModelList[i].Color = colorEditY.Text; } this.DialogResult = System.Windows.Forms.DialogResult.OK; }
再将设置的颜色属性赋值给当前设置颜色面板作用域的list
List<CurveModel> curveModelList = new List<CurveModel>();
此时窗体返回OK,再回到右键打开窗口的地方
if (result == DialogResult.OK) { TriggerSetCurveColor(); XtraMessageBox.Show("设置曲线颜色成功"); }
此时会触发设置曲线颜色的方法
public static void TriggerSetCurveColor() { if (OnSetCurveColor != null) { OnSetCurveColor(null, System.EventArgs.Empty); } }
此事件声明
public static event EventHandler OnSetCurveColor;
事件会在图形所在的页面进行订阅
ChartCompareHelper.OnSetCurveColor + = ChartCompareHelper_OnSetCurveColor;
订阅执行的方法
private void ChartCompareHelper_OnSetCurveColor(object sender, EventArgs e) { ChartCompareHelper.SetCurveColor(this.zedGraphControl1); }
此方法传递当前ZedGraph对象
public static void SetCurveColor(ZedGraphControl zedGraphControl) { for (int i = 0; i < curveModelList.Count;i++) { zedGraphControl.GraphPane.CurveList[i].Color = System.Drawing.ColorTranslator.FromHtml(curveModelList[i].Color); } zedGraphControl.GraphPane.AxisChange(); zedGraphControl.Refresh(); zedGraphControl.Invalidate(); }
然后按顺序设置曲线颜色。