Page 1 of 1

Changing legend position

Posted: Thu Jul 27, 2023 9:19 am
by 16489863
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?

Re: Changing legend position

Posted: Fri Jul 28, 2023 6:50 am
by yeray
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;

Re: Changing legend position

Posted: Mon Jul 31, 2023 12:20 pm
by 16489863
Thank you, that was it.