场景
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之后
注:
博客主页:
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 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轴刻度的最大和最小值,然后刷新曲线图。
最后将此右键菜单项添加到右键中去。