Skip to content

Demo: "Show Value Under Mouse, Multiple Series" is broken #3680

@Graat

Description

@Graat

ScottPlot5 demo "Show Value Under Mouse, Multiple Series" responds unintuitively

suggested fix

https://github.com/ScottPlot/ScottPlot/blob/main/src/ScottPlot5/ScottPlot5%20Demos/ScottPlot5%20WinForms%20Demo/Demos/ShowValueOnHoverMultiple.cs

I added a line in the section where it is searching for the point which is nearest to the mouse position. This should fix the issue.

        formsPlot1.MouseMove += (s, e) =>
        {
            // determine where the mouse is
            Pixel mousePixel = new(e.Location.X, e.Location.Y);
            Coordinates mouseLocation = formsPlot1.Plot.GetCoordinates(mousePixel);

            // get the nearest point of each scatter
            Dictionary<int, DataPoint> nearestPoints = new();
            for (int i = 0; i < MyScatters.Count; i++)
            {
                DataPoint nearestPoint = MyScatters[i].Data.GetNearest(mouseLocation, formsPlot1.Plot.LastRender);
                nearestPoints.Add(i, nearestPoint);
            }

            // determine which scatter's nearest point is nearest to the mouse
            bool pointSelected = false;
            int scatterIndex = -1;
            double smallestDistance = double.MaxValue;
            for (int i = 0; i < nearestPoints.Count; i++)
            {
                if (nearestPoints[i].IsReal)
                {
                    // calculate the distance of the point to the mouse
                    double distance = nearestPoints[i].Coordinates.Distance(mouseLocation);
                    if (distance < smallestDistance)
                    {
                        // store the index
                        scatterIndex = i;
                        pointSelected = true;
                        smallestDistance = distance; //I added this line to update smallestDistance.
                    }
                }
            }

            // place the crosshair, marker and text over the selected point
            if (pointSelected)
            {
                ScottPlot.Plottables.Scatter scatter = MyScatters[scatterIndex];
                DataPoint point = nearestPoints[scatterIndex];

                MyCrosshair.IsVisible = true;
                MyCrosshair.Position = point.Coordinates;
                MyCrosshair.LineColor = scatter.MarkerStyle.Fill.Color;

                MyHighlightMarker.IsVisible = true;
                MyHighlightMarker.Location = point.Coordinates;
                MyHighlightMarker.MarkerStyle.Outline.Color = scatter.MarkerStyle.Fill.Color;

                MyHighlightText.IsVisible = true;
                MyHighlightText.Location = point.Coordinates;
                MyHighlightText.LabelText = $"{point.X:0.##}, {point.Y:0.##}";
                MyHighlightText.LabelFontColor = scatter.MarkerStyle.Fill.Color;

                formsPlot1.Refresh();
                base.Text = $"Selected Scatter={scatter.Label}, Index={point.Index}, X={point.X:0.##}, Y={point.Y:0.##}";
            }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good First IssueThis issue has limited complexity and may be a good start for new contributorsHelp WantedScott won't do this soon, but PRs from the community are welcome!⚠️ HIGH PRIORITY

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions