Hello.
How to catch event when axis or axis labels are clicked (labels back rectangle). Is there any way to calculate this area coordinates?
Axis and Labels click event
Re: Axis and Labels click event
Hello,
Code like this would do it:
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
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;
This tutorial contains further information on custom drawing:
https://www.steema.com/docs/teechart/vc ... rial13.htm
Regards,
Marc Meumann
Steema Support
-
- Newbie
- Posts: 3
- Joined: Mon Nov 07, 2022 12:00 am
Re: Axis and Labels click event
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
Hello,
Extrapolate the code, there are several ways in. For example, use the OnMouseUp event that has x,y too.
Regards,
Marc Meumann
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;
Marc Meumann
Steema Support
Re: Axis and Labels click event
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;
-
- Newbie
- Posts: 3
- Joined: Mon Nov 07, 2022 12:00 am
Re: Axis and Labels click event
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
Looks good. Thanks for the feedback.