Series Group

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
sde
Newbie
Newbie
Posts: 21
Joined: Fri Jun 21, 2013 12:00 am

Series Group

Post by sde » Wed Apr 20, 2016 9:14 pm

I recently came across the series group feature which allows you to show or hide different groups of your chart series in the chart legend.

I see that when using the series group, if you choose to show the group names in the legend, that is all you get, the name of the group. Is there a way to show a symbol next to the group name? I ask because it would make it a lot easier to do the following.

Suppose I have 4 box series in a chart, Series1, Series2, Series3, Series4. Series1 and Series3 are red and Series2 and Series4 are blue. Under ordinary circumstances, if I make the legend visible, I will see all four series listed in the legend with two of the symbols being identical, and red, while the other two are identical and blue. But let us say instead I want my legend to only show two items, to have a red symbol with the word "odd" and a blue symbol with the word "even." Well, by using series groups, I could put Series1 and Series3 in a group and name it "Odd" and could put Series2 and Series4 in a group and call it "Even," but that will not give me the red and blue symbols that I would like to see in the legend.

Right now I end up hiding series3 and series4 from the legend and renaming Series1 to "Odd" and Series2 to "Even." But this can get unwieldy when you have 20 or 30 series and more then two groupings of colors. So I was wondering if there is an easy way to do this with groups.

Thanks!
SDE

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

Re: Series Group

Post by Yeray » Thu Apr 21, 2016 2:02 pm

Hello,

Here it is a simple example of drawing a symbol manually on the legend:

Code: Select all

procedure TForm1.Chart1AfterDraw(Sender: TObject);
var i, xPos, yPos: Integer;
begin
  xPos:=Chart1.Legend.Left + Chart1.Legend.Width - 15;

  for i:=0 to Chart1.SeriesGroups.Count-1 do
  begin
    yPos:=Chart1.Legend.Item[i].Top+2;

    with Chart1.Canvas do
    begin
      Brush.Color:=Chart1.SeriesGroups[i].Series[0].Color;
      Rectangle(Rect(xPos, yPos, xPos+10, yPos+10), 0);
    end;
  end;
end;

procedure TForm1.Chart1GetLegendRect(Sender: TCustomChart; var Rect: TRect);
begin
  Rect.Right:=rect.Right+15;
end;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  (Chart1.SeriesGroups.Add as TSeriesGroup).Name:='Red';
  (Chart1.SeriesGroups.Add as TSeriesGroup).Name:='Blue';

  for i:=0 to 10 do
  begin
    Chart1.AddSeries(TLineSeries).FillSampleValues();
    if (i mod 2 = 0) then
      Chart1[i].Color:=clRed
    else
      Chart1[i].Color:=clBlue;

    Chart1.SeriesGroups.Items[i mod 2].Add(Chart1[i]);
  end;

  Chart1.Legend.LegendStyle:=lsSeriesGroups;
  Chart1.Legend.CheckBoxes:=true;
end;
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

Post Reply