Page 1 of 1
Nearest Series
Posted: Wed Jan 28, 2009 6:31 am
by 10550286
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.
Posted: Wed Jan 28, 2009 9:21 am
by narcis
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.
Posted: Thu Jan 29, 2009 12:10 am
by 10550286
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.
Posted: Thu Jan 29, 2009 10:29 am
by narcis
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!
Posted: Thu Jan 29, 2009 11:45 pm
by 10550286
That looks like it will work. Thanks for you help!
Posted: Fri Jan 30, 2009 12:17 am
by 10550286
Perhaps you may also want to consider adding some of that functionality to the tool? Just a suggestion.
Posted: Fri Jan 30, 2009 9:21 am
by narcis
Hi Sam,
Yes, I've added it to the wish-list to be considered for inclusion in future releases.