Caption goes outside of bounderies

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
EdDressel
Newbie
Newbie
Posts: 35
Joined: Mon Nov 25, 2019 12:00 am

Caption goes outside of bounderies

Post by EdDressel » Tue Sep 15, 2020 4:05 pm

In a very simple demo (attached) I have a caption on the left axis (but this problem can occur on other axes as well) where the caption can go beyond the area it should if the app is made smaller--here is an image from the attached demo where I resized the app and the left axis caption goes outside of the area:

Image

Is there a way to do word warp on the captions automatically? How can I make it so the caption always shows up?

Thank you,

Ed Dressel
Attachments
Axis Caption.zip
(1.67 KiB) Downloaded 1422 times
Ed Dressel
President
RetireReady Solutions

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

Re: Caption goes outside of bounderies

Post by Yeray » Thu Sep 17, 2020 7:44 am

Hello,

You could check if the left axis title fits in the chart height and wrap the text accordingly. Ie:

Code: Select all

procedure TForm1.Chart1Resize(Sender: TObject);
var oneLineTitle: String;
    titleHeight: Integer;
begin
  oneLineTitle:=StringReplace(Chart1.Axes.Left.Title.Text, sLineBreak, '', [rfReplaceAll]);
  Chart1.Canvas.AssignFont(Chart1.Axes.Left.Title.Font);

  if Chart1.Height < Chart1.Canvas.TextWidth(oneLineTitle) then
    Chart1.Axes.Left.Title.Text:=WrapText(oneLineTitle, sLineBreak, [' '], Chart1.Height div Chart1.Axes.Left.Title.Font.Size)
  else
    Chart1.Axes.Left.Title.Text:=oneLineTitle;

  titleHeight:=Abs((Chart1.Axes.Left.Title.Text.CountChar(#13)+1)*Chart1.Axes.Left.Title.Font.Height);
  Chart1.MarginLeft:=Round((titleHeight+5)*100/Chart1.Width);
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

EdDressel
Newbie
Newbie
Posts: 35
Joined: Mon Nov 25, 2019 12:00 am

Re: Caption goes outside of bounderies

Post by EdDressel » Thu Sep 17, 2020 2:30 pm

Thank you for your response.

I have this problem in a dozen+ charts.

Could a future version have a .WrapText property? That would be easier.
Ed Dressel
President
RetireReady Solutions

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

Re: Caption goes outside of bounderies

Post by Yeray » Fri Sep 18, 2020 5:38 am

Hello,

Right. I've added it to the public tracker (#2369)
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

EdDressel
Newbie
Newbie
Posts: 35
Joined: Mon Nov 25, 2019 12:00 am

Re: Caption goes outside of bounderies

Post by EdDressel » Fri Sep 18, 2020 5:19 pm

Your solution works well with the rendering of the TChart, but when creating an image, it does not work. See the attached demo, which has a TImage one third the height of the chart, but the caption does not size correctly.

How could this be done for rendering to an image?

Thank you,

Ed Dressel
Attachments
Axis Caption.zip
(7.43 KiB) Downloaded 1427 times
Ed Dressel
President
RetireReady Solutions

EdDressel
Newbie
Newbie
Posts: 35
Joined: Mon Nov 25, 2019 12:00 am

Re: Caption goes outside of bounderies

Post by EdDressel » Tue Sep 22, 2020 2:03 pm

Hello?
Ed Dressel
President
RetireReady Solutions

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

Re: Caption goes outside of bounderies

Post by Yeray » Tue Sep 29, 2020 12:53 pm

Hello,

Sorry for the delay here.
You could create a new method (ie WrapLeftAxisTitle) and move the code from the OnResize event to it. Then, you only have to add a few parameters to it and call it both at OnResize event and in your AssignChartToGraphic method.
Ie:
Axis Caption with Image.zip
(2.79 KiB) Downloaded 1423 times
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

EdDressel
Newbie
Newbie
Posts: 35
Joined: Mon Nov 25, 2019 12:00 am

Re: Caption goes outside of bounderies

Post by EdDressel » Tue Sep 29, 2020 8:30 pm

We are a lot closer, but not quite there: if the legend is in the way (on the top or bottom) and has a large number of items in it, the caption can get truncated.

To reproduce this change the line in the constructor so that there are 20 sample values:

Code: Select all

  Series1.FillSampleValues(20);
Thank you,

Ed Dressel
Ed Dressel
President
RetireReady Solutions

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

Re: Caption goes outside of bounderies

Post by Yeray » Wed Sep 30, 2020 7:59 am

Hello,

You could wrap the text to fit the length you wish. In this case you may want to use ChartRect.Height. Ie:

Code: Select all

procedure TForm1.WrapLeftAxisTitle(aChart: TChart; const aHeight: Integer);
var
  lOneLineTitle: String;
  lTitleHeight: Integer;
  lTitle: TChartAxisTitle;
  lHeight: Integer;
begin
  Assert(aChart <> nil);

  aChart.Draw;

  lTitle := aChart.Axes.Left.Title;
  lOneLineTitle := StringReplace(lTitle.Text, sLineBreak, ' ', [rfReplaceAll]);
  lOneLineTitle := StringReplace(lOneLineTitle, '  ', ' ', [rfReplaceAll]);
  aChart.Canvas.AssignFont(lTitle.Font);

  lHeight:=aHeight;
  if (aChart.Legend.Alignment=laTop) or (aChart.Legend.Alignment=laBottom) then
     lHeight:=aChart.ChartRect.Height*aHeight div aChart.Height;

  if lHeight < aChart.Canvas.TextWidth(lOneLineTitle) then
    lTitle.Text := WrapText(lOneLineTitle, sLineBreak, [' '], lHeight div lTitle.Font.Size)
  else
    lTitle.Text := lOneLineTitle;

  lTitleHeight:=Abs( (lTitle.Text.CountChar(#13) + 1) * lTitle.Font.Height);
  aChart.MarginLeft := Round((lTitleHeight + 5) * 100 / aChart.Width);
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

EdDressel
Newbie
Newbie
Posts: 35
Joined: Mon Nov 25, 2019 12:00 am

Re: Caption goes outside of bounderies

Post by EdDressel » Wed Sep 30, 2020 2:56 pm

Perfect. Thank you for the quick response.
Ed Dressel
President
RetireReady Solutions

Post Reply