微软.NET图表控件 - 使用矩形选择点 [英] Microsoft .NET Chart control - select points using rectangle

查看:176
本文介绍了微软.NET图表控件 - 使用矩形选择点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与X数据,Y值绘制在一个散点图使用.NET 4.0图表控件。我相信从登打士获得微软图表控件。

Data with X, Y values is plotted on as a scatter chart using the .NET 4.0 Chart control. I believe the Microsoft chart control was obtained from Dundas.

我想用橡皮筋矩形相似图上的选择点用于放大图表的矩形。 (当单击第一个鼠标按钮,它建立了矩形的一个角落里,把鼠标移动半透明的矩形将被重画,直到鼠标被释放)。

I would like to select points on the chart using a rubber-band rectangle similar to the rectangle that is used to zoom the chart. (When the first mouse button is clicked it establishes one corner of the rectangle, as the mouse moves the semi-transparent rectangle would be redrawn until the mouse is released).

有没有办法覆盖变焦方法来创建一个选择,而不是移近?

Is there a way to override the zoom method to create a selection rather than zooming-in?

可以覆盖被放置在一个现有的图表将捕获的鼠标点击和显示一个透明的矩形?

Can an overlay be placed over an existing chart that will catch the mouse clicks and display a transparent rectangle?

我通常喜欢发布示例代码与我的问题,但在这种情况下,我不知道从哪里开始。链接到正确的位置的文档中,其他教程或示例代码,将不胜感激。

I usually like to post sample code with my questions, but in this case I don't know where to begin. Links to the correct spot in the documentation, other tutorials or example code would be appreciated.

推荐答案

这使用了一个正橡皮筋矩形选择抓点:

This uses a regular rubber-band rectangle to select the points caught:

< IMG SRC =http://i.stack.imgur.com/zSC1I.gifALT =在这里输入的形象描述>

Point mdown = Point.Empty;
List<DataPoint> selectedPoints = null;

private void chart1_MouseDown(object sender, MouseEventArgs e)
{
    mdown = e.Location;
    selectedPoints = new List<DataPoint>();
}

private void chart1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        chart1.Refresh();
        using (Graphics g = chart1.CreateGraphics())
            g.DrawRectangle(Pens.Red, GetRectangle(mdown, e.Location));
    }
}

private void chart1_MouseUp(object sender, MouseEventArgs e)
{
    Axis ax = chart1.ChartAreas[0].AxisX;
    Axis ay = chart1.ChartAreas[0].AxisY;
    Rectangle rect = GetRectangle(mdown, e.Location);

    foreach (DataPoint dp in chart1.Series[0].Points)
    {
        int x = (int)ax.ValueToPixelPosition(dp.XValue);
        int y = (int)ay.ValueToPixelPosition(dp.YValues[0]);
        if (rect.Contains(new Point(x,y))) selectedPoints.Add(dp);
    }

    // optionally color the found datapoints:
    foreach (DataPoint dp in chart1.Series[0].Points)
        dp.Color = selectedPoints.Contains(dp) ? Color.Red : Color.Black;
}

static public Rectangle GetRectangle(Point p1, Point p2)
{
    return new Rectangle(Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y),
        Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
}

请注意,这会为线,快绳工作,点图表。对于其他类型的,你必须适应选择标准!

Note that this will work for Line, FastLine and Point charts. For other types you would have to adapt the selection criterium!

这篇关于微软.NET图表控件 - 使用矩形选择点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