Skip to content

SignalPlotXY: GetPointNearestX fails to return the last value #1757

@MareMare

Description

@MareMare

ScottPlot Version: 4.1.37

Operating System: Windows10

Application Type: WinForms

Question:

GetPointNearestX() with SignalPlotXY fails to get the last X in the array.
But with ScatterPlot I can get it correctly.

public partial class DemoForm1 : Form
{
    private readonly VLine[] _vLines;

    public DemoForm1()
    {
        InitializeComponent();

        var plot = formsPlot1.Plot;

        // first segment is SignalPlotXY
        double[] xs1 = { 1, 2, 3, 4 };
        double[] ys1 = { 5, 4, 8, 6 };
        var plt1 = plot.AddSignalXY(xs1, ys1, Color.Red);
        plt1.YAxisIndex = 0;

        // second segment is ScatterPlot
        double[] xs2 = { 1, 2, 3, 4 };
        double[] ys2 = { 6, 9, 4, 5 };
        var plt2 = plot.AddScatter(xs2, ys2, Color.Blue);
        plt2.YAxisIndex = 1;


        var vLine0 = plot.AddVerticalLine(xs1[0], Color.Red, style: ScottPlot.LineStyle.Dash);
        vLine0.YAxisIndex = 0;

        var vLine1 = plot.AddVerticalLine(xs2[0], Color.Blue, style: ScottPlot.LineStyle.Dash);
        vLine1.YAxisIndex = 1;

        _vLines = new[] { vLine0, vLine1, };

        formsPlot1.Refresh();
    }

    private void formsPlot1_MouseMove(object sender, MouseEventArgs e)
    {
        (int mousePixelX, int mousePixelY) = (e.X, e.Y);

        var settings = formsPlot1.Plot.GetSettings(false);
        foreach (var plt in settings.Plottables)
        {
            if (plt is VLine)
            {
                continue;
            }

            var xAxisIndex = settings.GetXAxis(plt.XAxisIndex).AxisIndex;
            var yAxisIndex = settings.GetYAxis(plt.YAxisIndex).AxisIndex;

            (double coordinateX, double coordinateY) = formsPlot1.GetMouseCoordinates(xAxisIndex: xAxisIndex, yAxisIndex);

            if (plt is IHasPointsGenericX<double, double> point)
            {
                var nearest1 = point.GetPointNearestX(coordinateX);

                var vline = _vLines.FirstOrDefault(l => l.YAxisIndex == yAxisIndex);
                if (vline != null)
                {
                    vline.X = nearest1.x;
                }
            }
        }

        formsPlot1.Refresh();
    }
}

The following is the result of running the above code.

Animation

Perhaps the following is incorrect?

int index = Array.BinarySearch(Xs, MinRenderIndex, MaxRenderIndex - 1, x);

Metadata

Metadata

Assignees

No one assigned

    Labels

    BUGunexpected behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions