Page 1 of 1

Problem with GetLegendPos

Posted: Mon Oct 05, 2009 12:50 pm
by 10554168
Hi

I try to uses "GetLegendPos" to move the text and symbol of the legend items...

"The Legend.GetLegendPos event can be used to specify fixed Legend items X Y coordinates."

but it only move the text and not the items...

http://www.ulmer.de/file/tmp/GetLegendPos.jpg
(Test call)

Is this a bug or make I any error?

with best regards

Nils Bödeker

Re: Problem with GetLegendPos

Posted: Mon Oct 05, 2009 12:54 pm
by 10554168
Ups... i move not item... i mean the "symbol" are not moved...

Re: Problem with GetLegendPos

Posted: Mon Oct 05, 2009 2:50 pm
by yeray
Hi NilsBoedeker,

If you refer the issue discussed in this post, the only way to customize the legend I can see is drawing fully custom. Please, see this example:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  Chart1.View3D := false;

  for i:=0 to 2 do
  begin
    Chart1.AddSeries(TLineSeries.Create(self));
    Chart1[i].Title := 'Series ' + IntToStr(i+1);
    Chart1[i].FillSampleValues(50);
  end;

  Chart1[0].Title := 'long series title';
  Chart1.Legend.Visible := false;
  Chart1.MarginBottom := 10;
end;

procedure TForm1.DrawCustomLegend(Left, Top: Integer);
var i, SymbolsWidth, Separation, LegendHeight, LegendWidth: Integer;
   ItemLeftPos: array of Integer;
begin
  SymbolsWidth := 10;
  LegendHeight := Form1.Canvas.TextHeight('T') + 6;
  Separation := 15;

  SetLength(ItemLeftPos, Chart1.SeriesCount);

  LegendWidth := 0;
  for i:=0 to Chart1.SeriesCount-1 do
  begin
    ItemLeftPos[i] := Left + LegendWidth + Separation;
    LegendWidth := LegendWidth + Separation + Form1.Canvas.TextWidth(Chart1[i].Title) + SymbolsWidth;
  end;

  LegendWidth := LegendWidth + Separation;

  with Chart1.Canvas do
  begin
    Brush.Style := bsSolid;
    Rectangle(Left, Top, Left + LegendWidth, Top + LegendHeight);
    for i:=0 to Chart1.SeriesCount-1 do
    begin
      Pen.Color := Chart1[i].Color;
      Line(ItemLeftPos[i], Top + LegendHeight div 2, ItemLeftPos[i] + SymbolsWidth, Top + LegendHeight div 2);
      TextOut(ItemLeftPos[i] + SymbolsWidth + 3, Top + 2, Chart1[i].Title);
    end;
  end;
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
begin
  DrawCustomLegend(100, Chart1.Height - 25);
end;
Anyway, as you've seen, the symbols can't be repositioned with OnGetLegendPos event, only the text. So this could be something interesting to be improved in a future release. I've added it to the wish list (TV52014464).