获取图表控件中“橡皮筋”矩形内的所有数据点
本文关键字:橡皮筋 数据 控件 获取 | 更新日期: 2023-09-27 18:32:58
我在.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?
}
}
}
}
我正在尝试做的是查询系列中的每个数据点并检查它是否位于矩形内。在这里,我无法将每个数据点转换为其相应的点。这似乎很简单,所以我要么在这里错过了一些基本的东西,要么错误地处理了问题。
我还应该补充一点,我在这里和这里提到了类似的问题,但它们并没有讨论如何在矩形中实际识别数据点。
任何方向将不胜感激!
我已经在这里展示了如何欺骗Chart
以帮助获得Paint
事件中的DataPoints
坐标。
但是,由于您想在Paint
活动中捡起它们,因此不需要作弊..:
我定义了一个列表来收集套索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);
}
}
}