ScottPlot/ScottPlot

The coordinates are incorrect in WPF(.NET 6.0)

ICANfly0402 opened this issue · 3 comments

Issue: (Describe the bug here)
I want to create a mousetracker but the coordinates are incorrect.
Please check the attached video, you can see the crosshair position is not my mouse's position

How do I fix it , thanks.

ScottPlot Version: (What NuGet package are you using?)
5.0.34

Code Sample: See http://scottplot.net/faq/repro/ for tips about creating reproducible code samples
namespace plotTest
{
///


/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow : Window
{
private readonly ScottPlot.Plottables.Crosshair CH;

    public MainWindow()
    {
        InitializeComponent();
        CH = myPlot.Plot.Add.Crosshair(0, 0);
        CH.LineWidth = 2;
        CH.TextColor = ScottPlot.Colors.White;

        CH.TextBackgroundColor = CH.HorizontalLine.Color;

        myPlot.Refresh();
    }

    private void OnMouseMove(object sender, MouseEventArgs e)
    {
        System.Windows.Point mousePosition = e.GetPosition(this);
        float mouseX = (float)(mousePosition.X);
        float mouseY = (float)(mousePosition.Y);

        Pixel mousePixel = new Pixel(mouseX, mouseY);
        Coordinates mouseLocation = myPlot.Plot.GetCoordinates(mousePixel);

        CH.Position = mouseLocation;
        CH.VerticalLine.Text = $"{mouseLocation.X:N3}";
        CH.HorizontalLine.Text = $"{mouseLocation.Y:N3}";
        myPlot.Refresh();
    }
}

}

MainWindow.2024-05-07.17-10-29.mp4
ScottPlot.Plot myPlot = new();
/* code to reproduce the bug */
myPlot.SavePng("bug.png");

BTW, It is OK in SP4.1

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)

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)

Yes it is. I'm sorry that I did not review the old topics which had discussed about this issue.
This example is helpful, and this chart libary is great as well. Thanks very much for your work.