Page 1 of 1

How to make space in the legend for drawing?

Posted: Mon Jan 08, 2024 2:37 pm
by 16597273
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 8188 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 8188 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?

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

Posted: Thu Jan 11, 2024 12:13 pm
by yeray
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;