Incorrect IHasPoints.GetPointNearestX results using SignalPlotXY
See original GitHub issueScottPlot version v4.1.8-beta
Note: this might be related to #548 and maybe this function is just not yet supported for SignalPlotXY
(then this is not a bug of course, but probably this function should then not be available to SignalPlotXY
instead of returning false values).
There seems to be an issue with the results returned by IHasPoints.GetPointNearestX
with a SignalPlotXY
when the x values aren’t consecutive integers starting at zero.
Here is an example to highlight the nearest point of a ScatterPlot (adapted from here)
public partial class MainWindow : Window
{
private readonly IHasPoints plottable;
private readonly ScatterPlot HighlightedPoint;
private int LastHighlightedIndex = -1;
public MainWindow()
{
InitializeComponent();
int sampleCount = 50;
double[] xs = new double[sampleCount];
double[] ys = new double[sampleCount];
Random rand = new Random(0);
for (int i = 0; i < 50; i++)
{
xs[i] = DateTime.Now.ToOADate() + i*10;
ys[i] = i > 0 ? ys[i - 1] + rand.NextDouble() - .5 : 0;
}
//plottable = plot.Plot.AddScatterPoints(xs, ys); // <-- this works as expected
plottable = plot.Plot.AddSignalXY(xs, ys);
HighlightedPoint = plot.Plot.AddPoint(0, 0);
HighlightedPoint.Color = System.Drawing.Color.Red;
HighlightedPoint.MarkerSize = 10;
HighlightedPoint.MarkerShape = MarkerShape.openCircle;
HighlightedPoint.IsVisible = false;
plot.Plot.XAxis.DateTimeFormat(true);
}
private void plot_MouseMove(object sender, MouseEventArgs e)
{
(double mouseCoordX, double mouseCoordY) = plot.GetMouseCoordinates();
// these results are incorrect when plottable is SignalPlotXY:
(double pointCoordX, double pointCoordY, int pointIndex) = plottable.GetPointNearestX(mouseCoordX);
// highlight the point of interest
HighlightedPoint.Xs[0] = pointCoordX;
HighlightedPoint.Ys[0] = pointCoordY;
HighlightedPoint.IsVisible = true;
tbPointCoord.Text = $"{pointCoordX:N1}, {pointCoordY:N1}";
tbIndex.Text = pointIndex.ToString();
if (LastHighlightedIndex != pointIndex)
{
LastHighlightedIndex = pointIndex;
plot.Render();
}
}
}
With the ScatterPlot this works as expected. Changing the IHasPoints
plottable to SignalPlotXY
, GetPointNearestX
now gives wrong results, the index always relates to x=0. This is especially striking when using DateTimeFormat(true)
as here we provide DateTime.ToOADate()
values as xs values, which e.g. are in the range of 44000 and above. So here, the determined index by GetPointNearestX
is always the max index of the array (49 in the example case).
I guess this is because here https://github.com/ScottPlot/ScottPlot/blob/92ec0558a76795a2fbfc4a54258399522092b074/src/ScottPlot/Plottable/SignalPlotBase.cs#L689 it is assumed that the x parameter is directly correlated to the index, which is true for SignalPlot
but not for SignalPlotXY
. Probably GetPointNearestX
should be made virtual in SignalPlotBase
and SignalPlotXY
would need to override it with an implementation to determine the index based on the parameter value similar to here https://github.com/ScottPlot/ScottPlot/blob/92ec0558a76795a2fbfc4a54258399522092b074/src/ScottPlot/Plottable/ScatterPlot.cs#L292-L309
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Hi @at2software, As you rightly noted,
SignalXY
does not support.GetPointNearestX()
It uses the implementation of the parent which isPlottableSignal
. And of course it does not work correctly becauseSignalXY
uses a completely different approach to X values. There is possible to exclude support for theIHasPoints
interface fromPlottableSignalXY
. But it may be possible to implement it’s support, I’ll see what can be done the other day.Following-up on this,
I think the original issue was solved by #886 which was merged today and I’ll release on NuGet tonight.
SignalPlotXY does not properly support OffsetX and OffsetY, so I created an issue to track that #890
I think this is enough so this ticket can be closed, but let me know if any other questions or issues come up. Thanks for both of your input on this topic!