Page 1 of 1

Legend custom position

Posted: Fri Feb 01, 2019 1:31 pm
by 16584466
Hello,

I have an issue with placing the legend in a custom position, I have some code set inside the OnGetLegendRect to set the Rect position but I need to get an axis IAxisSize (I have a custom bottom axis). Once my application loads data to the plot the legend appears a little off position but once I move the mouse over the plot the legend jumps back to the correct position. Is there a way to make the chart recalculate the values IStartPos, IStartEnd etc?

Re: Legend custom position

Posted: Mon Feb 11, 2019 9:48 am
by yeray
Hello,

You probably need to force a chart repaint so some internal properties are calculated the second time the OnGetLegendRect event is fired.

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
//...
  Chart1.Draw;
end;

Re: Legend custom position

Posted: Thu Feb 28, 2019 6:10 am
by 16584466
Hello,

Thank you for your reply, I already tried that, has an affect on the position of the legend but it still moves once the mouse pasts over the plot.

Regards

Re: Legend custom position

Posted: Thu Feb 28, 2019 9:01 am
by yeray
Hello,

If you could send us a simple example project to reproduce the problem here it could help us to provide a fix/workaround.

Re: Legend custom position

Posted: Fri Mar 01, 2019 2:55 pm
by 16584466
Hello,

It looks like I cannot reproduce the issue outside my application so I would like to ask this question, I need the legend rectangle to be drawn in a specific position inside the chart area for example 20 pixels above bottom axis and 20 pixels right of the left axis (plot can be empty of data but there is always at least 1 line series attached)

Regards

Re: Legend custom position

Posted: Fri Mar 01, 2019 4:04 pm
by yeray
Hello,

You could use the Legend in CustomPosition. Ie:

Code: Select all

uses Series;

procedure TForm1.Chart1BeforeDrawSeries(Sender: TObject);
begin
  Chart1.Legend.CustomPosition:=True;
  Chart1.Legend.Left:=TChart(Sender).ChartRect.Left+20;
  Chart1.Legend.Top:=TChart(Sender).ChartRect.Bottom-20-Chart1.Legend.Height;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=False;
  Chart1.Legend.LegendStyle:=lsSeries;
  Chart1.AddSeries(TLineSeries);

  Chart1.Draw;
end;
The only problem is the ChartRect is defined later than than the Legend drawing. That's why you should make sure the chart has been redrawn before using ChartRect to set the Legend position.