Winform中設置ZedGraph鼠標懸浮顯示舉例最近曲線上的點的座標值和X軸與Y軸的標題

場景

Winform中設置ZedGraph鼠標雙擊獲取距離最近曲線上的點的座標值:編程

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

如今要實現鼠標懸浮時顯示距離最近曲線上的點的橫縱座標和X軸和Y軸的標題。.net

 

 

注:rest

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公衆號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。 code

實現

在包含ZedGraph控件的窗體的load方法中執行初始化zedGraph的方法,在初始化的方法中對鼠標懸浮事件從新綁定。 orm

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;
            }
        } 
相關文章
相關標籤/搜索