How to get correct Legend for ColorGrid AND Line Series

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
X-ray
Newbie
Newbie
Posts: 10
Joined: Thu Dec 15, 2022 12:00 am

How to get correct Legend for ColorGrid AND Line Series

Post by X-ray » Wed Apr 03, 2024 1:59 pm

Hello,

We produce graphics like in the attached plot with TChart, where we use a ColorGrid plus 3 Line Series.
Usually we display the Colors of the Color Grid in the Legend. Now we want in addition to show a legend for the 3 Line Series. But when we include the Line Series in the Legend the Color Grid colors are vanishing, see attachment.
How can we have the Color Grid colors Legend AND a Legend for the Line Series?

Image

Image

Thanks and best regards
Attachments
Legend1CaptureNew.png
Legend1CaptureNew.png (404.6 KiB) Viewed 414 times
Legend2Capture.PNG
Legend2Capture.PNG (198.18 KiB) Viewed 414 times

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

Re: How to get correct Legend for ColorGrid AND Line Series

Post by Yeray » Thu Apr 04, 2024 7:12 am

Hello,

When the legend shows the palette, I don't see a lot of space to show any other item. Then, the ExtraLegendTool may be the most appropriate solution for this.
See the example here.
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

X-ray
Newbie
Newbie
Posts: 10
Joined: Thu Dec 15, 2022 12:00 am

Re: How to get correct Legend for ColorGrid AND Line Series

Post by X-ray » Thu Apr 04, 2024 7:36 am

Hello Yeray,

Thanks for the quick response, that indeed seems to be a way to proceed.
But how can I make sure to just just show the 3 Line Series in THAT extra legend?

Best regards,

Thomas

X-ray
Newbie
Newbie
Posts: 10
Joined: Thu Dec 15, 2022 12:00 am

Re: How to get correct Legend for ColorGrid AND Line Series

Post by X-ray » Thu Apr 04, 2024 2:03 pm

To phrase this question differently, how can I explicitly decide in which of both (or more) Legends any Series is shown?
Because Series.ShowInLegend is global for all Legends.

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

Re: How to get correct Legend for ColorGrid AND Line Series

Post by Yeray » Thu Apr 04, 2024 8:42 pm

Hello,

You could do something similar to the discussed here.

Code: Select all

uses TeEngine, Chart, Series, TeeSurfa, TeeExtraLegendTool;

type TMyExtraLegendTool=class(TExtraLegendTool)
  protected
    procedure ChartEvent(AEvent: TChartToolEvent); override;
end;

TLegendAccess=class(TChartLegend);

var Chart1: TChart;

procedure TMyExtraLegendTool.ChartEvent(AEvent: TChartToolEvent);
var i: Integer;
begin
  if AEvent=cteAfterDraw then
  begin
    for i:=0 to ParentChart.SeriesCount-1 do
        ParentChart[i].ShowInLegend:=ParentChart[i] is TLineSeries;

    if Assigned(ParentChart) and Assigned(Series) then
    begin
      if Legend.Visible then
      begin
        TLegendAccess(Legend).CalcRect;
        Legend.DrawLegend;
      end;
    end;
  end;
end;

procedure TForm1.Chart1BeforeDrawChart(Sender: TObject);
var i: Integer;
begin
  for i:=0 to Chart1.SeriesCount-1 do
      Chart1[i].ShowInLegend:=Chart1[i] is TColorGridSeries;
end;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
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;

    OnBeforeDrawChart:=Chart1BeforeDrawChart;
  end;

  with TColorGridSeries(Chart1.AddSeries(TColorGridSeries)) do
  begin
    Pen.Hide;
    FillSampleValues;
  end;

  for i:=0 to 1 do
  with TLineSeries(Chart1.AddSeries(TLineSeries)) do
  begin
    Pen.Width:=2;
    FillSampleValues;
  end;

  Chart1.Draw;

  with TMyExtraLegendTool(Chart1.Tools.Add(TMyExtraLegendTool)) do
  begin
    Series:=Chart1[1];
    Legend.LegendStyle:=lsSeries;
    Legend.Left:=Chart1.Legend.Left;
    Legend.Top:=Chart1.ChartRect.Top;
  end;
end;
ColorGrid_Line_ExtraLegend.png
ColorGrid_Line_ExtraLegend.png (41.2 KiB) Viewed 363 times
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

X-ray
Newbie
Newbie
Posts: 10
Joined: Thu Dec 15, 2022 12:00 am

Re: How to get correct Legend for ColorGrid AND Line Series

Post by X-ray » Fri Apr 05, 2024 9:38 am

Thanks a lot, we are practically there:
NewCapture.PNG
NewCapture.PNG (383.05 KiB) Viewed 350 times
I am setting the Position in the Create of the form but it looks like I need to update the position of the
extra legend before the painting.
One other quick question, how can I change/influence the texts that are shown in the Extra legend, I'd like to have other text than names of the Series?

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

Re: How to get correct Legend for ColorGrid AND Line Series

Post by Yeray » Fri Apr 05, 2024 9:55 am

Hello,
X-ray wrote:
Fri Apr 05, 2024 9:38 am
I am setting the Position in the Create of the form but it looks like I need to update the position of the
extra legend before the painting.
Yes, that's why I called Draw before creating the TExtraLegendTool at OnCreate in the example above.
X-ray wrote:
Fri Apr 05, 2024 9:38 am
One other quick question, how can I change/influence the texts that are shown in the Extra legend, I'd like to have other text than names of the Series?
We use to recommend adding extra dummy series to be shown in the legend.
You'd have one extra series for each entry you want to show in the legend. You can set their Color property to match the colors of the main series, setting your custom Title to them, but without adding any data to them.
Knowing their indexes, you could modify the code in the TMyExtraLegendTool.ChartEvent to only activate ShowInLegend property for those dummy series.
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

X-ray
Newbie
Newbie
Posts: 10
Joined: Thu Dec 15, 2022 12:00 am

Re: How to get correct Legend for ColorGrid AND Line Series

Post by X-ray » Fri Apr 05, 2024 10:07 am

Ok, I just had to populate the Title properties of the Line Series, all perfect now, thanks!
Latest2Capture.PNG
Latest2Capture.PNG (498.9 KiB) Viewed 346 times

Post Reply