场景

Winform中设置ZedGraph鼠标双击获取距离最近曲线上的点的坐标值:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102466406

现在要实现鼠标悬浮时显示距离最近曲线上的点的横纵坐标和X轴和Y轴的标题。

 

 

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

在包含ZedGraph控件的窗体的load方法中执行初始化zedGraph的方法,在初始化的方法中对鼠标悬浮事件重新绑定。

zgc.CursorValueEvent -= zgc_CursorValueEvent;       //显示焦点值事件
zgc.CursorValueEvent += zgc_CursorValueEvent;       //显示焦点值事件

 

然后在显示焦点值事件中

private static string zgc_CursorValueEvent(ZedGraphControl sender, GraphPane pane, Point mousePt)
        {
            //获取ZedGraphControl对象
            ZedGraphControl zgc = sender as ZedGraphControl;
            if (zgc != null)
            {
                //声明曲线对象
                CurveItem nearstCurve;
                int i;
                Double y = 0.0;
                string z = String.Empty;
                string xTitle = String.Empty;
                string yTtile = String.Empty;
                try
                {
                    //获取距离最近的曲线
                    zgc.GraphPane.FindNearestPoint(mousePt, out nearstCurve, out i);
                    if (nearstCurve != null && nearstCurve.Points.Count > i && nearstCurve.Points[i] != null)
                    {
                        //获取举例最近的点的Tag,在生成曲线时使用Tag存储的X轴的信息
                        z = nearstCurve.Points[i].Tag.ToString();
                        //获取当前pane面板的XAxis的标题的文本内容
                        xTitle = zgc.GraphPane.XAxis.Title.Text;
                        //获取当前pane面板的YAxis的标题的文本内容,通过nearstCurve.YAxisIndex获取当前举例最近的曲线所对应的Y轴的Index
                        yTtile = zgc.GraphPane.YAxisList[nearstCurve.YAxisIndex].Title.Text;
                        y = nearstCurve.Points[i].Y;
                    }
                }
                catch(Exception ex)
                {

                }
                return "X-" + xTitle + ": " + z + "  Y-" + yTtile +": "+ y.ToString();
            }
            else
            {
                return String.Empty;
            }
        } 

 

posted on 2019-11-19 13:56  霸道流氓  阅读(872)  评论(0编辑  收藏  举报

Live2D