Find the nearest point on Bottom-Axis when clicking chart

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Christian Ziegelt
Newbie
Newbie
Posts: 12
Joined: Mon Feb 17, 2014 12:00 am

Find the nearest point on Bottom-Axis when clicking chart

Post by Christian Ziegelt » Thu Sep 25, 2014 2:23 pm

Dear all,

I am searching for the easiest way to find the nearest point from a "click" position on the chart.
I am interested only in results from one series (IndexSeries) and only regarding the x position.

In former times this could be established by the NearestTool - unfortunately it is working on all axis now.

Any ideas ?
Thanks in advance for any tip.
Best regards
Christian

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

Re: Find the nearest point on Bottom-Axis when clicking chart

Post by Yeray » Fri Sep 26, 2014 10:12 am

Hi Christian,

If you know the series you want to find the nearest XValue to the mouse X position clicked, you can loop the values to search it. Ie:

Code: Select all

uses Series, Math;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=False;

  Chart1.AddSeries(TLineSeries).FillSampleValues();
  Chart1.OnClick:=Chart1OnClick;
end;

procedure TForm1.Chart1OnClick(Sender: TObject);
var clickedXValue: Double;
    clickedXPoint, i, tmpDist, minDist, minDistIndex: Integer;
begin
  clickedXPoint:=Chart1.GetCursorPos.X;
  clickedXValue:=Chart1.Axes.Bottom.CalcPosPoint(clickedXPoint);
  Caption:='clickedXValue: ' + FormatFloat('#0.##', clickedXValue);

  if (Chart1.SeriesCount>0) and (Chart1[0].Count>0) then
  begin
    minDist:=Abs(clickedXPoint-Chart1[0].CalcXPos(0));
    minDistIndex:=0;
    for i:=1 to Chart1[0].Count-1 do
    begin
      tmpDist:=Abs(clickedXPoint-Chart1[0].CalcXPos(i));
      if tmpDist<minDist then
      begin
        minDist:=tmpDist;
        minDistIndex:=i;
      end;
    end;
    Caption:=Caption + ', Nearest Index: ' + IntToStr(minDistIndex) + ', ' +
       'Nearest X Value: ' + FormatFloat('#0.##', Chart1[0].XValue[minDistIndex]);
  end;
end;
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

Post Reply