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

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

问题描述

我想获得一个系列的Y值,该系列对应于用户点击的ChartArea的X位置。



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

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

一旦我在图表区域中获得了X位置,鼠标,我会使用它来获得在x位置的系列的Y值。

解决方案


  • 您的代码有什么问题?


对于 chart.ChartAreas [0] .CursorX :A Chart Cursor 是由缩放,然后


表示水平或垂直线它定义沿着
轴的位置。


所以它和鼠标光标没有什么关系,当不缩放时。




  • 如何解决您的问题?



有几个选项;

c $ c> HitTest for Charts

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

这是简单和安全的,但只有当游标实际上 over a DataPoint 。它适合你的程度取决于 ChartType ;非常适合 Bars ,但不适用于 c> PixelPositionToValue :

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

理论这只是安全地从一个 Paint 事件,但在实践中,似乎在从用户交互调用时也能正常工作。 (如果遇到问题,您可以执行 Paint 事件之一的虚拟调用,并在拉取您想要的值后;



但是它只会带回

code>根据轴的值,而不是最近的 DataPoint 。如果你真的需要得到实际的 DataPoint ,那么你必须在 Points code>系列 ..:

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

这会显示上一个和下一个 DataPoint 。由你决定使用哪一个。


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.

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;
    }

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.

解决方案

  • What is wrong with you code?

As for chart.ChartAreas[0].CursorX: A Chart Cursor is an object created by and used for zooming and which then

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.

  • How to solve your problem then?

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

One simple thing is to use the HitTest for 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;
}

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..

And you could call 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;
}

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..)

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();

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天全站免登陆