在图表控件上显示鼠标轴坐标
有没有一种简单的方法来检索图表区域中任何点的 X/Y 坐标(当然相对于该图表轴)?
截至目前,我只是设法在鼠标位于系列(而不是外部)上时检索坐标
private void chart_GetToolTipText(object sender, ToolTipEventArgs e)
{
if (e.HitTestResult.Series != null)
{
e.Text = e.HitTestResult.Series.Points[e.HitTestResult.PointIndex].YValues[0] + " \n " + DateTime.FromOADate(e.HitTestResult.Series.Points[e.HitTestResult.PointIndex].XValue);
}
}
Is there a simple way to retrieve the X/Y coordinates of ANY point in the chart Area (relative to that chart Axis of course)?
As of now, I just managed to retrieve coordinates when the mouse is on a Series (not outside)
private void chart_GetToolTipText(object sender, ToolTipEventArgs e)
{
if (e.HitTestResult.Series != null)
{
e.Text = e.HitTestResult.Series.Points[e.HitTestResult.PointIndex].YValues[0] + " \n " + DateTime.FromOADate(e.HitTestResult.Series.Points[e.HitTestResult.PointIndex].XValue);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
无论如何,与 MS 图表控件一样,没有简单的方法可以完成任务,
但是有一种时髦的解决方法可以完成任务。可惜我已经习惯了……
Anyway, as always with MS Chart Controls, there is no easy way to do things,
but a funky workaround to get things done. I am sadly getting used to it...
这适合我的目的,并且不会对光标产生副作用。
This works for my purposes and doesn't side effect the cursor.
我尝试了你的答案,但它对我不起作用。它最终将光标放在一处并且永远不会移动。我相信这是因为我沿两个轴使用十进制/双精度值,并且光标被四舍五入到最接近的整数。
经过多次尝试,我找到了一种确定光标在图表内位置的方法。困难的部分是弄清楚图表元素的所有“位置”实际上都是百分比值(从 0 到 100)。
根据
http://msdn.microsoft.com /en-us/library/system.windows.forms.datavisualization.charting.elementposition.aspx:
“定义图表元素在相对坐标中的位置,范围从 (0,0) 到 (100,100)。”
我希望你不介意,我在这里发布这个答案只是为了后代,以防其他人遇到这个问题,并且你的方法也不适用于他们。它无论如何都不漂亮或优雅,但到目前为止它对我有用。
I tried your answer, but it didn't work for me. It ended up putting the cursor in one spot and never moving. I believe this is because I use decimal/double values along both axes, and the cursor is being rounded to the nearest integer.
After several attempts, I was able to work out a method for determining where the cursor is inside the chart. The hard part was figuring out that all the "positions" for the chart elements are actually percentage values (from 0 to 100).
As per
http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.elementposition.aspx:
"Defines the position of the chart element in relative coordinates, which range from (0,0) to (100,100)."
I hope you don't mind, I am posting this answer here just for posterity, in case anyone else comes across this problem, and your method also does not work for them. Its not pretty or elegant in any way, but so far it works for me.
这有效
这有效
This works
And this works
这就是我所得到的,我认为我们中的许多人都有相同的想法,但对您正在寻找的东西有不同的解释。
这将为您提供绘图区域中任何位置的坐标。我发现
HitTest
提供了一个干净简单的解决方案,但是需要进行一些检查,无论光标是在数据点、网格线上还是在绘图区域中(这似乎优先)按这个顺序)。我假设您会对坐标感兴趣,无论鼠标位于哪个对象上。This is what I got, I think many of us are along the same lines but with different interpretations of what it is you're looking for.
This will give you the coordinates at any location in the plotting area. I found the
HitTest
gives a clean and simple solution, but there are a few checks to make, whether the cursor is on a data point, a gridline, or in the plotting area (which seem to take precedence in that order). I assume you'll be interested in the coordinate regardless of which of those objects the mouse is over.VB.net版本,带缩放校正:
VB.net version, with zoom correction: