Page 1 of 1

problems with TNearestTool and TLineSeries

Posted: Tue Jun 26, 2007 8:51 am
by 8575642
Hi all,

I'm trying to use the TNearestTool to work out the time (x value) of the point (on a TLineSeries) closest to the mouse. When the user clicks a button, a nearest tool is created:

Code: Select all

TNearestTool *nearTool = new TNearestTool(TrendChart);
TrendChart->Tools->Add(nearTool);
nearTool->LinePen->Color = clBlack;
nearTool->LinePen->Width = 2;
nearTool->Brush->Color = clRed;
nearTool->Pen->Color = clRed;
nearTool->Size = 5;
nearTool->Series = series;
nearTool->Active = true;
As they move their mouse around, the TNearestTool snaps to the closest point correctly. Then when they double-click, I have this code:

Code: Select all

for (int i=0; i < TrendChart->Tools->Count; i++)
{
   //Find the nearest tool tool
   if ( dynamic_cast<TNearestTool*>(TrendChart->Tools->Items[i]) != 0 )
   {
        TNearestTool *nearTool = (TNearestTool*)TrendChart->Tools->Items[i];
        TDateTime tdtTime = nearTool->Series->XValue[nearTool->GetNearestPoint(__classid(TLineSeries), nearTool->Series, Mouse->CursorPos.x, Mouse->CursorPos.y)];
        //Etc
The problem is that GetNearestPoint does not seem return the correct index value. It's always at least 2 or three series points out, sometimes as many as 10 (which can be up to an hour on the chart).

Has anyone else seen this? Am I doing anything noticeably wrong?

Cheers,

Andrew

Posted: Tue Jun 26, 2007 10:22 am
by yeray
Hi Andrew,

The problem is that Mouse->CursorPos.x and Mouse->CursorPos.y don't return the accurate values of the chart and thats what causes a wrong return in GetNearestPoint method.
Instead of those values you can use onMouseMove event to save X and Y coordinates to global variables and use those variables in the GetNearestPoint method.
This works fine for us here.

Posted: Wed Jun 27, 2007 5:05 am
by 8575642
Awesome, that works perfectly. Thanks very much.