How to solve nearest value, only searching on x, (not Y)?

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Queue Moderators

Post Reply
plotter
Posts: 41
Joined: Thu Apr 06, 2017 2:29 am

How to solve nearest value, only searching on x, (not Y)?

Post by plotter » Thu Sep 21, 2017 12:15 pm

How do I solve for the nearest X value in a series, only specifying the x value and not caring about the Y?
My scenario is I am creating several step line hi low series, and the x values are close in time range but not exactly at the same time.
Sometimes the cooresponding value is a few seconds before or after the others.
So I need a method that solves forward or backward for the closest x value.
The Y value is irrelevant to this search, and unknown at the time of the search...
Any Ideas ?

Thanks,
Heather

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

Re: How to solve nearest value, only searching on x, (not Y)

Post by ArctionPasi » Thu Sep 21, 2017 1:37 pm

Just iterate thru the data point array and find the nearest diff.

Like

Code: Select all

double nearestDist = double.MaxValue; 
int nearestIndex = -1; 

for(int i=0;i<series.PointCount;i++)
{
double dist = Math.Abs(x - dataPoints[i].X); 

if(dist < nearestDist)
{
nearestDist = dist; 
neartestIndex =i; 
}
}
LightningChart Support Team, PT

plotter
Posts: 41
Joined: Thu Apr 06, 2017 2:29 am

Re: How to solve nearest value, only searching on x, (not Y)

Post by plotter » Thu Sep 21, 2017 3:49 pm

Hi Pasi,
I currently am doing that, but wondered if there was a better method...
Thanks,
Heather

ArctionKestutis
Posts: 552
Joined: Mon Mar 14, 2016 9:22 am

Re: How to solve nearest value, only searching on x, (not Y)

Post by ArctionKestutis » Mon Sep 25, 2017 9:45 am

Hi Heather,

A little bit extended answer.
SolveNearestDataPointByCoord tries to find the points with shortest Euclidean distance. If you have other metric for shortest distance than "Euclidean distance", you could implement yourself.
If your points (x coordinates) in progressive order, there are some techniques, which are more optimal than just running straightforward FOR loop.

All the best.

plotter
Posts: 41
Joined: Thu Apr 06, 2017 2:29 am

Re: How to solve nearest value, only searching on x, (not Y)

Post by plotter » Tue Sep 26, 2017 8:06 pm

Hello and thanks for the details,

Yes my data is progressive, and I really would like to optimize it better than just iterating through the data...
If you might have some additional tips / pointers handy, I would really appreciate it.

Thanks again for all your help and information!
Heather


plotter
Posts: 41
Joined: Thu Apr 06, 2017 2:29 am

Re: How to solve nearest value, only searching on x, (not Y)

Post by plotter » Thu Sep 28, 2017 5:27 pm

Thank you!

Post Reply