Hello!
I have a chart that has a customized legend. The events that I override to draw this customized legend are:
TCustomAxisPanel.OnBeforeDrawChart
TCustomChart.OnGetLegendText
TCustomChart.AfterDraw
TXustomChartLegend.Symbol.OnDraw
Now I need to add a second legend to the chart that also needs to be customized like the first legend. I have tried calling TCustomChart.Legend.DrawLegend in the TCustomchart.AfterDraw event. This does add another legend to the chart, but it is an uncustomized replica of the first legend I added.
How can I add a second legend, and still use all of the event overrides to get the customized appearance I require?
Thanks,
erzsebet
Multiple Legends with Chart Draw Event Overrides
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Re: Multiple Legends with Chart Draw Event Overrides
Hi erzsebet,
What about using TExtraLegendTool tool?
What about using TExtraLegendTool tool?
Best Regards,
Narcís Calvet / Development & Support Steema Software Avinguda Montilivi 33, 17003 Girona, Catalonia Tel: 34 972 218 797 http://www.steema.com |
Instructions - How to post in this forum |
Re: Multiple Legends with Chart Draw Event Overrides
Narcís, hello!
Thank you for the suggestion to use TExtraLegendTool! I was able to make a small project that does exactly what I need and am confident I'll be able to translate what I've learned about TExtraLegendTool into my main program. I do still have a couple of questions about legend alignment, if you would be take a look at those questions, I would appreciate it!
When I have the two legends right aligned, I would like both of the legends to be the width of whichever legend is larger. In the case below, I would like "Size by That" to be the same width as "Color By This." (Note that this is just an example & the legends don't actually correspond to the points in the series). If the legends are aligned at the bottom, is there a way to get the chart to resize so that both legends are completely visible? In the image below, the "Size by That" legend is cut off. Thank you!
-erzsebet
Thank you for the suggestion to use TExtraLegendTool! I was able to make a small project that does exactly what I need and am confident I'll be able to translate what I've learned about TExtraLegendTool into my main program. I do still have a couple of questions about legend alignment, if you would be take a look at those questions, I would appreciate it!
When I have the two legends right aligned, I would like both of the legends to be the width of whichever legend is larger. In the case below, I would like "Size by That" to be the same width as "Color By This." (Note that this is just an example & the legends don't actually correspond to the points in the series). If the legends are aligned at the bottom, is there a way to get the chart to resize so that both legends are completely visible? In the image below, the "Size by That" legend is cut off. Thank you!
-erzsebet
- Attachments
-
- TwoLegends_Bottom.PNG (72.01 KiB) Viewed 5191 times
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Re: Multiple Legends with Chart Draw Event Overrides
Hi erzsebet,
Yes, code below works fine for me:When I have the two legends right aligned, I would like both of the legends to be the width of whichever legend is larger. In the case below, I would like "Size by That" to be the same width as "Color By This." (Note that this is just an example & the legends don't actually correspond to the points in the series).
Code: Select all
procedure TForm1.FormCreate(Sender: TObject);
begin
Chart1.Draw;
end;
procedure TForm1.Chart1AfterDraw(Sender: TObject);
begin
ChartTool1.Legend.CustomPosition:=True;
ChartTool1.Legend.Left:=Chart1.Legend.Left;
ChartTool1.Legend.Top:=Chart1.Legend.ShapeBounds.Bottom + 10;
ChartTool1.Legend.Width:=Chart1.Legend.Width;
ChartTool1.Legend.ShapeBounds.Right:=Chart1.Legend.ShapeBounds.Bottom;
end;
procedure TForm1.Chart1GetLegendRect(Sender: TCustomChart;
var Rect: TRect);
begin
Rect.Right:=Chart1.Legend.ShapeBounds.Right;
end;
Yes, you can change panel margins, for example:If the legends are aligned at the bottom, is there a way to get the chart to resize so that both legends are completely visible? In the image below, the "Size by That" legend is cut off.
The attachment TwoLegends_Bottom.PNG is no longer available
Code: Select all
Chart3.MarginBottom:=15;
Best Regards,
Narcís Calvet / Development & Support Steema Software Avinguda Montilivi 33, 17003 Girona, Catalonia Tel: 34 972 218 797 http://www.steema.com |
Instructions - How to post in this forum |
Re: Multiple Legends with Chart Draw Event Overrides
Narcís, hello -
Thank you for the code snips!! I am now able to get the legends to be the same width and also to change the chart margins so that the legend is not truncated if it aligned to the bottom. In my sample code, I am calculating the Chart's MarginBottom in the FormShow event. Is there a particular TChart event where this calculation can/should be placed?
Here is my version of OnGetLegendRect event to get the effect I wanted. Here is the relevant code:
Thanks!!!
-erzsebet
Thank you for the code snips!! I am now able to get the legends to be the same width and also to change the chart margins so that the legend is not truncated if it aligned to the bottom. In my sample code, I am calculating the Chart's MarginBottom in the FormShow event. Is there a particular TChart event where this calculation can/should be placed?
Here is my version of OnGetLegendRect event to get the effect I wanted. Here is the relevant code:
Code: Select all
{ AfterDraw overrides the TCustomChart OnAfterDraw event to reset all of the events and legend settings to their defaults prior to drawing other charts. }
procedure TForm2.AfterDraw(Sender: TObject);
begin
FSizeLegendTool.Legend.CustomPosition := True;
FSizeLegendTool.Legend.Left := Chart1.Legend.Left;
FSizeLegendTool.Legend.Top := Chart1.Legend.ShapeBounds.Bottom + 10;
Chart1.Legend := FSizeLegendTool.Legend;
Chart1.Legend.Title.Text := Self.SetLegendTitleText;
Chart1.Legend.DrawLegend;
end;
Code: Select all
{ GetLegendRect overrides the TChart OnGetLegendRect event to allow all legends to display with the same width as the default legend. }
procedure TForm2.GetLegendRect(Sender: TCustomChart; var Rect: TRect);
begin
try
if Chart1.Legend.ShapeBounds.Right > FLegendWidth then
{ Store the width of this legend. }
FLegendWidth := Chart1.Legend.ShapeBounds.Right
else if Chart1.Legend.ShapeBounds.Right < FLegendWidth then
{ Apply the saved width to ensure legends are the same size. }
Rect.Right := FLegendWidth;
except
on E : Exception do begin
FDebugStr := 'TChartScatterPlotDef.GetLegendRect: Exception caught - ' + E.Message;
end;
end;
end;
-erzsebet
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Re: Multiple Legends with Chart Draw Event Overrides
Hi erzsebet,
What about using TChart's OnAfterDraw event?
What about using TChart's OnAfterDraw event?
Best Regards,
Narcís Calvet / Development & Support Steema Software Avinguda Montilivi 33, 17003 Girona, Catalonia Tel: 34 972 218 797 http://www.steema.com |
Instructions - How to post in this forum |
Re: Multiple Legends with Chart Draw Event Overrides
Narcís, hi -
We've decided to align our legends to the right of the chart, and are no longer pursuing drawing beneath the chart. If we do need to resize the chart's bottom margin, I will be sure to try using the Chart.OnAfterDraw event.
Thanks!
-erzsebet
We've decided to align our legends to the right of the chart, and are no longer pursuing drawing beneath the chart. If we do need to resize the chart's bottom margin, I will be sure to try using the Chart.OnAfterDraw event.
Thanks!
-erzsebet