ScottPlot/ScottPlot

Scatter: Inaccurate point nearest to the cursor

Str8ICE opened this issue · 1 comments

Hello!

I was trying to fetch and display the coordinates of the data point that is nearest to the mouse cursor and I noticed a really strange and annoying behavior (or two).

In the below GIF you can see that the plot "thinks" that the line is somewhere else, a bit lower than where it's displayed.

ScottPlotGif1

A workaround might be to increase the maxDistance argument given to the GetNearest method but even so, the problem is that the point values don't match with the location of the mouse pointer (it shows 34 when I hover over 40, it shows 67 when I hover over 80, etc.), as presented in the second GIF:

ScottPlotGif2

As a quick observation, the closer to the left the points I "interrogate" are, the more accurate the result. The further to the right I hover, the greater the difference between the result and the real value.

I would like to mention that I'm using ScottPlot 5.0.34 and that the issue only reproduces in WPF, not in WinForms.
This is my code. Any suggestion, ideas, remarks would be highly appreciated!

public partial class MainWindow
{
    private readonly Scatter _myScatter;

    public MainWindow()
    {
        InitializeComponent();

        var xValues = new double[100];
        var yValues = new double[100];

        for (var i = 0; i < 100; i++)
        {
            xValues[i] = i;
            yValues[i] = i;
        }

        _myScatter = WpfPlot.Plot.Add.Scatter(xValues, yValues);
    }

    private void OnMouseMove(object sender, MouseEventArgs e)
    {
        var position = e.GetPosition(WpfPlot);
        var mousePixel = new Pixel(position.X, position.Y);
        var mouseLocation = WpfPlot.Plot.GetCoordinates(mousePixel);

        var nearest = _myScatter.Data.GetNearest(mouseLocation, WpfPlot.Plot.LastRender, 300F);

        if (nearest.IsReal)
        {
            MyLabel.Content = $"X={nearest.X:0.##}, Y={nearest.Y:0.##}";
        }
    }
}

Thank you for this awesome library! It has been a life savor.

MCF commented

Is your computer display scaled under Windows? If yes, your wpf application has to handle the scaling when converting a raw display pixel position into the effective pixel position as expected by ScottPlot. Here is an example of how to do that:

#3465 (comment)