Recent

Author Topic: TAChartListBox Autoextend possible? [SOLVED]  (Read 465 times)

AL

  • Full Member
  • ***
  • Posts: 159
TAChartListBox Autoextend possible? [SOLVED]
« on: November 20, 2019, 04:59:43 pm »
Is there a way to make the tchartlistbox autoextend to show a new long serie name?

Thanks   
AL
« Last Edit: January 08, 2020, 10:37:02 pm by AL »
Laz 2.1.0, fpc 3.3.1, Win10
Laz 2.1.0, fpc3.3.1, Mac OS Mojave running on VMWare

wp

  • Hero Member
  • *****
  • Posts: 7330
Re: TAChartListBox Autoextend possible?
« Reply #1 on: November 20, 2019, 05:28:57 pm »
No, at least not by a built-in property or method. But you could hook into the OnPopulate event of the ChartListbox which is called whenever the listbox is fully populated, iterate through the items and measure the longest string width; finally add the Chart.Legend.SymbolWidth for the series icons, and the text height for the checkbox width, plus several pixels for margins and spacings.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ChartListbox1Populate(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   w: Integer;
  5.   h: Integer;
  6. begin
  7.   w := 0;
  8.   ChartListbox1.Canvas.Font.Assign(ChartListbox1.Font);
  9.   if ChartListbox1.Canvas.Font.Size = 0 then
  10.     ChartListbox1.Canvas.Font.Size := 9;
  11.   for i:=0 to ChartListbox1.items.count-1 do
  12.     w := Max(w, ChartListbox1.Canvas.TextWidth(ChartListbox1.Items[i]));
  13.   h := ChartListbox1.Canvas.TextHeight('Tg');
  14.   w := w + Chart1.Legend.SymbolWidth + h + 20;
  15.   ChartListbox1.Width := w;
  16. end;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

AL

  • Full Member
  • ***
  • Posts: 159
Re: TAChartListBox Autoextend possible?
« Reply #2 on: November 21, 2019, 12:35:57 am »
Thank you very much, work perfect.
I only had to slightly adjust  line 14 to:

14  w := w + Chartlistbox1.Chart.Legend.SymbolWidth + h + 20; 
Laz 2.1.0, fpc 3.3.1, Win10
Laz 2.1.0, fpc3.3.1, Mac OS Mojave running on VMWare

 

TinyPortal © 2005-2018