Nearest Series

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Sam F
Newbie
Newbie
Posts: 45
Joined: Wed Sep 10, 2008 12:00 am

Nearest Series

Post by Sam F » Wed Jan 28, 2009 6:31 am

Is there a simple way to find the nearest series on mouse move? I have got the nearest point tool, which unfortunately does not seem to work for all series, so I can neither use it to find a point on a multi series chart, or to even find the nearest series. Unless I'm missing something?

Thanks for your help.

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

Post by Narcís » Wed Jan 28, 2009 9:21 am

Hi Sam,

Which is the exact problem you are having with nearest point tool? An example we can run "as-is" to reproduce the problem here would be helpful.

You can either post your files at news://www.steema.net/steema.public.attachments newsgroup or at our upload page.

Thanks in advance.
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

Sam F
Newbie
Newbie
Posts: 45
Joined: Wed Sep 10, 2008 12:00 am

Post by Sam F » Thu Jan 29, 2009 12:10 am

My first problem is that I have to assign a specific series to the nearest point tool, rather than having it work to find the nearest point of any series.

Code: Select all

 TLineSeries* MySeries = new TLineSeries(this);
 MySeries->AddXY(4,101);
 MySeries->AddXY(5,24);
 Chart1->AddSeries(MySeries);

 TLineSeries* MySeries2 = new TLineSeries(this);
 MySeries2->AddXY(2,52);
 MySeries2->AddXY(7,13);
 Chart1->AddSeries(MySeries2);

 //Tool wont work without limiting it to one specific series
 NearestPointTool1->Series = MySeries; 
If the tool only works for a single series at a time, that makes it useless to my needs.
My primary requirement is, wherever the mouse is, I wish to know which series is closest. But I'd also like to be able to identify which point is closest as well, regardless of which series it is in.

Code: Select all

void __fastcall TForm1::Chart1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
 //How do I get these two values, in a multi-series environment?
 int iNearestSeriesIndex = 0;
 int iNearestPointIndex = 0;

 //Highlight the nearest series
 for(int i = 0; i < Chart1->SeriesCount(); i++)
 {
  if(i == iNearestSeriesIndex ) Chart1->Series[i]->Pen->Width = 3;
  else            Chart1->Series[i]->Pen->Width = 1;
 }

 //Display value of nearest point
 Chart1->SubTitle->Text->Clear();
 Chart1->SubTitle->Text->Add(Chart1->Series[iNearestSeriesIndex ]->YValues->Value[iNearestPointIndex]);
}
I hope that makes it clearer.

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

Post by Narcís » Thu Jan 29, 2009 10:29 am

Hi Sam F,

Yes, in that case you can use NearestPoint tool like this:

Code: Select all

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var NearestPoints: array of integer;
    i, j, k, SeriesIndex, ValueIndex: Integer;
    Dist, tmp: double;
    P1, P2: TPoint;
begin
  SetLength(NearestPoints, Chart1.SeriesCount);

  for i:=0 to High(NearestPoints) do
    NearestPoints[i]:=ChartTool1.GetNearestPoint(Chart1[i], X, Y);

  for j:=0 to High(NearestPoints) do
  begin
    P1.X:=Chart1[j].CalcXPos(NearestPoints[j]);
    P1.Y:=Chart1[j].CalcYPos(NearestPoints[j]);
    P2.X:=X;
    P2.Y:=Y;
    tmp:=Distance(P1,P2);

    if ((j=0) or (tmp<Dist)) then
    begin
      Dist:=tmp;
      SeriesIndex:=j;
      ValueIndex:=NearestPoints[j];
    end;
  end;

  for k:=0 to Chart1.SeriesCount-1 do
  begin
    if k=SeriesIndex then
      (Chart1[k] as TPointSeries).Pointer.Pen.Width:=3
    else
      (Chart1[k] as TPointSeries).Pointer.Pen.Width:=1;

    Chart1[k].RefreshSeries;
  end;

  Chart1.Title.Text[0]:='Series: ' + IntToStr(SeriesIndex) +
                        ' - ValueIndex: ' + IntToStr(ValueIndex);
end;

function TForm1.Distance(Pt : TPoint; Pt2 : TPoint) : Double;
var	  dx, dy : LongInt;
begin
	dx:= pt .x - pt2.x;
	dy:= pt .y - pt2.y;
	Result := Sqrt((Dx * Dx) + (Dy * Dy));
end;
Hope this helps!
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

Sam F
Newbie
Newbie
Posts: 45
Joined: Wed Sep 10, 2008 12:00 am

Post by Sam F » Thu Jan 29, 2009 11:45 pm

That looks like it will work. Thanks for you help!

Sam F
Newbie
Newbie
Posts: 45
Joined: Wed Sep 10, 2008 12:00 am

Post by Sam F » Fri Jan 30, 2009 12:17 am

Perhaps you may also want to consider adding some of that functionality to the tool? Just a suggestion.

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

Post by Narcís » Fri Jan 30, 2009 9:21 am

Hi Sam,

Yes, I've added it to the wish-list to be considered for inclusion in future releases.
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

Post Reply