How to make space in the legend for drawing?

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Saffire
Newbie
Newbie
Posts: 1
Joined: Tue Dec 19, 2023 12:00 am

How to make space in the legend for drawing?

Post by Saffire » Mon Jan 08, 2024 2:37 pm

I was using TeeChart Pro version 2011 and used the following to make space in the legend to the left, for drawing the contour line types (in C++)

Code: Select all

GetLegendRectEvent(TCustomChart *Sender, TRect &Rect)
{
   int ExtraLegendWidth=floor((double)(Rect.Right-Rect.Left)*0.5+0.5);
   if (ExtraLegendWidth<35) ExtraLegendWidth=35;
   Rect.Left-=ExtraLegendWidth;
} 
This placed the symbols to the left of the text (which is right aligned), and left space on the left for drawing.
Then drew the lines in the legend to the left of the symbols, text using the ChartAfterDrawEvent
OldVers.png
OldVers.png (2.22 KiB) Viewed 21355 times
Now I am using Teechart Pro 2023 and using the same code, the legend places the symbols as left aligned and the text as right aligned, so that the lines are now drawn through the symbols.
NewVers.png
NewVers.png (3.16 KiB) Viewed 21355 times
I have tried setting TextSymbolGap in the GetLegendRectEvent, but this does not seem to help.

How to I get the symbols to lie just before the right aligned text, as before, when I add extra space to the legend on the left?

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

Re: How to make space in the legend for drawing?

Post by Yeray » Thu Jan 11, 2024 12:13 pm

Hello,

You could set the Symbol.Position to spRight, but then the texts are moved to the left and it's also broken.
Could you please confirm it?

Code: Select all

Legend.Symbol.Position:=spRight;
To work around this, you could set the Symbol.Position to spLeft at the OnBeforeDrawChart event and set it to spRight at OnGetLegendRect event.

Here the full test example I used, in delphi:

Code: Select all

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;
    Axes.Left.Grid.Hide;
    Axes.Bottom.Grid.Hide;

    with TContourSeries(AddSeries(TContourSeries)) do
    begin
      UsePalette:=False;
      UseColorRange:=True;
      StartColor:=clRed;
      EndColor:=clBlue;

      FillSampleValues;
    end;

    Legend.TextAlignment:=taRightJustify;
    OnGetLegendRect:=GetLegendRect;
    OnBeforeDrawChart:=BeforeDrawChart;
    OnAfterDraw:=AfterDrawChart;
  end;
end;

procedure TForm1.GetLegendRect(Sender: TCustomChart; var Rect: TRect);
var ExtraLegendWidth: Integer;
begin
  ExtraLegendWidth:=Round((Rect.Right-Rect.Left)*0.5+0.5);
  if (ExtraLegendWidth<35) then
     ExtraLegendWidth:=35;

  Rect.Left:=Rect.Left-ExtraLegendWidth;
  Sender.Legend.Symbol.Position:=spRight;
end;

procedure TForm1.BeforeDrawChart(Sender: TObject);
begin
  Chart1.Legend.Symbol.Position:=spLeft;
end;

procedure TForm1.AfterDrawChart(Sender: TObject);
var i: Integer;
begin
  with Chart1.Legend do
    for i:=0 to Items.Count-1 do
    begin
      Chart1.Canvas.Pen.Width:=3;
      Chart1.Canvas.Pen.Color:=TContourSeries(Chart1[0]).LegendItemColor(Items.Count-i-1);
      Chart1.Canvas.HorizLine3D(Left+4, Items[i].SymbolRect.Left - 4, Items[i].SymbolRect.Top + (Items[i].SymbolRect.Bottom - Items[i].SymbolRect.Top) div 2, 0);
    end;
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