Is this possible?

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
johnnix
Newbie
Newbie
Posts: 5
Joined: Mon Jun 18, 2007 12:00 am

Is this possible?

Post by johnnix » Tue Jul 03, 2007 1:26 pm

Hello all,

I have a TChart with several TLineSeries added by code. I need to use the TNearestTool but it should "look" at the series that is closer to the mouse. Can anyone give me a hint on how to do that?

Regards

P.S. TeeChart Pro 7.0

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Tue Jul 03, 2007 2:15 pm

Hi johnnix,

Each TNearestTool is intended to have a series assigned. However, to achieve what you request you can do something like this:

Code: Select all

uses Series, Math;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;

begin
  for i:=0 to 4 do
  begin
    Chart1.AddSeries(TLineSeries.Create(self));
    Chart1[i].FillSampleValues();
  end;
end;

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var i, tmpIndex, SeriesIndex: Integer;
    tmpDistance, currDistance: Real;
begin
  tmpDistance:=sqrt(Power(Chart1.Width,2)+   //tmpDistance is chart's diagonal,
                    Power(Chart1.Height,2)); //which is the max. distance possible
  SeriesIndex:=-1;

  for i:=0 to Chart1.SeriesCount-1 do
  begin
    tmpIndex:=ChartTool1.GetNearestPoint(Chart1[i],X,Y);

    currDistance:=Abs(Distance(Chart1[i].CalcXPos(tmpIndex),
                               Chart1[i].CalcYPos(tmpIndex), X, Y));

    if (currDistance < tmpDistance) then
    begin
      SeriesIndex:=i;
      tmpDistance:=currDistance;
    end;
  end;

  if SeriesIndex<>-1 then
    ChartTool1.Series:=Chart1[SeriesIndex];
end;

function TForm1.Distance(XPos, YPos, X, Y: Integer): Real;
begin
  Result:=sqrt(Power(XPos-X,2)+Power(YPos-Y,2));
end;
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

johnnix
Newbie
Newbie
Posts: 5
Joined: Mon Jun 18, 2007 12:00 am

Post by johnnix » Tue Jul 03, 2007 3:13 pm

This is just excellent!!!!! Thank you very much for your time.

Kindest regards

Post Reply