Lazarus

Programming => Graphics and Multimedia => TAChart => Topic started by: AL on November 20, 2019, 04:59:43 pm

Title: TAChartListBox Autoextend possible? [SOLVED]
Post by: AL 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
Title: Re: TAChartListBox Autoextend possible?
Post by: wp 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;
Title: Re: TAChartListBox Autoextend possible?
Post by: AL 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; 
TinyPortal © 2005-2018