ScottPlot/ScottPlot

Draggable line on DateTime axis has unexpected snapping behavior

kareem469 opened this issue · 3 comments

The Draggable vertical line is not moving with in a second record when DateTime have multiple millisecond records with in a second. It directly moving to next second record.

For example

05-08-2024 15:36:10.100
05-08-2024 15:36:10.300
05-08-2024 15:36:10.500
05-08-2024 15:36:10.800
05-08-2024 15:36:11.100

Here, while dragging the vertical line, it is moving from 05-08-2024 15:36:10.100 directly to 05-08-2024 15:36:11.100 record. In between records are skipping,

i used the following code:

private void FormsPlot1_MouseMove(object? sender, MouseEventArgs e)
    {
        // this rectangle is the area around the mouse in coordinate units
        CoordinateRect rect = formsPlot1.Plot.GetCoordinateRect(e.X, e.Y, radius: 10);

        if (PlottableBeingDragged is null)
        {
            // set cursor based on what's beneath the plottable
            var lineUnderMouse = GetLineUnderMouse(e.X, e.Y);
            if (lineUnderMouse is null) Cursor = Cursors.Default;
            else if (lineUnderMouse.IsDraggable && lineUnderMouse is VerticalLine) Cursor = Cursors.SizeWE;
            else if (lineUnderMouse.IsDraggable && lineUnderMouse is HorizontalLine) Cursor = Cursors.SizeNS;
        }
        else
        {
            // update the position of the plottable being dragged
            if (PlottableBeingDragged is HorizontalLine hl)
            {
                hl.Y = rect.VerticalCenter;
                hl.Text = $"{hl.Y:0.00}";
            }
            else if (PlottableBeingDragged is VerticalLine vl)
            {
                vl.X = rect.HorizontalCenter;
                vl.Text = $"{vl.X:0.00}";
            }
            formsPlot1.Refresh();
        }
    }

I have a list array of DateTime values with millisecond precision and i want to find the index of the closest DateTime value to the position of the dragged vertical line

Hi @kareem469, does this problem happen even if you zoom in more? I'm wondering if your current zoom is such that a pixel is one second wide or something, so it would not be possible to drag by smaller than a second

Hi @swharden,

For 1 second there will be maximum of 10 records and minimum of 1 record.
For example if 4 records exists between 2 seconds then I want to drag the vertical line and get the nearest record while moving vertical line between to 2 secs data