Skip to content

Scatter: GetNearest() can throw System.IndexOutOfRangeException #3461

@sunshuaize

Description

@sunshuaize

Question: My program renders Scatter in ScottPlot in real time through hardware device acquisition
referring to this example: https://github.com/ScottPlot/ScottPlot/blob/main/src/ScottPlot5/ScottPlot5%20Demos/ScottPlot5%20WinForms%20Demo/Demos/ShowValueOnHover.cs

I added the FormsPlot1_MouseMove event, but an error occurred on this line:

DataPoint nearest = Scatter.Data.GetNearest(mouseLocation, formsPlot1.Plot.LastRender);

This error is sporadic, sometimes appearing, sometimes not appearing.

Thank you very much for seeing. I don't know if I've described it

Another issue is that some Scatter does not trigger MouseMove in ScottPlot

Call stack

System.IndexOutOfRangeException
  HResult=0x80131508
  Message=Index was outside the bounds of the array.
  Source=ScottPlot
  StackTrace:
   在 ScottPlot.DataSources.ScatterSourceDoubleArray.GetNearest(Coordinates mouseLocation, RenderDetails renderInfo, Single maxDistance) 在 ScottPlot.DataSources\ScatterSourceDoubleArray.cs 中: 第 70 行
   在 AngleTestSys.Form1.FormsPlot1_MouseMove(Object sender, MouseEventArgs e) 在 D:\jinchangyu\sunshuaize\AngleTestSys\AngleTestSys\Form1.cs 中: 第 466 行
   在 System.Windows.Forms.Control.OnMouseMove(MouseEventArgs e) 在 /_/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs 中: 第 8348 行
   在 System.Windows.Forms.Control.OnMouseMove(MouseEventArgs e) 在 /_/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs 中: 第 8348 行
   在 System.Windows.Forms.Control.WmMouseMove(Message& m) 在 /_/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs 中: 第 12380 行
   在 System.Windows.Forms.Control.WndProc(Message& m) 在 /_/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs 中: 第 13281 行
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 /_/src/System.Windows.Forms/src/System/Windows/Forms/Control.ControlNativeWindow.cs 中: 第 68 行
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 /_/src/System.Windows.Forms/src/System/Windows/Forms/Control.ControlNativeWindow.cs 中: 第 122 行
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam) 在 /_/src/System.Windows.Forms/src/System/Windows/Forms/NativeWindow.cs 中: 第 370 行
   在 Interop.User32.DispatchMessageW(MSG& msg)
   在 System.Windows.Forms.Application.ComponentManager.Interop.Mso.IMsoComponentManager.FPushMessageLoop(UIntPtr dwComponentID, msoloop uReason, Void* pvLoopData) 在 /_/src/System.Windows.Forms/src/System/Windows/Forms/Application.ComponentManager.cs 中: 第 346 行
   在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(msoloop reason, ApplicationContext context) 在 /_/src/System.Windows.Forms/src/System/Windows/Forms/Application.ThreadContext.cs 中: 第 1117 行
   在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(msoloop reason, ApplicationContext context) 在 /_/src/System.Windows.Forms/src/System/Windows/Forms/Application.ThreadContext.cs 中: 第 981 行
   在 System.Windows.Forms.Application.Run(Form mainForm) 在 /_/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs 中: 第 1188 行
   在 AngleTestSys.Program.Main() 在 D\AngleTestSys\AngleTestSys\Program.cs 中: 第 9 行

Code

private void FormsPlot1_MouseMove(object? sender, MouseEventArgs e)
{
    Pixel mousePixel = new(e.Location.X, e.Location.Y);
    Coordinates mouseLocation = formsPlot1.Plot.GetCoordinates(mousePixel);
    if (mouseLocation.X > 0 && mouseLocation.Y > 0)
    {
        //
        DataPoint nearest = Scatter.Data.GetNearest(mouseLocation, formsPlot1.Plot.LastRender);

        if (nearest.IsReal)
        {
            MyCrosshair.IsVisible = true;
            MyCrosshair.Position = nearest.Coordinates;
            formsPlot1.Refresh();
            Text = $"Selected Index={nearest.Index}, X={nearest.X:0.##}, Y={nearest.Y:0.##}";
        }

        // hide the crosshair when no point is selected
        if (!nearest.IsReal && MyCrosshair.IsVisible)
        {
            MyCrosshair.IsVisible = false;
            formsPlot1.Refresh();
            Text = $"No point selected";
        }
    }
}

devenv_eaix5YXMul
devenv_AkkQyV54vP
AngleTestSys_7kU6aanCM6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions