2016-02-02 27 views
1

我在.NET 4.0 WinForms圖表控件中有一個X-Y圖。我嘗試實現橡皮筋選擇,以便用戶可以單擊並拖動鼠標在圖上創建一個矩形,從而選擇位於此矩形內的所有點。在圖表控件中的「橡皮筋」矩形內獲取所有數據點

雖然我能夠對矩形的圖形進行編碼,但我現在試圖識別位於此矩形內的數據點。這裏是相關的代碼:

public partial class Form1 : Form 
{ 
    System.Drawing.Point _fromPosition; 
    Rectangle _selectionRectangle; 

    public Form1() 
    { 
     InitializeComponent();    
    } 

    private void chart1_MouseMove(object sender, MouseEventArgs e) 
    { 
     // As the mouse moves, update the dimensions of the rectangle 
     if (e.Button == MouseButtons.Left) 
     { 
      Point p = e.Location; 
      int x = Math.Min(_fromPosition.X, p.X); 
      int y = Math.Min(_fromPosition.Y, p.Y); 
      int w = Math.Abs(p.X - _fromPosition.X); 
      int h = Math.Abs(p.Y - _fromPosition.Y); 
      _selectionRectangle = new Rectangle(x, y, w, h); 

      // Reset Data Point Attributes 
      foreach (DataPoint point in chart1.Series[0].Points) 
      { 
       point.BackSecondaryColor = Color.Black; 
       point.BackHatchStyle = ChartHatchStyle.None; 
       point.BorderWidth = 1; 
      } 
      this.Invalidate(); 
     }      
    } 

    private void chart1_MouseDown(object sender, MouseEventArgs e) 
    { 
     // This is the starting position of the rectangle 
     _fromPosition = e.Location; 
    } 

    private void chart1_Paint(object sender, PaintEventArgs e) 
    { 
     e.Graphics.DrawRectangle(new Pen(Color.Blue, 2), _selectionRectangle); 
     foreach (DataPoint point in chart1.Series[0].Points) 
     {     
      // Check if the data point lies within the rectangle 
      if (_selectionRectangle.Contains(???)))) 
      { 
       // How do I convert DataPoint into Point? 
      } 
     } 
    } 
} 

我想要做的是查詢系列中的每個數據點,並檢查它是否位於矩形內。在這裏,我無法將每個DataPoint轉換爲相應的Point。它看起來非常直截了當,所以我要麼錯過了一些基本的東西,要麼錯誤地處理這個問題。

我還要補充一點,我提到類似的問題herehere,但他們不談論如何真正在矩形內識別數據點。

任何方向將不勝感激!

回答

0

我已經展示瞭如何騙取Chart以幫助獲取Paint事件hereDataPoints的座標。

但只要你想接他們在Paint事件反正不需要作弊..:

我定義了一個列表收集lassoed DataPoints

List<DataPoint> dataPoints = new List<DataPoint>(); 

我清除每一個新的選擇:

void chart1_MouseDown(object sender, MouseEventArgs e) 
{ 
    _fromPosition = e.Location; 
    dataPoints.Clear(); 
} 

最後我能寫出來的結果:

void chart1_MouseUp(object sender, MouseEventArgs e) 
{ 
    foreach(DataPoint pt in dataPoints) 
     Console.WriteLine("found:" + pt.ToString() + 
      " at " + chart1.Series[0].Points.IndexOf(pt)); 
} 

而在Paint事件我們利用ValueToPixelPosition方法兩軸的:

void chart1_Paint(object sender, PaintEventArgs e) 
{ 
    using (Pen pen = new Pen(Color.Blue, 2) // dispose of my Pen 
      {DashStyle = System.Drawing.Drawing2D.DashStyle.Dot}) 
     e.Graphics.DrawRectangle(pen, _selectionRectangle); 
    foreach (DataPoint point in chart1.Series[0].Points) 
    { // !! officially these functions are only reliable in a paint event!! 
     double x = chart1.ChartAreas[0].AxisX.ValueToPixelPosition(point.XValue); 
     double y = chart1.ChartAreas[0].AxisY.ValueToPixelPosition(point.YValues[0]); 
     PointF pt = new PointF((float)x,(float)y); 

     // Check if the data point lies within the rectangle 
     if (_selectionRectangle.Contains(Point.Round(pt))) 
     { 
      if (!dataPoints.Contains(point)) dataPoints.Add(point); 
     } 
    }  
}