Page 1 of 1

Series Caption and Legend Overlap

Posted: Mon Oct 06, 2014 9:05 pm
by 10546565
XE2, TeeChart Pro 2014.11 04.09

In the attached demo, the series caption and legend overlap. If it is vertically enlarged, the problem does not occur--but I don't want to waist vertical spacing.

Is there a way that fixes this that doesn't waist vertical spacing?

Ed Dressel

Re: Series Caption and Legend Overlap

Posted: Tue Oct 07, 2014 9:36 am
by yeray
Hello,

I've reproduced the problem so I've added it to the public tracker:
http://bugs.teechart.net/show_bug.cgi?id=947

In the meanwhile, you could use a TAnnotationTool instead of using the bottom axis title, and recalculate the margin on the bottom manually. Ie:

Code: Select all

uses Series, Math, TeeTools;

var bottomTitle: TAnnotationTool;

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

  with Chart1.AddSeries(TBarSeries) as TBarSeries do
  begin
    FillSampleValues();
    Marks.Visible:=false;

    for i:=0 to Count-1 do
      Labels[i]:=FormatFloat('#0.##', YValue[i]) + #13 + 'Extra Line 1' + #13 + 'Extra Line 2';
  end;

  Chart1.Axes.Bottom.Title.Text:='Bottom Axis Title';

  recalcBottomMargin;
end;

procedure TForm1.Chart1Resize(Sender: TObject);
begin
  recalcBottomMargin;
end;

procedure TForm1.recalcBottomMargin;

  function CountOccurences( const SubText: string;
                            const Text: string): Integer;
  begin
    Result := Pos(SubText, Text);
    if Result > 0 then
      Result := (Length(Text) - Length(StringReplace(Text, SubText, '', [rfReplaceAll]))) div  Length(subtext);
  end;

