problems with TNearestTool and TLineSeries

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Andrew
Newbie
Newbie
Posts: 11
Joined: Thu Feb 06, 2003 5:00 am

problems with TNearestTool and TLineSeries

Post by Andrew » Tue Jun 26, 2007 8:51 am

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

Yeray
Site Admin
Site Admin
Posts: 9602
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Post by Yeray » Tue Jun 26, 2007 10:22 am

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.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Andrew
Newbie
Newbie
Posts: 11
Joined: Thu Feb 06, 2003 5:00 am

Post by Andrew » Wed Jun 27, 2007 5:05 am

Awesome, that works perfectly. Thanks very much.

Post Reply