How to find the nearest point when using a cursor?

Need help in implementing some specific function to your LightningChart Ultimate powered application? Post a question and get code snippets from other LightningChart Ultimate community members.

Moderator: Arction_LasseP

Posts: 4
Joined: Mon Jul 16, 2018 4:13 pm

How to find the nearest point when using a cursor?

Post by firstwluke » Thu Jul 19, 2018 1:54 am


I already added a MouseMove event handler to the cursor that can dynamically move the cursor when the mouse move. I see the documentation that when using a cursor, you can set "cursor.SnapToPoints = true" to mark all the lines' data points that closest to the cursor. However, my requirement is to only mark one line's data point which has the closest distance to the mouse position, not all the lines. Is there any way to make that happen?

What's more, is there anyway to control the visiblity of the cursor marker correctly? I currently set cursor.TrackPoint.Color1 = Colors.Transparent, cursor.TrackPoint.Color2 = Colors.Transparent, cursor.TrackPoint.Color3 = Colors.Transparent, and cursor.TrackPoint.BorderColor = Colors.Transparent initially, and add MouseEnter event (cursor.TrackPoint.BorderColor = Colors.White) and MouseLeave event(cursor.TrackPoint.BorderColor = Colors.Transparent).

The application could toggle the visiblity of the line. When I move the mouse out and disable line's visibility, and then move mouse back to the chart, the cursor markers are still there. You can see the picture that when I make blue line's PointlineSeries.LineVisible = false, the cusror circle markers of that line are still there.
screen1.png (12.23 KiB) Viewed 5438 times
screen2.png (12.38 KiB) Viewed 5438 times


Posts: 401
Joined: Mon Mar 14, 2016 9:22 am

Re: How to find the nearest point when using a cursor?

Post by ArctionKestutis » Fri Jul 20, 2018 7:48 am


Our Demo App contains group of 'Cursor tracking' examples, which illustrate various implementation strategies. Please go through those examples for the ideas. LineSeriesCursor is probably not the one you need, at least not its point tracking feature. Line series cursors allow visual analysis of line series data by tracking the Y-values by X coordinate. SnapToPoints property is modifies behavior of Cursor between series points. If SnapToPoints is enabled, then cursor position is only allowed at the point X-value (rounded to nearest one if needed). Otherwise, position is interpolated.
I believe that for your requirements it is better to use one of Series' SolveNearestDataPoint method. For example, as in example 'solve nearest, project on axes'

Code: Select all

_chart.ViewXY.PointLineSeries[0].SolveNearestDataPointByCoord(pX, pY, out x, out y, out nearestIndex);
Afterwards you can compare distance from those points to Mouse position as estimate from method:

Code: Select all

At the end you just add (or make visible) SeriesEventMarker, if point fulfill all our requirements.

LineSeriesCursor's TrackPoint has basic Point-shape-style. As you noticed already, for symbol coloring 4 colors could be used: Color1, Color2, Color3 or BorderColor. The Color1 is the main color, as well as solid fill color if you set so. Color2 is the gradient color: if gradient fill is set, then it will be between Color1 and Color2. BorderColor is self-explanatory name of color. Color3 is is used in some special shapes, but not for TrackPoint. Below is image of colored TrackPoint with property table, which should help explain coloring better.
TrackPoint colors explained
TrackPoint colors explained
TrackPoint colors.jpg (218.25 KiB) Viewed 5431 times
If you don't want to see tracking Point, just set

Code: Select all

cursor.Style = CursorStyle.VerticalNoTracking;
I could not able to reproduce LineSeriesCursor's TrackPoint drawing for invisible line (at least WinForms edition). Which LightningChart version and edition you are using. Instead of disabling visibility of Line (or together with it) you can set

Code: Select all

Series.CursorTrackEnabled = false;
Hope this helps.