Axis and Labels click event

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
lukasz.walus
Newbie
Newbie
Posts: 3
Joined: Mon Nov 07, 2022 12:00 am

Axis and Labels click event

Post by lukasz.walus » Thu Aug 07, 2025 9:17 am

Hello.

How to catch event when axis or axis labels are clicked (labels back rectangle). Is there any way to calculate this area coordinates?
Zrzut ekranu 2025-08-07 111703.png
Zrzut ekranu 2025-08-07 111703.png (15.68 KiB) Viewed 470 times

Marc
Site Admin
Site Admin
Posts: 1332
Joined: Thu Oct 16, 2003 4:00 am
Location: Girona
Contact:

Re: Axis and Labels click event

Post by Marc » Thu Aug 07, 2025 9:57 am

Hello,

Code like this would do it:

Code: Select all

procedure TForm5.Chart1AfterDraw(Sender: TObject);
begin
  Chart1.canvas.TextOut(10,3,MyMessage);
end;

procedure TForm5.Chart1ClickAxis(Sender: TCustomChart; Axis: TChartAxis;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var topThreshold,bottomThreshold,clickValue : Double;

begin

  topThreshold := 400;
  bottomThreshold := 200;
  clickValue := Axis.CalcPosPoint(Y);

  if (not Axis.horizontal) then
  Begin
    if (clickValue>bottomThreshold) and (clickValue<topThreshold) then
      MyMessage := 'In range: ' +  FloatToStr(Axis.CalcPosPoint(Y))
    else
      MyMessage := 'Out of range: ' +  FloatToStr(Axis.CalcPosPoint(Y));

    Chart1.Repaint;
  end;

end;
If you want to draw the rectangle then you can use the Axis.CalcPosValue method in unison with Axis.CalcPosPoint to define the bounds of the rectangle.

This tutorial contains further information on custom drawing:
https://www.steema.com/docs/teechart/vc ... rial13.htm

Regards,
Marc Meumann
Steema Support

lukasz.walus
Newbie
Newbie
Posts: 3
Joined: Mon Nov 07, 2022 12:00 am

Re: Axis and Labels click event

Post by lukasz.walus » Thu Aug 07, 2025 10:22 am

This is only partial solution, bacause ClickAxis event is fired only when axis line is clicked, not labels or whole marked area.

Marc
Site Admin
Site Admin
Posts: 1332
Joined: Thu Oct 16, 2003 4:00 am
Location: Girona
Contact:

Re: Axis and Labels click event

Post by Marc » Thu Aug 07, 2025 1:41 pm

Hello,

Extrapolate the code, there are several ways in. For example, use the OnMouseUp event that has x,y too.

Code: Select all

procedure TForm5.Chart1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  //check x,y location against your target rectangle
end;
Regards,
Marc Meumann
Steema Support

Marc
Site Admin
Site Admin
Posts: 1332
Joined: Thu Oct 16, 2003 4:00 am
Location: Girona
Contact:

Re: Axis and Labels click event

Post by Marc » Thu Aug 07, 2025 2:38 pm

here's an example rectangle:

Code: Select all

procedure TForm5.Chart1AfterDraw(Sender: TObject);
var rect : TRect;
    leftPos, topPos, bottomPos : Integer;
begin
  leftPos := Chart1.LeftAxis.PosAxis - Chart1.LeftAxis.MaxLabelsWidth - Chart1.LeftAxis.TickLength - 2;
  topPos := Chart1.LeftAxis.CalcYPosValue(Chart1.LeftAxis.Maximum);
  bottomPos := Chart1.LeftAxis.CalcYPosValue(Chart1.LeftAxis.Minimum+((Chart1.LeftAxis.Maximum-Chart1.LeftAxis.Minimum)/2));

  rect := TRect.Create( leftPos ,topPos,Chart1.LeftAxis.PosAxis,bottomPos);

  Chart1.Canvas.Brush.Style := TBrushStyle.bsClear;
  Chart1.Canvas.Pen.Color := clRed;

  Chart1.Canvas.Rectangle(rect);
end;

lukasz.walus
Newbie
Newbie
Posts: 3
Joined: Mon Nov 07, 2022 12:00 am

Re: Axis and Labels click event

Post by lukasz.walus » Fri Aug 08, 2025 5:33 am

Thank You for all answers. I found yet another option, in my opinion a little bit cleaner:

Code: Select all

void __fastcall TFormMain::Chart1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y)
{
    TRect rect( Chart1->Axes->Left->Shape->ShapeBounds.Left,
                Chart1->Axes->Left->Shape->ShapeBounds.Top,
                Chart1->Axes->Left->Shape->ShapeBounds.Right,
                Chart1->Axes->Left->Shape->ShapeBounds.Bottom );

    if( rect.Contains(TPoint(X,Y)) ) {
        DEBUG_FUNC("Succesfull CLICK!\n");
    }
}

Marc
Site Admin
Site Admin
Posts: 1332
Joined: Thu Oct 16, 2003 4:00 am
Location: Girona
Contact:

Re: Axis and Labels click event

Post by Marc » Fri Aug 08, 2025 7:54 am

Looks good. Thanks for the feedback.

Post Reply