var i, maxHeight, nLines, tmpMargin: Integer;
begin
  Chart1.Draw;

  maxHeight:=0;
  for i:=0 to Chart1[0].Count-1 do
  begin
    nLines:=CountOccurences(#13, Chart1[0].Labels[i]) + 1;
    maxHeight:=Max(maxHeight, nLines*Chart1.Canvas.TextHeight(Chart1[0].Labels[i]));
  end;

  tmpMargin:=maxHeight;

  if Chart1.Axes.Bottom.Title.Visible or Assigned(bottomTitle) then
  begin
    if not Assigned(bottomTitle) then
      bottomTitle:=Chart1.Tools.Add(TAnnotationTool) as TAnnotationTool;

    with bottomTitle do
    begin
      Text:=Chart1.Axes.Bottom.Title.Text;
      Shape.Assign(Chart1.Axes.Bottom.Title);
      Left:=Chart1.Axes.Bottom.IStartPos+(Chart1.Axes.Bottom.IAxisSize div 2) - (Chart1.Canvas.TextWidth(Text) div 2);
      tmpMargin:=Chart1.Axes.Bottom.TickLength+maxHeight+Chart1.Canvas.TextHeight(Text);
      Top:=Chart1.Axes.Bottom.PosAxis+tmpMargin;
      Chart1.MarginBottom:=tmpMargin;
    end;

    Chart1.Axes.Bottom.Title.Visible:=false;
  end;

  Chart1.MarginUnits:=muPixels;
  Chart1.MarginBottom:=tmpMargin;
end;

Re: Series Caption and Legend Overlap

Posted: Tue Oct 07, 2014 2:05 pm
by 10546565
Thank you for your response.

I merged your code with my demo and the problem still occurs--the problem for me was legend overlap with captions. See the attached demo.

Is there a work around?

Ed Dressel

Re: Series Caption and Legend Overlap

Posted: Tue Oct 07, 2014 3:14 pm
by yeray
Hi Ed,

You can do something similar than what I was doing, setting the legend to be in the bottom though a custom position and adding the necessary margin to the bottom:

Code: Select all

constructor TForm1.Create(aOwner: TComponent);
var
  I: Integer;
  lValue: Double;
  lCaption: string;
begin
  inherited;
  FAnnTool := nil;
  lValue := 100;
  for I := 1 to 5 do
  begin
    lValue := lValue * (1 + (Random * 0.2));
    lCaption := inttostr(I);
    if I = 3 then
      lCaption := lCaption + #13 + 'Extra Caption Line';
    Series1.AddBar(lValue, lCaption, clTeeColor);
  end;

  chrtSavingsNeeded.Axes.Bottom.Title.Text:='Bottom Axis Title';

  RecalcBottomMargin;
end;

procedure TForm1.RecalcBottomMargin;
  function CountOccurences( const aSubText, aText: string): Integer;
  begin
    Result := Pos(aSubText, aText);
    if Result > 0 then
      Result := (Length(aText) - Length(StringReplace(aText, aSubText, '', [rfReplaceAll]))) div  Length(aSubtext);
  end;

var i, maxHeight, nLines, tmpMargin, tmpHeight: Integer;
begin
  chrtSavingsNeeded.Draw;

  chrtSavingsNeeded.Canvas.Font.Assign(chrtSavingsNeeded.Axes.Bottom.Title.Font);
  maxHeight:=0;
  for i:=0 to chrtSavingsNeeded[0].Count-1 do
  begin
    nLines:=CountOccurences(#13, chrtSavingsNeeded[0].Labels[i]) + 1;
    maxHeight:= Max(maxHeight, nLines*chrtSavingsNeeded.Canvas.TextHeight(chrtSavingsNeeded[0].Labels[i]));
  end;

  tmpMargin:=maxHeight+chrtSavingsNeeded.Axes.Bottom.TickLength;

  if chrtSavingsNeeded.Axes.Bottom.Title.Visible or Assigned(FAnnTool) then
  begin
    if not Assigned(FAnnTool) then
      FAnnTool:=chrtSavingsNeeded.Tools.Add(TAnnotationTool) as TAnnotationTool;

    with FAnnTool do
    begin
      Text := chrtSavingsNeeded.Axes.Bottom.Title.Text;
      Shape.Assign(chrtSavingsNeeded.Axes.Bottom.Title);
      Left := chrtSavingsNeeded.Axes.Bottom.IStartPos+(chrtSavingsNeeded.Axes.Bottom.IAxisSize div 2) - (chrtSavingsNeeded.Canvas.TextWidth(Text) div 2);
      Top := chrtSavingsNeeded.Axes.Bottom.PosAxis+tmpMargin;
      tmpMargin := tmpMargin+chrtSavingsNeeded.Canvas.TextHeight(Text);
    end;

    chrtSavingsNeeded.Axes.Bottom.Title.Visible:=false;
  end;

  if chrtSavingsNeeded.Legend.Visible and (chrtSavingsNeeded.Legend.Alignment = laBottom) then
  begin
    chrtSavingsNeeded.Legend.CustomPosition:=true;
    chrtSavingsNeeded.Legend.Left:=(chrtSavingsNeeded.Width div 2) - (chrtSavingsNeeded.Legend.Width div 2);
    tmpHeight:=chrtSavingsNeeded.Legend.Height;
    chrtSavingsNeeded.Legend.Top:=chrtSavingsNeeded.Axes.Bottom.PosAxis+tmpMargin;
    tmpMargin:=tmpMargin+tmpHeight;
  end;

  chrtSavingsNeeded.MarginUnits:=muPixels;
  chrtSavingsNeeded.MarginBottom:=tmpMargin;
end;
procedure TForm1.Resize;
begin
  inherited;
  RecalcBottomMargin;
end;

Re: Series Caption and Legend Overlap

Posted: Thu Oct 16, 2014 8:25 pm
by 10546565
Thank you for your responses.

If I use the Footer.Text property for my charts sub-title, the overlap still occurs. (I tried to change the code you wrote but could not get it to work with my new demo).

Ed Dressel

Re: Series Caption and Legend Overlap

Posted: Fri Oct 17, 2014 8:34 am
by narcis
Hi Ed,

With the modifications David Berneda made to the code it's working fine now:
EdDresselCaptions.jpg
EdDresselCaptions.jpg (21.34 KiB) Viewed 15396 times
So you may expect this being fixed in the next maintenance release. If you are interested, you can also check David's new beta version here.

Re: Series Caption and Legend Overlap

Posted: Fri Oct 17, 2014 2:49 pm
by 10546565
It's difficult to release beta code to my end users--it doesn't go over very well when it doesn't work. How close is this to being a final release?

Re: Series Caption and Legend Overlap

Posted: Mon Oct 20, 2014 6:59 am
by narcis
Hello Ed,
TestAlways wrote:How close is this to being a final release?
We expect this version being out in about one month.