Changing legend position

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Metek
Newbie
Newbie
Posts: 9
Joined: Mon Nov 02, 2020 12:00 am
Location: Germany
Contact:

Changing legend position

Post by Metek » Thu Jul 27, 2023 9:19 am

Hi,
I want to show the legend always at the TopLeft of the chart rect:

Code: Select all

Chart1.Legend.Left := Chart1.ChartRect.Left;
Chart1.Legend.Top := Chart1.ChartRect.Top;
The chart is aligned to alClient of the main window, Legend.CustomPosition is set to true and Legend. PositionUnits is set to muPixels.
But where do I place the code to be executed after resizing the chart?
It seems to work best within OnBeforeDrawSeries, but not after maximising / restoring the window.
Can you please help?
Kind regards
Metek

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

Re: Changing legend position

Post by Yeray » Fri Jul 28, 2023 6:50 am

Hello,

You may be missing to force a Legend.CalcRect. This works fine for me:

Code: Select all

uses Chart, TeEngine, TeeProcs, Series;

var Chart1: TChart;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1:=TChart.Create(Self);
  with Chart1 do
  begin
    Parent:=Self;
    Align:=alClient;
    Color:=clWhite;
    Gradient.Visible:=False;
    Walls.Back.Color:=clWhite;
    Walls.Back.Gradient.Visible:=False;
    View3D:=False;

    AddSeries(TFastLineSeries).FillSampleValues;
    AddSeries(TFastLineSeries).FillSampleValues;

    Legend.PositionUnits:=muPixels;
    Legend.CustomPosition:=True;

    OnBeforeDrawSeries:=ChartBeforeDrawSeries;
  end;

end;

procedure TForm1.ChartBeforeDrawSeries(Sender: TObject);
begin
  RepositionLegend;
end;

procedure TForm1.RepositionLegend;
begin
  Chart1.Legend.Left:=Chart1.ChartRect.Left;
  Chart1.Legend.Top:=Chart1.ChartRect.Top;
  Chart1.Legend.CalcRect;
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

Metek
Newbie
Newbie
Posts: 9
Joined: Mon Nov 02, 2020 12:00 am
Location: Germany
Contact:

Re: Changing legend position

Post by Metek » Mon Jul 31, 2023 12:20 pm

Thank you, that was it.
Kind regards
Metek

Post Reply