Page 1 of 1

Is this possible?

Posted: Tue Jul 03, 2007 1:26 pm
by 9245569
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

Posted: Tue Jul 03, 2007 2:15 pm
by narcis
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;

Posted: Tue Jul 03, 2007 3:13 pm
by 9245569
This is just excellent!!!!! Thank you very much for your time.

Kindest regards