Page 1 of 1
How to get correct Legend for ColorGrid AND Line Series
Posted: Wed Apr 03, 2024 1:59 pm
by 16594956
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?
Thanks and best regards
Re: How to get correct Legend for ColorGrid AND Line Series
Posted: Thu Apr 04, 2024 7:12 am
by yeray
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.
Re: How to get correct Legend for ColorGrid AND Line Series
Posted: Thu Apr 04, 2024 7:36 am
by 16594956
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
Re: How to get correct Legend for ColorGrid AND Line Series
Posted: Thu Apr 04, 2024 2:03 pm
by 16594956
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.
Re: How to get correct Legend for ColorGrid AND Line Series
Posted: Thu Apr 04, 2024 8:42 pm
by yeray
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 (41.2 KiB) Viewed 29774 times
Re: How to get correct Legend for ColorGrid AND Line Series
Posted: Fri Apr 05, 2024 9:38 am
by 16594956
Thanks a lot, we are practically there:
- NewCapture.PNG (383.05 KiB) Viewed 29761 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?
Re: How to get correct Legend for ColorGrid AND Line Series
Posted: Fri Apr 05, 2024 9:55 am
by yeray
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.
Re: How to get correct Legend for ColorGrid AND Line Series
Posted: Fri Apr 05, 2024 10:07 am
by 16594956
Ok, I just had to populate the Title properties of the Line Series, all perfect now, thanks!
- Latest2Capture.PNG (498.9 KiB) Viewed 29757 times