Multiple Legends with Chart Draw Event Overrides

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
erzsebet
Newbie
Newbie
Posts: 21
Joined: Tue Apr 13, 2010 12:00 am

Multiple Legends with Chart Draw Event Overrides

Post by erzsebet » Tue Sep 28, 2010 11:03 pm

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

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: Multiple Legends with Chart Draw Event Overrides

Post by Narcís » Wed Sep 29, 2010 10:51 am

Hi erzsebet,

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
Image Image Image Image Image Image
Instructions - How to post in this forum

erzsebet
Newbie
Newbie
Posts: 21
Joined: Tue Apr 13, 2010 12:00 am

Re: Multiple Legends with Chart Draw Event Overrides

Post by erzsebet » Wed Sep 29, 2010 10:52 pm

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).
TwoLegends_RightAligned.PNG
TwoLegends_RightAligned.PNG (67.14 KiB) Viewed 5163 times
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
Thank you!

-erzsebet
Attachments
TwoLegends_Bottom.PNG
TwoLegends_Bottom.PNG (72.01 KiB) Viewed 5150 times

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: Multiple Legends with Chart Draw Event Overrides

Post by Narcís » Thu Sep 30, 2010 1:00 pm

Hi erzsebet,
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).
Yes, code below works fine for me:

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;
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
Yes, you can change panel margins, for example:

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
Image Image Image Image Image Image
Instructions - How to post in this forum

erzsebet
Newbie
Newbie
Posts: 21
Joined: Tue Apr 13, 2010 12:00 am

Re: Multiple Legends with Chart Draw Event Overrides

Post by erzsebet » Thu Sep 30, 2010 4:42 pm

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:

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;
Thanks!!!

-erzsebet

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: Multiple Legends with Chart Draw Event Overrides

Post by Narcís » Wed Oct 06, 2010 9:47 am

Hi erzsebet,

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
Image Image Image Image Image Image
Instructions - How to post in this forum

erzsebet
Newbie
Newbie
Posts: 21
Joined: Tue Apr 13, 2010 12:00 am

Re: Multiple Legends with Chart Draw Event Overrides

Post by erzsebet » Thu Oct 07, 2010 8:55 pm

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

Post Reply