Nearest point by coordinates

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Queue Moderators

Post Reply
lukoprovandrey
Posts: 29
Joined: Fri Sep 04, 2015 2:30 pm

Nearest point by coordinates

Post by lukoprovandrey » Tue Jun 28, 2016 3:30 pm

Hello.

Could you suggest me how I can find a nearest point (and X coordinate of this point) from all series by X coordinate?

It is need for drawing a LineSeriesCursors in a properly place.

Thanks.
- Andrey.

User avatar
ArctionPasi
Posts: 1367
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland
Contact:

Re: Nearest point by coordinates

Post by ArctionPasi » Fri Jul 01, 2016 12:52 pm

Hi Andrey,

You can use SolveYCoordAtXCoord for solving by screen coordinate, or SolveYValueAtXValue method for solving by given X value. Since you need Y value and nearest X value and not coordinates, i would recommend the follwing approach:

Code: Select all

  void m_chart_MouseMove(object sender, MouseEventArgs e)
        {
            foreach (PointLineSeries s in m_chart.ViewXY.PointLineSeries)
            {
                double xValue;
                m_chart.ViewXY.XAxes[0].CoordToValue(e.X, out xValue, false);

                LineSeriesValueSolveResult r = s.SolveYValueAtXValue(xValue);

                double xNearest = s.Points[r.NearestDataPointIndex].X;
                double yNearest = s.Points[r.NearestDataPointIndex].Y;
            }
        }
LightningChart Support Team, PT

lukoprovandrey
Posts: 29
Joined: Fri Sep 04, 2015 2:30 pm

Re: Nearest point by coordinates

Post by lukoprovandrey » Thu Jul 07, 2016 1:19 pm

OK, but can I do something like this but for SampleDataSeries?

User avatar
ArctionPasi
Posts: 1367
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland
Contact:

Re: Nearest point by coordinates

Post by ArctionPasi » Thu Jul 07, 2016 6:29 pm

For SampleDataSeries it goes like this:

Code: Select all

void m_chart_MouseMove(object sender, MouseEventArgs e)
        {
            foreach (SampleDataSeries s in m_chart.ViewXY.SampleDataSeries)
            {
                double xValue;
                m_chart.ViewXY.XAxes[0].CoordToValue(e.X, out xValue, false);

                LineSeriesValueSolveResult r = s.SolveYValueAtXValue(xValue);

                double xNearest = s.FirstSampleTimeStamp + (double) r.NearestDataPointIndex / s.SamplingFrequency;
                double yNearest = s.SamplesDouble[r.NearestDataPointIndex];
            }
        }
LightningChart Support Team, PT

Post Reply