从单击 ChartArea 的 X 光标位置获取系列的 Y 值 [英] Get Y value of series from the X Cursor position where a ChartArea is clicked on

查看:29
本文介绍了从单击 ChartArea 的 X 光标位置获取系列的 Y 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取与用户点击的 ChartArea 的 X 位置相对应的系列的 Y 值.

I would like to get the Y Value for a series corresponding to the X position of a ChartArea that the user has clicked-upon.

我试图在点击的图表区域内捕获鼠标的 X 位置,但结果返回 NaN:

I have tried to capture the X position of the mouse within the chartarea that is clicked upon but I am getting NaN returned for the result:

 private void chart_Click(object sender, EventArgs e)
    {
       double XVal = chart.ChartAreas[0].CursorX.Position;
    }

一旦我在用户单击鼠标的图表区域中获得了 X 位置,我就想使用它来获得该 x 位置的系列的 Y 值.

Once I have got the X position in the chartarea where the user has clicked the mouse, I would then like to use that to get the Y value of a series at that x-position.

推荐答案

  • 你的代码有什么问题?
  • 至于 chart.ChartAreas[0].CursorX:Chart Cursor 是由 缩放 然后

    代表一条水平线或垂直线,沿线定义一个位置一个轴.

    Represents a horizontal or vertical line that defines a position along an axis.

    因此它与鼠标光标没有太大关系,并且在不缩放或(禁用缩放时)用于选择时无效.

    So it has not much to do with the mouse cursor and is not valid when not zooming or (when zooming is disabled) for selecting.

    • 那么如何解决您的问题?

    您有几个选择;不幸的是,没有一个既简单完全符合您的要求.

    There are a few options you have; unfortunately none are both simple and exactly what you asked for.

    一个简单的事情是将HitTest用于Charts:

    private void chart1_MouseMove(object sender, MouseEventArgs e)
    {
        HitTestResult hit = chart1.HitTest(e.X, e.Y);
        if (hit.PointIndex >= 0)
            infoLabel.Text = "Over DataPoint No " +  hit.PointIndex;
    }
    

    这既简单又安全,但只有当光标实际上超过DataPoint 时才有效.它适合您的程度取决于ChartType;对于ColumnsBars 来说非常好,但对于PointsBubbles 来说不太好..

    This is simple and safe but will work only when the cursor is actually over a DataPoint. How well it suits you may depend on the ChartType; Very well for Columns or Bars but not so well for Points or Bubbles..

    并且你可以调用PixelPositionToValue:

    private void chart1_MouseMove(object sender, MouseEventArgs e)
    {
        var xv =  chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);                    
        var yv =  chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y);
        infoLabel.Text = "x = " +  xv + "   y =" + yv;
    }
    

    theory 这只是从 Paint 事件之一调用是安全的,但实际上从用户交互调用时它似乎也能正常工作.(如果你遇到问题,你可以做一个 Paint 事件之一进行虚拟调用,并在拉取您想要的值后使用标志中止它;该示例做了很多比你需要的更多,但我怀疑它根本没有必要......)

    In theory this is only safe to call from one of the Paint events but in practice it seems to work fine when called from user interactions as well. (If you run into problems you could do a dummy call on one of the Paint events and abort it with a flag after pulling the value(s) you want; the example does a lot more than what you need, but I doubt it will be necessary at all..)

    但是它只会根据轴带回Values,而不是最近的DataPoint.如果您确实需要获得实际的 DataPoint,则必须搜索 SeriesPoints ..:

    However it will only bring back the Values according to the axis, not the nearest DataPoint. If you really need to get at the actual DataPoint you would then have to do a search over the Points of your Series..:

    Series S = chart1.Series[0];            // short reference
    DataPoint pPrev = S.Points.Select(x => x)
                            .Where(x => x.XValue >= xv)
                            .DefaultIfEmpty(S.Points.First()).First();
    DataPoint pNext = S.Points.Select(x => x)
                            .Where(x => x.XValue <= xv)
                            .DefaultIfEmpty(S.Points.Last()).Last();
    

    这应该会显示上一个和下一个 DataPoint.由您决定使用哪个..

    This should bring up the previous and next DataPoint. It is up to you to decide which one to use..

    这篇关于从单击 ChartArea 的 X 光标位置获取系列的 Y 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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