2012-09-14 73 views
1

我對ZedGraph有一點不同的要求。在ZedGraph窗格上點擊繪製線

我想在用戶單擊ZedGraph窗格時在ZedGraph窗格上創建曲線。另外,我還在該窗格上繪製了其他圖。但是我希望每當用戶點擊zedGraph區域時,我們就可以得到用戶點擊的座標,並且在點擊的座標上繪製一條直線。

我用鼠標點擊事件alogn與FindNearestObject方法像下面這樣:

private void zedGraph_RenderedTrack_MouseClick(object sender, EventArgs e) 
    { 
     MouseEventArgs xx = (MouseEventArgs)e; 
     object nearestObject; 
     int index; 
     this.zedGraph_RenderedTrack.GraphPane.FindNearestObject(new PointF(xx.X, xx.Y), this.CreateGraphics(), out nearestObject, out index); 
     if (nearestObject != null) 
     { 
      DrawALine(xx.X, Color.Red, true); 
     } 
    } 

但是使用這個,ZedGraph搜索一些曲線,找到最近的點,然後繪製線,但我想,該線將被繪製在用戶點擊的地方。有什麼方法可以這樣做嗎?

回答

5

您可以嘗試下面的代碼,它將爲鼠標單擊事件繪製一條垂直線。

public Form1() 
    { 
     InitializeComponent(); 
    }   

    PointPairList userClickrList = new PointPairList(); 
    LineItem userClickCurve = new LineItem("userClickCurve"); 

    private void zedGraphControl1_MouseClick(object sender, MouseEventArgs e) 
    { 
     // Create an instance of Graph Pane 
     GraphPane myPane = zedGraphControl1.GraphPane; 

     // x & y variables to store the axis values 
     double xVal; 
     double yVal; 

     // Clear the previous values if any 
     userClickrList.Clear(); 

     myPane.Legend.IsVisible = false; 

     // Use the current mouse locations to get the corresponding 
     // X & Y CO-Ordinates 
     myPane.ReverseTransform(e.Location, out xVal, out yVal); 

     // Create a list using the above x & y values 
     userClickrList.Add(xVal, myPane.YAxis.Scale.Max); 
     userClickrList.Add(xVal, myPane.YAxis.Scale.Min); 

     // Add a curve 
     userClickCurve = myPane.AddCurve(" ", userClickrList, Color.Red, SymbolType.None); 

     zedGraphControl1.Refresh(); 
    } 

enter image description here

你只需要改變userClickList畫出水平線。

Happy Coding ..... :)