目前效果,转出来的效果不咋地
思路就是拿到所有的记录器中的数据然后使用库的api得到相应点数据点,最后渲染上去,代码在下方
鼠标 移动方法
private void Myplot_MouseMove(object sender, MouseEventArgs e)
{
if (PressDatalogger.Data.CountTotal == 0 || PressDatalogger.Data.CountTotal == 0 || PressDatalogger.Data.CountTotal == 0)
return;
Pixel mousePixel = new(e.GetPosition(MyPlot).X, e.GetPosition(MyPlot).Y);
Coordinates pressPoint = MyPlot.Plot.GetCoordinates(mousePixel, null, PressDatalogger.Axes.YAxis);
Coordinates posPoint = MyPlot.Plot.GetCoordinates(mousePixel, null, PostionDatalogger.Axes.YAxis);
Coordinates speedPoint = MyPlot.Plot.GetCoordinates(mousePixel, null, SpeedDatalogger.Axes.YAxis);
var pressPointnearest = GetNearestPoint(PressDatalogger, pressPoint);
var posPointnearest = GetNearestPoint(PostionDatalogger, posPoint);
var speedPointnearest = GetNearestPoint(SpeedDatalogger, speedPoint);
CheckSelectSerier();
MyCrosshair.Position = speedPointnearest.Coordinates;
MyCrosshair.Axes = PressDatalogger.Axes;
UpdateHighlightedPoint(_speedSerierIscheck, SpeedHighlightedPoint, speedPointnearest, SpeedDatalogger);
UpdateHighlightedPoint(_pressSerierIscheck, PressHighlightedPoint, pressPointnearest, PressDatalogger);
UpdateHighlightedPoint(_positionSerierIscheck, PositionHighlightedPoint, posPointnearest, PostionDatalogger);
}
private DataPoint GetNearestPoint(DataLogger datalogger, Coordinates point)
{
ScatterSourceDoubleArray pointDoubleArray = new ScatterSourceDoubleArray(
datalogger.Data.Coordinates.Select(c => c.X).ToArray(),
datalogger.Data.Coordinates.Select(c => c.Y).ToArray()
);
return pointDoubleArray.GetNearestX(point, MyPlot.Plot.LastRender);
}
private void UpdateHighlightedPoint(bool isChecked, Marker highlightedPoint, DataPoint nearestPoint, DataLogger datalogger)
{
if (isChecked)
{
highlightedPoint.Location = nearestPoint.Coordinates;
highlightedPoint.IsVisible = true;
highlightedPoint.Axes.YAxis = datalogger.Axes.YAxis;
highlightedPoint.Color = datalogger.LineColor;
highlightedPoint.LegendText = datalogger.LegendText + nearestPoint.Y.ToString("F3");
}
else
{
highlightedPoint.IsVisible = false;
}
}
扩展方法:最开始需要鼠标点击后判断当前序列在哪个Y轴上,但是不好处理,写了个没啥用的扩展方法,关于如果获取位置点可以参考GitHub连接Updated: I want to display the value of the data point closest to the cursor. I see the problem. · ScottPlot/ScottPlot · Discussion #3847 · GitHub
public static (bool state, Coordinates? coordinates) GetNearest(
this DataLoggerSource dataLoggerSource,
Coordinates mouseLocation,
DataLogger dataLogger,
float maxDistance = 2f
)
{
float cX = (float)mouseLocation.X.ToThreeDecimal();
float cY = (float)mouseLocation.Y.ToThreeDecimal();
float minX = cX - 15;
float maxX = cX + 15;
float minY = cY - maxDistance;
float maxY = cY + maxDistance;
List<Coordinates> xCoordinates = new List<Coordinates>();
var point = dataLogger.Data.Coordinates.FirstOrDefault(p => p.X >= minX && p.X <= maxX && p.Y >= minY && p.Y <= maxY);
if (point.X > 0 && point.Y > 0)
{
return (true, point);
}
return (false, null);
}