question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect IHasPoints.GetPointNearestX results using SignalPlotXY

See original GitHub issue

ScottPlot 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
StendProgcommented, Feb 18, 2021

Hi @at2software, As you rightly noted, SignalXY does not support. GetPointNearestX() It uses the implementation of the parent which is PlottableSignal. And of course it does not work correctly because SignalXY uses a completely different approach to X values. There is possible to exclude support for the IHasPoints interface from PlottableSignalXY. But it may be possible to implement it’s support, I’ll see what can be done the other day.

0reactions
swhardencommented, Mar 20, 2021

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found