我有一个图表,希望用户在指针悬停在点上时能看到该点的值。 通过使用digEmAll在页面找到图表中点的值的帮助,我编写了以下代码:
Point? prevPosition = null;
ToolTip tooltip = new ToolTip();
void chart1_MouseMove(object sender, MouseEventArgs e)
{
var pos = e.Location;
if (prevPosition.HasValue && pos == prevPosition.Value)
return;
tooltip.RemoveAll();
prevPosition = pos;
var results = chart1.HitTest(pos.X, pos.Y, false, ChartElementType.PlottingArea);
foreach (var result in results)
{
if (result.ChartElementType == ChartElementType.PlottingArea)
{
chart1.Series[0].ToolTip = "X=#VALX, Y=#VALY";
}
}
}
with a period at the end for proper formatting.通过上面的代码,用户可以在指针接近一系列值时看到这些值。但现在,我该如何让用户只有在指针落在这些点上时才能看到这些值呢?
int k = result.PointIndex;
if (k >= 0)
{
chart1.Series[0].Points[k].ToolTip = "X=#VALX, Y=#VALY";
}
代替,而不是。chart1.Series[0].ToolTip = "X=#VALX, Y=#VALY";
为了解决我的问题,但它没有用。
(Math.Abs(pos.X - pointXPixel) < 2 && Math.Abs(pos.Y - pointYPixel) < 2)
失败了。 - Ramesh Durai