Winform中自定义添加ZedGraph右键实现设置所有Y轴刻度的上下限
程序员文章站
2022-04-14 19:58:54
场景 Winforn中实现ZedGraph自定义添加右键菜单项(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100140716 Winform中实现ZedGraph中曲线右键显示为中文: https://blog ......
场景
winforn中实现zedgraph自定义添加右键菜单项(附源码下载):
https://blog.csdn.net/badao_liumang_qizhi/article/details/100140716
winform中实现zedgraph中曲线右键显示为中文:
https://blog.csdn.net/badao_liumang_qizhi/article/details/100115292
在上面两篇文章的基础上,已经实现的zedgraph的右键效果
现在需要根据当前图形的y轴的选项自动追加右键菜单,打开设置y轴上下限的输入面板,进而设置y轴的上下限。
比如当曲线图有两个y轴,那么在右键时就会显示两个设置上下限的菜单。
点击相应的右键菜单时弹出设置上下限的窗口
将上限设置为150之后
注:
博客主页:
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
在初始化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 mnuyaxisuplower = null; for (int i = 0; i < control.graphpane.yaxislist.count; i++) { yaxis yaxis = control.graphpane.yaxislist[i]; mnuyaxisuplower = new toolstripmenuitem(); mnuyaxisuplower.name = "mnuyaxisuplower" + i; mnuyaxisuplower.text = string.format("设置[{0}]上下限", yaxis.title.text); mnuyaxisuplower.click += delegate(object sender, eventargs e) { dialog.frmsetscalemaxmin frmsetscalemaxmin = new frmsetscalemaxmin(mnuyaxisuplower.text, yaxis.scale.max, yaxis.scale.min); dialogresult result = frmsetscalemaxmin.showdialog(); if (result == dialogresult.ok) { yaxis.scale.max = frmsetscalemaxmin.max; yaxis.scale.min = frmsetscalemaxmin.min; //更改上下限后刷新曲线图 control.axischange(); control.refresh(); control.invalidate(); } }; menustrip.items.add(mnuyaxisuplower); } #endregion #endregion #region 汉化上下文菜单中的菜单项 foreach (toolstripmenuitem item in menustrip.items) { switch (item.name) { case "copied_to_clip": item.text = @"复制到剪贴板"; break; case "copy": item.text = @"复制"; item.click += item_click; 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; } } #endregion }
上面代码包含了汉化右键菜单的代码,主要的是上面扩展右键菜单的代码部分
toolstripmenuitem mnuyaxisuplower = null; for (int i = 0; i < control.graphpane.yaxislist.count; i++) { yaxis yaxis = control.graphpane.yaxislist[i]; mnuyaxisuplower = new toolstripmenuitem(); mnuyaxisuplower.name = "mnuyaxisuplower" + i; mnuyaxisuplower.text = string.format("设置[{0}]上下限", yaxis.title.text); mnuyaxisuplower.click += delegate(object sender, eventargs e) { dialog.frmsetscalemaxmin frmsetscalemaxmin = new frmsetscalemaxmin(mnuyaxisuplower.text, yaxis.scale.max, yaxis.scale.min); dialogresult result = frmsetscalemaxmin.showdialog(); if (result == dialogresult.ok) { yaxis.scale.max = frmsetscalemaxmin.max; yaxis.scale.min = frmsetscalemaxmin.min; //更改上下限后刷新曲线图 control.axischange(); control.refresh(); control.invalidate(); } }; menustrip.items.add(mnuyaxisuplower); }
注:
首先循环所有的y轴,并通过yaxis yaxis = control.graphpane.yaxislist[i]获取y轴对象。然后设置菜单选项的名称、文本和点击事件。
在点击事件中弹出设置上下限窗口
窗口布局设置如下
然后修改窗体代码为
public partial class frmsetscalemaxmin : devexpress.xtraeditors.xtraform { #region 字段定义 private string _title = string.empty; //保存要设置的坐标标题 private double _orignalmax = 0.0; //保存原坐标上限 private double _orignalmin = 0.0; //保存原坐标下限 #endregion #region 构造方法 public frmsetscalemaxmin(string title, double orignalmax, double orignalmin) { this._title = title; this._orignalmax = orignalmax; this._orignalmin = orignalmin; initializecomponent(); } #endregion #region 属性定义 /// <summary> /// 设置后的坐标上限 /// </summary> public double max { get { double value = 0.0; double.tryparse(this.txtmax.editvalue.tostring(), out value); return value; } } /// <summary> /// 设置后的坐标下限 /// </summary> public double min { get { double value = 0.0; double.tryparse(this.txtmin.editvalue.tostring(), out value); return value; } } #endregion #region 事件处理 /// <summary> /// 窗体加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void frmsetscalemaxmin_load(object sender, eventargs e) { this.text = this._title; this.txtmax.editvalue = this._orignalmax; this.txtmin.editvalue = this._orignalmin; } /// <summary> /// 确定 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnok_click(object sender, eventargs e) { this.dialogresult = system.windows.forms.dialogresult.ok; } /// <summary> /// 取消 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btncancel_click(object sender, eventargs e) { this.dialogresult = system.windows.forms.dialogresult.cancel; } #endregion }
点击确定后返回结果并设置y轴刻度的最大和最小值,然后刷新曲线图。
最后将此右键菜单项添加到右键中去。