Page 1 of 1

Caption goes outside of bounderies

Posted: Tue Sep 15, 2020 4:05 pm
by 16587426
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

Re: Caption goes outside of bounderies

Posted: Thu Sep 17, 2020 7:44 am
by yeray
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;

Re: Caption goes outside of bounderies

Posted: Thu Sep 17, 2020 2:30 pm
by 16587426
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.

Re: Caption goes outside of bounderies

Posted: Fri Sep 18, 2020 5:38 am
by yeray
Hello,

Right. I've added it to the public tracker (#2369)

Re: Caption goes outside of bounderies

Posted: Fri Sep 18, 2020 5:19 pm
by 16587426
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

Re: Caption goes outside of bounderies

Posted: Tue Sep 22, 2020 2:03 pm
by 16587426
Hello?

Re: Caption goes outside of bounderies

Posted: Tue Sep 29, 2020 12:53 pm
by yeray
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 146 times

Re: Caption goes outside of bounderies

Posted: Tue Sep 29, 2020 8:30 pm
by 16587426
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

Re: Caption goes outside of bounderies

Posted: Wed Sep 30, 2020 7:59 am
by yeray
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;

Re: Caption goes outside of bounderies

Posted: Wed Sep 30, 2020 2:56 pm
by 16587426
Perfect. Thank you for the quick response.