Page 1 of 1
Axis and Labels click event
Posted: Thu Aug 07, 2025 9:17 am
by 16494676
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 (15.68 KiB) Viewed 430 times
Re: Axis and Labels click event
Posted: Thu Aug 07, 2025 9:57 am
by Marc
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
Re: Axis and Labels click event
Posted: Thu Aug 07, 2025 10:22 am
by 16494676
This is only partial solution, bacause ClickAxis event is fired only when axis line is clicked, not labels or whole marked area.
Re: Axis and Labels click event
Posted: Thu Aug 07, 2025 1:41 pm
by Marc
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
Re: Axis and Labels click event
Posted: Thu Aug 07, 2025 2:38 pm
by Marc
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;
Re: Axis and Labels click event
Posted: Fri Aug 08, 2025 5:33 am
by 16494676
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");
}
}
Re: Axis and Labels click event
Posted: Fri Aug 08, 2025 7:54 am
by Marc
Looks good. Thanks for the feedback.