C# MS图表,如何在鼠标移动事件中获取CursorX数据点?
我有一个关于ms图表的问题:如何在鼠标移动事件中获取CursorX数据点?假设您的图表被称为C# MS图表,如何在鼠标移动事件中获取CursorX数据点?,c#,charts,datapoint,C#,Charts,Datapoint,我有一个关于ms图表的问题:如何在鼠标移动事件中获取CursorX数据点?假设您的图表被称为\u chart,并且您要查找的图表的索引是chart\u index: 首先,从X像素坐标计算X轴坐标: // Assume MouseEventArgs e is passed in from (for example) chart_MouseDown(): var xAxis = _chart.ChartAreas[CHART_INDEX].AxisX; double x = xAxis.Pixel
\u chart
,并且您要查找的图表的索引是chart\u index
:
首先,从X像素坐标计算X轴坐标:
// Assume MouseEventArgs e is passed in from (for example) chart_MouseDown():
var xAxis = _chart.ChartAreas[CHART_INDEX].AxisX;
double x = xAxis.PixelPositionToValue(e.Location.X);
现在x
是x轴上像素的x坐标(以x轴为单位)
然后,您必须确定距离X轴值最近的前一个数据点
假设SERIES\u INDEX
是您感兴趣的系列的索引:
private double nearestPreceedingValue(double xCoord)
{
// Find the last at or before the current xCoord.
// Since the data is ordered on X, we can binary search for it.
var data = _chart.Series[SERIES_INDEX].Points;
int index = data.BinarySearch(xCoord, (xVal, point) => Math.Sign(xCoord - point.XValue));
if (index < 0)
{
index = ~index; // BinarySearch() returns the index of the next element LARGER than the target.
index = Math.Max(0, index-1); // We want the value of the previous element, so we must decrement the returned index.
} // If this is before the start of the graph, use the first valid data point.
// Return -1 if not found, or the value of the nearest preceeding point if found.
// (Substitute an appropriate value for -1 if you need a different "invalid" value.)
return (index < data.Count) ? (int)data[index].YValues[0] : -1;
}
private双近程值(双xCoord)
{
//在当前xCoord处或之前查找最后一个。
//因为数据是在X上排序的,所以我们可以对它进行二进制搜索。
var数据=_图表.系列[系列索引].点;
int index=data.BinarySearch(xCoord,(xVal,point)=>Math.Sign(xCoord-point.XValue));
如果(指数<0)
{
index=~index;//BinarySearch()返回下一个大于目标的元素的索引。
index=Math.Max(0,index-1);//我们需要上一个元素的值,因此必须递减返回的索引。
}//如果在图形开始之前,请使用第一个有效数据点。
//如果未找到,则返回-1,如果找到,则返回最近的前一个点的值。
//(如果需要不同的“无效”值,请用适当的值替换-1。)
返回(index
表示您想要数据点上的工具提示?