場景
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; } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/103522.html
標籤:C#
