Group: Forum Members
Last Active: 8 Years Ago
Posts: 61,
Visits: 35
|
Hi,
I asked this question before and I got some code which is working alright but it turns really slow if there are more thousands of data points. I'm trying to get the current data points for the displayed lines at a user chosen coordinate on the graph.
I got "this"(I made some changes) code from you before: //j is a counter in a loop of line series NLineSeries graphLine = (NLineSeries)chart.Series[j]; if (graphLine.Visible) { NViewToScale2DTransformation view2Scale = new NViewToScale2DTransformation(nChartControl1.View.Context, chart, (int)StandardAxis.PrimaryX, (int)StandardAxis.PrimaryY);
NVector2DD vecScale = new NVector2DD(); view2Scale.Transform(new NPointF(graphLinePanel.Left, 0), ref vecScale);
int count = graphLine.Values.Count;
if (count > 0) { minIndex = 0; double minDistance = GetDistanceFromDataPoint(graphLine, 0, vecScale); graphLine.FillStyles.Clear();
int i = 1; double curDistance = GetDistanceFromDataPoint(graphLine, i, vecScale); while (i < count && minDistance >= curDistance) { curDistance = GetDistanceFromDataPoint(graphLine, i, vecScale); if (curDistance < minDistance) { minDistance = curDistance; minIndex = i; } i++; } } }
double GetDistanceFromDataPoint(NLineSeries graphLine, int index, NVector2DD vec) { double x; double y = (double)graphLine.Values[index];
if (graphLine.UseXValues) { x = (double)graphLine.XValues[index]; } else { x = index; }
double dx = vec.X - x; double dy = vec.Y - y;
return dx * dx; }
Just wondering if you have any faster function implemented by now? Or do you think that could be rewritten to be more efficient?
Thank you, Daniel
|
Group: Forum Members
Last Active: Yesterday @ 1:54 AM
Posts: 3,054,
Visits: 4,009
|
Hi Daniel, The solution below is just a reference solution - you can of course write your own that uses a more advanced search method. Note that all types of search rely on the data being organized somehow and require some additional preprocessing of the data - for this type of search you can use a k-d tree: http://en.wikipedia.org/wiki/Kd-tree There are a number of open source implementations you can use in your code.
Best Regards, Nevron Support Team
|
Group: Forum Members
Last Active: 8 Years Ago
Posts: 61,
Visits: 35
|
So there's no built in function, I should just improve the search, am I right?
Daniel
|
Group: Forum Members
Last Active: 8 Years Ago
Posts: 61,
Visits: 35
|
Hi,
I was thinking about something more simple which does not include any searching in a data set, as if it does then it will always hit a certain data set size when it turns slow.
I thought hat would work:
int count = graphLine.Values.Count; double minDistance = GetDistanceFromDataPoint(graphLine, 0, vecScale); double maxDistance = GetDistanceFromDataPoint(graphLine, count-1, vecScale);
int index = Convert.ToInt32((minDistance / (minDistance + maxDistance)) * (count-1));
But unfortunately it does not return accurate points. Do you think that there's any way to make that work, or I should really just stick with the searching?
Thanks, Daniel
|
Group: Forum Members
Last Active: 8 Years Ago
Posts: 61,
Visits: 35
|
OK, I know why it does not work. I assumed that the list is in some kind of order as I need to take into account just the X axis locations. In my head I thought it would built up the way like that
Distance: 3 2 1 0 1 2 3 4 5 |-----|-----|-----|-----|-----|-----|-----|-----|
0 Distance is the point that I am looking for. My graph is a "real time graph", I mean I keep adding points to it, but the points are always in order(x sec after the previous data point), because of that I made the previously mentioned assumption(x coordinates are in order.)
I do not think that I need kd-tree as I should be working just with 1D points(just x), I still could put a sorting algorithm(like quicksort) on it and take the first, but I still think that it could be done neater.
Any suggestion, idea?
Daniel
|
Group: Forum Members
Last Active: 8 Years Ago
Posts: 61,
Visits: 35
|
stupid me, no point to sort it, finding minimum always faster... but to find it I still should check all of the data points because of the unorganized list, so I should not even use the previously mentioned while loop but a stepping through one.
I still think that's a bad practice. I do not know the logic behind the vector you use(it is not the way I tought) so I have no idea what to use.
|