ZedGraph怎樣在雙擊圖形後添加箭頭標記

場景

在ZedGraph的曲線圖上,雙擊圖時會在圖形上生成箭頭符號標記。編程

效果

 

 

注:spa

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

實現

首先在ZedGraph所在的窗體的load事件中對ZedGraph的鼠標雙擊事件進行重寫rest

    zgc.DoubleClickEvent -= zgc_DoubleClickEvent;       //鼠標雙擊事件訂閱
    zgc.DoubleClickEvent += zgc_DoubleClickEvent;       //鼠標雙擊事件訂閱

而後在具體訂閱的事件實現中code

private static bool zgc_DoubleClickEvent(ZedGraphControl sender, MouseEventArgs e)
        {
            //獲取(ZedGraphControl 對象
            ZedGraphControl zgc = sender as ZedGraphControl;
            if (zgc != null)
            {
                try
                {
      //獲取鼠標焦點距離最近的點所在的曲線以及在曲線上的點的索引
                    PointF mousePt = new PointF(e.X, e.Y);
                    CurveItem nearstCurve;
                    int i;
                    zgc.GraphPane.FindNearestPoint(mousePt, out nearstCurve, out i);
                    if (nearstCurve != null && nearstCurve.Points[i] != null)
                    {
                        //獲取鼠標焦點距離最近的點的座標
                        Double x = nearstCurve.Points[i].X;
                        Double y = nearstCurve.Points[i].Y;
                        string title = nearstCurve.Points[i].Tag as string;

                        
                        #region 添加箭頭標記

                        ArrowObj myArrow = new ArrowObj(Color.FromArgb(0xA0, 0x00, 0x00), 20, i + 1, zgc.GraphPane.YAxisList[0].Scale.Min, i + 1,

zgc.GraphPane.YAxisList[0].Scale.Max);
                        myArrow.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
                        myArrow.Line.DashOff = 1;
                        myArrow.Line.DashOn = 1;
                        myArrow.ZOrder = ZOrder.B_BehindLegend;

                        zgc.GraphPane.GraphObjList.Clear();
                        zgc.GraphPane.GraphObjList.Add(myArrow);

                        #endregion
                    }
                    return true;
                }
                catch(Exception ex)
                {
                    ICSharpCode.Core.LoggingService<DataChartHelper>.Error("zgc_DoubleClickEvent exception:" + ex.Message, ex);
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

 

注:對象

首先獲取距離鼠標焦點最近的點以及所在的曲線。blog

而後根據距離最近的點的橫縱座標使用ArrowObj 對象生成線。教程

ArrowObj :表示圖形上的圖形箭頭或線對象的類。ArrowObj對象的列表由GraphObjList集合類維護。索引

ArrowObj 的構造方法爲:事件

 

 

第一個參數爲顏色對象,第二個參數爲大小,後面四個參數用於定位,兩點肯定一條直線,兩個座標肯定一個點,因此是四個座標肯定一條線。

前兩個肯定一個點,後兩個肯定一個點。

在代碼中使用的是:

ArrowObj myArrow = new ArrowObj(Color.FromArgb(0xA0, 0x00, 0x00), 20, i + 1, zgc.GraphPane.YAxisList[0].Scale.Min, i + 1,

zgc.GraphPane.YAxisList[0].Scale.Max);

第一個點是在X軸上點,其座標爲上面距離鼠標焦點最近的點的橫座標+1,Y軸刻度的最小值。

第二個點座標橫座標同樣,縱座標是Y軸刻度的最大值。

相關文章
相關標籤/搜索