悬停时突出显示winforms MS Chart线
Posted
技术标签:
【中文标题】悬停时突出显示winforms MS Chart线【英文标题】:Highlight winforms MS Chart line on hover 【发布时间】:2021-12-28 05:23:59 【问题描述】:我正在尝试将 MS Chart 与自定义控件一起使用。我的目的是:
鼠标悬停在连接两个相邻点的线段上时,仅突出显示该线段 查找这两个相邻点的索引(我需要它以便能够通过同时移动两个点来拖动该线)插图种类:
现在我可以使用here 描述的方法检测图表上一条线上的鼠标悬停。但我一直在寻找索引或至少是这两个点的坐标。
【问题讨论】:
你看到this了吗? 谢谢。我已经对其进行了相当大的修改并设法使其工作。我将在答案中发布我的代码 【参考方案1】:所以this question 的最初想法是通过 x 找到最近的点(假设所有系列的 x 值确实在稳步增加),然后计算 y 值。但我对此进行了一些改进,并增加了对完全垂直线的支持。所以这是我捕获所需行的代码:
private static GrippedLine? LineHitTest(Series series, double xPos, double yPos, Axis xAxis, Axis yAxis)
double xPixelPos = xAxis.PixelPositionToValue(xPos);
double yPixelPos = yAxis.PixelPositionToValue(yPos);
DataPoint[] neighbors = new DataPoint[2];
neighbors[0] = series.Points.Last(x => x.XValue <= xPixelPos);
neighbors[1] = series.Points.First(x => x.XValue >= xPixelPos);
DataPoint[] verticalMates;
foreach (DataPoint neighbor in neighbors)
if (Math.Abs(neighbor.XValue - xPixelPos) < LINE_GRIP_REGION)
verticalMates = series.Points.FindAllByValue(neighbor.XValue, "X").ToArray();
if (verticalMates.Length > 1)
if (verticalMates.Length > 2)
if (verticalMates[0].YValues[0] < verticalMates[verticalMates.Length - 1].YValues[0])
neighbors[0] = verticalMates.LastOrDefault(y => y.YValues[0] < yPixelPos);
neighbors[1] = verticalMates.FirstOrDefault(y => y.YValues[0] >= yPixelPos);
else
neighbors[0] = verticalMates.LastOrDefault(y => y.YValues[0] > yPixelPos);
neighbors[1] = verticalMates.FirstOrDefault(y => y.YValues[0] <= yPixelPos);
else
neighbors[0] = verticalMates[0];
neighbors[1] = verticalMates[1];
break;
double x0 = xAxis.ValueToPixelPosition(neighbors[0].XValue);
double y0 = yAxis.ValueToPixelPosition(neighbors[0].YValues[0]);
double x1 = xAxis.ValueToPixelPosition(neighbors[1].XValue);
double y1 = yAxis.ValueToPixelPosition(neighbors[1].YValues[0]);
double Yinterpolated = y0 + (y1 - y0) * (xPos - x0) / (x1 - x0);
int[] linePoints = new int[2];
// if mouse Y position is near the calculated OR the line is vertical
if (Math.Abs(Yinterpolated - yPos) < LINE_GRIP_REGION || neighbors[0].XValue == neighbors[1].XValue)
linePoints[0] = series.Points.IndexOf(neighbors[0]);
linePoints[1] = series.Points.IndexOf(neighbors[1]);
else
return null;
return new GrippedLine()
startLinePointIndex = linePoints[0],
endLinePointIndex = linePoints[1],
x0Correction = neighbors[0].XValue - xPixelPos,
y0Correction = neighbors[0].YValues[0] - yPixelPos,
x1Correction = neighbors[1].XValue - xPixelPos,
y1Correction = neighbors[1].YValues[0] - yPixelPos
;
【讨论】:
以上是关于悬停时突出显示winforms MS Chart线的主要内容,如果未能解决你的问题,请参考以下文章