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。它看起來非常直截了當,所以我要麼錯過了一些基本的東西,要麼錯誤地處理這個問題。
我還要補充一點,我提到類似的問題here和here,但他們不談論如何真正在矩形內識別數據點。
任何方向將不勝感激!