Series Caption and Legend Overlap

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Series Caption and Legend Overlap

Post by TestAlways » Mon Oct 06, 2014 9:05 pm

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
Attachments
Legend and Caption Overwrite.rar
(1.84 KiB) Downloaded 855 times

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

Re: Series Caption and Legend Overlap

Post by Yeray » Tue Oct 07, 2014 9:36 am

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;
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

TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Re: Series Caption and Legend Overlap

Post by TestAlways » Tue Oct 07, 2014 2:05 pm

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
Attachments
Legend and Caption Overwrite.rar
(2.36 KiB) Downloaded 896 times

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

Re: Series Caption and Legend Overlap

Post by Yeray » Tue Oct 07, 2014 3:14 pm

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;
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

TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Re: Series Caption and Legend Overlap

Post by TestAlways » Thu Oct 16, 2014 8:25 pm

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
Attachments
Legend and Caption Overwrite.rar
(2.63 KiB) Downloaded 844 times

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: Series Caption and Legend Overlap

Post by Narcís » Fri Oct 17, 2014 8:34 am

Hi Ed,

With the modifications David Berneda made to the code it's working fine now:
EdDresselCaptions.jpg
EdDresselCaptions.jpg (21.34 KiB) Viewed 15369 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.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Re: Series Caption and Legend Overlap

Post by TestAlways » Fri Oct 17, 2014 2:49 pm

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?

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: Series Caption and Legend Overlap

Post by Narcís » Mon Oct 20, 2014 6:59 am

Hello Ed,
TestAlways wrote:How close is this to being a final release?
We expect this version being out in about one month.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply