rptnestedgroups.pp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. unit rptnestedgroups;
  2. {$mode objfpc}{$H+}
  3. {$I demos.inc}
  4. interface
  5. uses
  6. Classes,
  7. SysUtils,
  8. fpreport,
  9. udapp;
  10. type
  11. { TNestedGroupsDemo }
  12. TNestedGroupsDemo = class(TReportDemoApp)
  13. private
  14. FReportData: TFPReportUserData;
  15. sl: TStringList;
  16. rec: TStringList;
  17. procedure GetReportDataFirst(Sender: TObject);
  18. procedure GetReportDataValue(Sender: TObject; const AValueName: String; var AValue: Variant);
  19. procedure GetReportDataEOF(Sender: TObject; var IsEOF: Boolean);
  20. procedure GetReportFieldNames(Sender: TObject; List: TStrings);
  21. procedure ReportDataNext(Sender: TObject);
  22. procedure PrepareRecord;
  23. Protected
  24. procedure InitialiseData; override;
  25. procedure CreateReportDesign;override;
  26. procedure LoadDesignFromFile(const AFilename: string);
  27. procedure HookupData(const AComponentName: string; const AData: TFPReportData);
  28. public
  29. constructor Create(AOWner :TComponent); override;
  30. destructor Destroy; override;
  31. Class function Description : string; override;
  32. end;
  33. implementation
  34. uses
  35. fpReportStreamer,
  36. fpTTF,
  37. fpJSON,
  38. jsonparser,
  39. fpexprpars;
  40. const
  41. clGroupHeaderFooter2 = TFPReportColor($EFE1C7);
  42. clGroupHeaderFooter3 = TFPReportColor($DFD1B7);
  43. { TNestedGroupsDemo }
  44. procedure TNestedGroupsDemo.GetReportDataFirst(Sender: TObject);
  45. begin
  46. {$IFDEF gdebug}
  47. writeln('GetReportDataFirst');
  48. {$ENDIF}
  49. PrepareRecord;
  50. end;
  51. procedure TNestedGroupsDemo.GetReportDataValue(Sender: TObject; const AValueName: String; var AValue: Variant);
  52. begin
  53. {$IFDEF gdebug}
  54. writeln(Format('GetReportDataValue - %d', [lReportData.RecNo]));
  55. {$ENDIF}
  56. case AValueName of
  57. 'region': AValue := rec[0];
  58. 'subregion': AValue := rec[1];
  59. 'country': AValue := rec[2];
  60. 'code': AValue := rec[3];
  61. 'population': AValue := rec[4];
  62. end;
  63. end;
  64. procedure TNestedGroupsDemo.GetReportDataEOF(Sender: TObject; var IsEOF: Boolean);
  65. begin
  66. {$IFDEF gdebug}
  67. writeln(Format('GetReportDataEOF - %d', [lReportData.RecNo]));
  68. {$ENDIF}
  69. if FReportData.RecNo > sl.Count then
  70. IsEOF := True
  71. else
  72. IsEOF := False;
  73. end;
  74. procedure TNestedGroupsDemo.GetReportFieldNames(Sender: TObject; List: TStrings);
  75. begin
  76. {$IFDEF gdebug}
  77. writeln('********** GetReportFieldNames');
  78. {$ENDIF}
  79. List.Add('region');
  80. List.Add('subregion');
  81. List.Add('country');
  82. List.Add('code');
  83. List.Add('population');
  84. end;
  85. procedure TNestedGroupsDemo.ReportDataNext(Sender: TObject);
  86. begin
  87. PrepareRecord;
  88. end;
  89. procedure TNestedGroupsDemo.PrepareRecord;
  90. begin
  91. if FReportData.RecNo > sl.Count then
  92. exit;
  93. rec.DelimitedText := sl[FReportData.RecNo-1];
  94. end;
  95. procedure TNestedGroupsDemo.InitialiseData;
  96. begin
  97. sl := TStringList.Create;
  98. {$I countries2.inc}
  99. rec := TStringList.Create;
  100. rec.Delimiter := ';';
  101. rec.StrictDelimiter := true;
  102. end;
  103. procedure TNestedGroupsDemo.CreateReportDesign;
  104. var
  105. Page: TFPReportPage;
  106. TitleBand: TFPReportTitleBand;
  107. DataBand: TFPReportDataBand;
  108. GroupHeader, GroupHeader1Region,
  109. GroupHeader2Subregion, GroupHeader3Initial: TFPReportGroupHeaderBand;
  110. Memo: TFPReportMemo;
  111. PageFooter: TFPReportPageFooterBand;
  112. GroupFooter, GroupFooter3Initial,
  113. GroupFooter2SubRegion, GroupFooter1Region: TFPReportGroupFooterBand;
  114. ChildBand: TFPReportChildBand;
  115. Shape: TFPReportShape;
  116. begin
  117. Inherited;
  118. rpt.Author := 'Pascal Riekenberg';
  119. rpt.Title := 'FPReport Demo 13 - Nested Grouping';
  120. {****************}
  121. {*** page ***}
  122. {****************}
  123. Page := TFPReportPage.Create(rpt);
  124. Page.Orientation := poPortrait;
  125. Page.PageSize.PaperName := 'A4';
  126. { page margins }
  127. Page.Margins.Left := 25;
  128. Page.Margins.Top := 20;
  129. Page.Margins.Right := 10;
  130. Page.Margins.Bottom := 20;
  131. Page.Data := FReportData;
  132. Page.Font.Name := 'LiberationSans';
  133. {*****************}
  134. {*** title ***}
  135. {*****************}
  136. TitleBand := TFPReportTitleBand.Create(Page);
  137. TitleBand.Layout.Height := 40;
  138. TitleBand.Frame.Shape := fsRectangle;
  139. TitleBand.Frame.BackgroundColor := clReportTitleSummary;
  140. Memo := TFPReportMemo.Create(TitleBand);
  141. Memo.Layout.Left := 0;
  142. Memo.Layout.Top := 10;
  143. Memo.Layout.Width := Page.PageSize.Width - Page.Margins.Left - Page.Margins.Right;
  144. Memo.Layout.Height := 16;
  145. Memo.TextAlignment.Horizontal := taCentered;
  146. Memo.UseParentFont := False;
  147. Memo.Text := 'COUNTRY AND POPULATION AS OF 2016';
  148. Memo.Font.Size := 16;
  149. Memo := TFPReportMemo.Create(TitleBand);
  150. Memo.Layout.Left := 0;
  151. Memo.Layout.Top := 18;
  152. Memo.Layout.Width := Page.PageSize.Width - Page.Margins.Left - Page.Margins.Right;
  153. Memo.Layout.Height := 10;
  154. Memo.TextAlignment.Horizontal := taCentered;
  155. Memo.UseParentFont := False;
  156. Memo.Text := '(Total [formatfloat(''#,##0.0'',total_sum_population_in_M / 1000)] B)';
  157. Memo.Font.Size := 10;
  158. {**********************}
  159. {*** group header ***}
  160. {**********************}
  161. {*** group header 1 region ***}
  162. GroupHeader1Region := TFPReportGroupHeaderBand.Create(Page);
  163. GroupHeader1Region.Layout.Height := 15;
  164. GroupHeader1Region.GroupCondition := 'region';
  165. GroupHeader1Region.Frame.Shape := fsRectangle;
  166. GroupHeader1Region.Frame.BackgroundColor := clGroupHeaderFooter;
  167. GroupHeader1Region.StartOnNewPage := True;
  168. GroupHeader1Region.ReprintHeader := True;
  169. GroupHeader1Region.VisibleExpr := 'not InRepeatedGroupHeader';
  170. Memo := TFPReportMemo.Create(GroupHeader1Region);
  171. Memo.Layout.Left := 3;
  172. Memo.Layout.Top := 1;
  173. Memo.Layout.Width := 170;
  174. Memo.Layout.Height := 6;
  175. Memo.UseParentFont := False;
  176. Memo.Font.Size := 16;
  177. Memo.TextAlignment.Vertical := tlBottom;
  178. Memo.Text := 'Region: [region] ([formatfloat(''#,##0.0'', grp1region_sum_population_in_M)] M)';
  179. Memo := TFPReportMemo.Create(GroupHeader1Region);
  180. Memo.Layout.Left := 25;
  181. Memo.Layout.Top := 1;
  182. Memo.Layout.Width := 145;
  183. Memo.Layout.Height := 6;
  184. Memo.UseParentFont := False;
  185. Memo.Font.Size := 10;
  186. Memo.TextAlignment.Vertical := tlBottom;
  187. Memo.TextAlignment.Horizontal := taRightJustified;
  188. Memo.Text := '[formatfloat(''#0.0'', grp1region_sum_population / total_sum_population * 100)] % in World';
  189. ChildBand := TFPReportChildBand.Create(Page);
  190. ChildBand.Layout.Height := 7;
  191. ChildBand.VisibleExpr := 'InRepeatedGroupHeader';
  192. ChildBand.Frame.Shape := fsRectangle;
  193. ChildBand.Frame.BackgroundColor := clGroupHeaderFooter;
  194. GroupHeader1Region.ChildBand := ChildBand;
  195. Memo := TFPReportMemo.Create(ChildBand);
  196. Memo.Layout.Left := 3;
  197. Memo.Layout.Top := 1;
  198. Memo.Layout.Width := 170;
  199. Memo.Layout.Height := 4;
  200. Memo.Text := 'Region: [region]';
  201. ChildBand := TFPReportChildBand.Create(Page);
  202. ChildBand.Layout.Height := 2;
  203. GroupHeader1Region.ChildBand.ChildBand := ChildBand;
  204. Shape := TFPReportShape.Create(ChildBand);
  205. Shape.Color := clGroupHeaderFooter;
  206. Shape.Layout.Left := 0;
  207. Shape.Layout.Top := 0;
  208. Shape.Layout.Width := 3;
  209. Shape.Layout.Height := ChildBand.Layout.Height;
  210. Shape.Frame.Shape := fsRectangle;
  211. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  212. {*** group header 2 subregion ***}
  213. GroupHeader2Subregion := TFPReportGroupHeaderBand.Create(Page);
  214. GroupHeader2Subregion.Layout.Height := 15;
  215. GroupHeader2Subregion.GroupCondition := 'subregion';
  216. GroupHeader2Subregion.Frame.Shape := fsRectangle;
  217. GroupHeader2Subregion.Frame.BackgroundColor := clGroupHeaderFooter2;
  218. GroupHeader2Subregion.GroupHeader := GroupHeader1Region;
  219. GroupHeader2Subregion.StartOnNewPage := True;
  220. GroupHeader2Subregion.ReprintHeader := True;
  221. GroupHeader2Subregion.VisibleExpr := 'not InRepeatedGroupHeader';
  222. Shape := TFPReportShape.Create(GroupHeader2Subregion);
  223. Shape.Color := clGroupHeaderFooter;
  224. Shape.Layout.Left := 0;
  225. Shape.Layout.Top := 0;
  226. Shape.Layout.Width := 3;
  227. Shape.Layout.Height := GroupHeader2Subregion.Layout.Height;
  228. Shape.Frame.Shape := fsRectangle;
  229. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  230. Shape := TFPReportShape.Create(GroupHeader2Subregion);
  231. Shape.Color := clNone;
  232. Shape.Layout.Left := 3;
  233. Shape.Layout.Top := 0;
  234. Shape.Layout.Width := 2;
  235. Shape.Layout.Height := GroupHeader2Subregion.Layout.Height;
  236. Shape.Frame.Shape := fsRectangle;
  237. Shape.Frame.BackgroundColor := clWhite;
  238. Memo := TFPReportMemo.Create(GroupHeader2Subregion);
  239. Memo.Layout.Left := 7;
  240. Memo.Layout.Top := 1;
  241. Memo.Layout.Width := 170;
  242. Memo.Layout.Height := 6;
  243. Memo.UseParentFont := False;
  244. Memo.Font.Size := 16;
  245. Memo.TextAlignment.Vertical := tlBottom;
  246. Memo.Text := 'Subregion: [subregion] ([formatfloat(''#,##0.0'', grp2subregion_sum_population_in_M)] M)';
  247. Memo := TFPReportMemo.Create(GroupHeader2Subregion);
  248. Memo.Layout.Left := 25;
  249. Memo.Layout.Top := 1;
  250. Memo.Layout.Width := 145;
  251. Memo.Layout.Height := 6;
  252. Memo.UseParentFont := False;
  253. Memo.Font.Size := 10;
  254. Memo.TextAlignment.Vertical := tlBottom;
  255. Memo.TextAlignment.Horizontal := taRightJustified;
  256. Memo.Text := '[formatfloat(''#0.0'', grp2subregion_sum_population / grp1region_sum_population * 100)] % in [region] - [formatfloat(''#0.0'', grp2subregion_sum_population / total_sum_population * 100)] % in World';
  257. ChildBand := TFPReportChildBand.Create(Page);
  258. ChildBand.Layout.Height := 7;
  259. ChildBand.VisibleExpr := 'InRepeatedGroupHeader';
  260. ChildBand.Frame.Shape := fsRectangle;
  261. ChildBand.Frame.BackgroundColor := clGroupHeaderFooter2;
  262. GroupHeader2Subregion.ChildBand := ChildBand;
  263. Shape := TFPReportShape.Create(ChildBand);
  264. Shape.Color := clGroupHeaderFooter;
  265. Shape.Layout.Left := 0;
  266. Shape.Layout.Top := 0;
  267. Shape.Layout.Width := 3;
  268. Shape.Layout.Height := ChildBand.Layout.Height;
  269. Shape.Frame.Shape := fsRectangle;
  270. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  271. Shape := TFPReportShape.Create(ChildBand);
  272. Shape.Color := clNone;
  273. Shape.Layout.Left := 3;
  274. Shape.Layout.Top := 0;
  275. Shape.Layout.Width := 2;
  276. Shape.Layout.Height := ChildBand.Layout.Height;
  277. Shape.Frame.Shape := fsRectangle;
  278. Shape.Frame.BackgroundColor := clWhite;
  279. Memo := TFPReportMemo.Create(ChildBand);
  280. Memo.Layout.Left := 7;
  281. Memo.Layout.Top := 1;
  282. Memo.Layout.Width := 170;
  283. Memo.Layout.Height := 4;
  284. Memo.Text := 'Subregion: [subregion]';
  285. ChildBand := TFPReportChildBand.Create(Page);
  286. ChildBand.Layout.Height := 2;
  287. GroupHeader2Subregion.ChildBand.ChildBand := ChildBand;
  288. Shape := TFPReportShape.Create(ChildBand);
  289. Shape.Color := clGroupHeaderFooter;
  290. Shape.Layout.Left := 0;
  291. Shape.Layout.Top := 0;
  292. Shape.Layout.Width := 3;
  293. Shape.Layout.Height := ChildBand.Layout.Height;
  294. Shape.Frame.Shape := fsRectangle;
  295. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  296. Shape := TFPReportShape.Create(ChildBand);
  297. Shape.Color := clNone;
  298. Shape.Layout.Left := 5;
  299. Shape.Layout.Top := 0;
  300. Shape.Layout.Width := 3;
  301. Shape.Layout.Height := ChildBand.Layout.Height;
  302. Shape.Frame.Shape := fsRectangle;
  303. Shape.Frame.BackgroundColor := clGroupHeaderFooter2;
  304. {*** group header 3 initial ***}
  305. GroupHeader3Initial := TFPReportGroupHeaderBand.Create(Page);
  306. GroupHeader3Initial.Layout.Height := 15;
  307. GroupHeader3Initial.GroupCondition := 'copy(country,1,1)';
  308. GroupHeader3Initial.Frame.Shape := fsRectangle;
  309. GroupHeader3Initial.Frame.BackgroundColor := clGroupHeaderFooter3;
  310. GroupHeader3Initial.GroupHeader := GroupHeader2Subregion;
  311. GroupHeader3Initial.ReprintHeader := True;
  312. GroupHeader3Initial.VisibleExpr := 'not InRepeatedGroupHeader';
  313. Shape := TFPReportShape.Create(GroupHeader3Initial);
  314. Shape.Color := clGroupHeaderFooter;
  315. Shape.Layout.Left := 0;
  316. Shape.Layout.Top := 0;
  317. Shape.Layout.Width := 3;
  318. Shape.Layout.Height := GroupHeader3Initial.Layout.Height;
  319. Shape.Frame.Shape := fsRectangle;
  320. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  321. Shape := TFPReportShape.Create(GroupHeader3Initial);
  322. Shape.Color := clNone;
  323. Shape.Layout.Left := 3;
  324. Shape.Layout.Top := 0;
  325. Shape.Layout.Width := 2;
  326. Shape.Layout.Height := GroupHeader3Initial.Layout.Height;
  327. Shape.Frame.Shape := fsRectangle;
  328. Shape.Frame.BackgroundColor := clWhite;
  329. Shape := TFPReportShape.Create(GroupHeader3Initial);
  330. Shape.Color := clGroupHeaderFooter2;
  331. Shape.Layout.Left := 5;
  332. Shape.Layout.Top := 0;
  333. Shape.Layout.Width := 3;
  334. Shape.Layout.Height := GroupHeader3Initial.Layout.Height;
  335. Shape.Frame.Shape := fsRectangle;
  336. Shape.Frame.BackgroundColor := clGroupHeaderFooter2;
  337. Shape := TFPReportShape.Create(GroupHeader3Initial);
  338. Shape.Color := clNone;
  339. Shape.Layout.Left := 8;
  340. Shape.Layout.Top := 0;
  341. Shape.Layout.Width := 2;
  342. Shape.Layout.Height := GroupHeader3Initial.Layout.Height;
  343. Shape.Frame.Shape := fsRectangle;
  344. Shape.Frame.BackgroundColor := clWhite;
  345. Memo := TFPReportMemo.Create(GroupHeader3Initial);
  346. Memo.Layout.Left := 12;
  347. Memo.Layout.Top := 1;
  348. Memo.Layout.Width := 170;
  349. Memo.Layout.Height := 6;
  350. Memo.UseParentFont := False;
  351. Memo.Font.Size := 16;
  352. Memo.TextAlignment.Vertical := tlBottom;
  353. Memo.Text := '[copy(country,1,1)] ([formatfloat(''#,##0.0'', grp3initial_sum_population_in_M)] M)';
  354. Memo := TFPReportMemo.Create(GroupHeader3Initial);
  355. Memo.Layout.Left := 25;
  356. Memo.Layout.Top := 1;
  357. Memo.Layout.Width := 145;
  358. Memo.Layout.Height := 6;
  359. Memo.UseParentFont := False;
  360. Memo.Font.Size := 10;
  361. Memo.TextAlignment.Vertical := tlBottom;
  362. Memo.TextAlignment.Horizontal := taRightJustified;
  363. Memo.Text := '[formatfloat(''#0.0'', grp3initial_sum_population / grp2subregion_sum_population * 100)] % in [subregion] - [formatfloat(''#0.0'', grp3initial_sum_population / grp1region_sum_population * 100)] % in [region] - [formatfloat(''#0.0'', grp3initial_sum_population / total_sum_population * 100)] % in World';
  364. Memo := TFPReportMemo.Create(GroupHeader3Initial);
  365. Memo.Layout.Left := 90;
  366. Memo.Layout.Top := 10.5;
  367. Memo.Layout.Width := 20;
  368. Memo.Layout.Height := 4;
  369. Memo.TextAlignment.Horizontal := taRightJustified;
  370. Memo.Text := 'Initial %';
  371. Memo := TFPReportMemo.Create(GroupHeader3Initial);
  372. Memo.Layout.Left := 110;
  373. Memo.Layout.Top := 10.5;
  374. Memo.Layout.Width := 20;
  375. Memo.Layout.Height := 4;
  376. Memo.TextAlignment.Horizontal := taRightJustified;
  377. Memo.Text := 'Subreg. %';
  378. Memo := TFPReportMemo.Create(GroupHeader3Initial);
  379. Memo.Layout.Left := 130;
  380. Memo.Layout.Top := 10.5;
  381. Memo.Layout.Width := 20;
  382. Memo.Layout.Height := 4;
  383. Memo.TextAlignment.Horizontal := taRightJustified;
  384. Memo.Text := 'Region %';
  385. Memo := TFPReportMemo.Create(GroupHeader3Initial);
  386. Memo.Layout.Left := 150;
  387. Memo.Layout.Top := 10.5;
  388. Memo.Layout.Width := 20;
  389. Memo.Layout.Height := 4;
  390. Memo.TextAlignment.Horizontal := taRightJustified;
  391. Memo.Text := 'World %';
  392. ChildBand := TFPReportChildBand.Create(Page);
  393. ChildBand.Layout.Height := 7;
  394. ChildBand.VisibleExpr := 'InRepeatedGroupHeader';
  395. ChildBand.Frame.Shape := fsRectangle;
  396. ChildBand.Frame.BackgroundColor := clGroupHeaderFooter3;
  397. GroupHeader3Initial.ChildBand := ChildBand;
  398. Shape := TFPReportShape.Create(ChildBand);
  399. Shape.Color := clGroupHeaderFooter;
  400. Shape.Layout.Left := 0;
  401. Shape.Layout.Top := 0;
  402. Shape.Layout.Width := 3;
  403. Shape.Layout.Height := ChildBand.Layout.Height;
  404. Shape.Frame.Shape := fsRectangle;
  405. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  406. Shape := TFPReportShape.Create(ChildBand);
  407. Shape.Color := clNone;
  408. Shape.Layout.Left := 3;
  409. Shape.Layout.Top := 0;
  410. Shape.Layout.Width := 2;
  411. Shape.Layout.Height := ChildBand.Layout.Height;
  412. Shape.Frame.Shape := fsRectangle;
  413. Shape.Frame.BackgroundColor := clWhite;
  414. Shape := TFPReportShape.Create(ChildBand);
  415. Shape.Color := clGroupHeaderFooter2;
  416. Shape.Layout.Left := 5;
  417. Shape.Layout.Top := 0;
  418. Shape.Layout.Width := 3;
  419. Shape.Layout.Height := ChildBand.Layout.Height;
  420. Shape.Frame.Shape := fsRectangle;
  421. Shape.Frame.BackgroundColor := clGroupHeaderFooter2;
  422. Shape := TFPReportShape.Create(ChildBand);
  423. Shape.Color := clNone;
  424. Shape.Layout.Left := 8;
  425. Shape.Layout.Top := 0;
  426. Shape.Layout.Width := 2;
  427. Shape.Layout.Height := ChildBand.Layout.Height;
  428. Shape.Frame.Shape := fsRectangle;
  429. Shape.Frame.BackgroundColor := clWhite;
  430. Memo := TFPReportMemo.Create(ChildBand);
  431. Memo.Layout.Left := 12;
  432. Memo.Layout.Top := 1;
  433. Memo.Layout.Width := 170;
  434. Memo.Layout.Height := 4;
  435. Memo.Text := '[copy(country,1,1)]';
  436. ChildBand := TFPReportChildBand.Create(Page);
  437. ChildBand.Layout.Height := 2;
  438. GroupHeader3Initial.ChildBand.ChildBand := ChildBand;
  439. Shape := TFPReportShape.Create(ChildBand);
  440. Shape.Color := clGroupHeaderFooter;
  441. Shape.Layout.Left := 0;
  442. Shape.Layout.Top := 0;
  443. Shape.Layout.Width := 3;
  444. Shape.Layout.Height := ChildBand.Layout.Height;
  445. Shape.Frame.Shape := fsRectangle;
  446. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  447. Shape := TFPReportShape.Create(ChildBand);
  448. Shape.Color := clGroupHeaderFooter2;
  449. Shape.Layout.Left := 5;
  450. Shape.Layout.Top := 0;
  451. Shape.Layout.Width := 3;
  452. Shape.Layout.Height := ChildBand.Layout.Height;
  453. Shape.Frame.Shape := fsRectangle;
  454. Shape.Frame.BackgroundColor := clGroupHeaderFooter2;
  455. Shape := TFPReportShape.Create(ChildBand);
  456. Shape.Color := clGroupHeaderFooter3;
  457. Shape.Layout.Left := 10;
  458. Shape.Layout.Top := 0;
  459. Shape.Layout.Width := 3;
  460. Shape.Layout.Height := ChildBand.Layout.Height;
  461. Shape.Frame.Shape := fsRectangle;
  462. Shape.Frame.BackgroundColor := clGroupHeaderFooter3;
  463. {*** variables ***}
  464. rpt.Variables.AddExprVariable('population_in_M', 'StrToFloat(population) / 1000000', rtFloat);
  465. rpt.Variables.AddExprVariable('grp1region_sum_population_in_M', 'sum(StrToFloat(population) / 1000000)', rtFloat, rtGroup, GroupHeader1Region);
  466. rpt.Variables.AddExprVariable('grp1region_sum_population', 'sum(StrToFloat(population))', rtFloat, rtGroup, GroupHeader1Region);
  467. rpt.Variables.AddExprVariable('grp2subregion_sum_population_in_M', 'sum(StrToFloat(population) / 1000000)', rtFloat, rtGroup, GroupHeader2Subregion);
  468. rpt.Variables.AddExprVariable('grp2subregion_sum_population', 'sum(StrToFloat(population))', rtFloat, rtGroup, GroupHeader2Subregion);
  469. rpt.Variables.AddExprVariable('grp3initial_sum_population_in_M', 'sum(StrToFloat(population) / 1000000)', rtFloat, rtGroup, GroupHeader3Initial);
  470. rpt.Variables.AddExprVariable('grp3initial_sum_population', 'sum(StrToFloat(population))', rtFloat, rtGroup, GroupHeader3Initial);
  471. rpt.Variables.AddExprVariable('total_sum_population_in_M', 'sum(StrToFloat(population) / 1000000)', rtFloat);
  472. rpt.Variables.AddExprVariable('total_sum_population', 'sum(StrToFloat(population))', rtFloat);
  473. {****************}
  474. {*** detail ***}
  475. {****************}
  476. DataBand := TFPReportDataBand.Create(Page);
  477. DataBand.Layout.Height := 8;
  478. DataBand.Frame.Shape := fsRectangle;
  479. DataBand.Frame.BackgroundColor := clDataBand;
  480. //DataBand.VisibleExpr := 'StrToFloat(''[population]'') > 50000000';
  481. Shape := TFPReportShape.Create(DataBand);
  482. Shape.Color := clGroupHeaderFooter;
  483. Shape.Layout.Left := 0;
  484. Shape.Layout.Top := 0;
  485. Shape.Layout.Width := 3;
  486. Shape.Layout.Height := DataBand.Layout.Height;
  487. Shape.Frame.Shape := fsRectangle;
  488. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  489. Shape := TFPReportShape.Create(DataBand);
  490. Shape.Color := clNone;
  491. Shape.Layout.Left := 3;
  492. Shape.Layout.Top := 0;
  493. Shape.Layout.Width := 2;
  494. Shape.Layout.Height := DataBand.Layout.Height;
  495. Shape.Frame.Shape := fsRectangle;
  496. Shape.Frame.BackgroundColor := clWhite;
  497. Shape := TFPReportShape.Create(DataBand);
  498. Shape.Color := clGroupHeaderFooter2;
  499. Shape.Layout.Left := 5;
  500. Shape.Layout.Top := 0;
  501. Shape.Layout.Width := 3;
  502. Shape.Layout.Height := DataBand.Layout.Height;
  503. Shape.Frame.Shape := fsRectangle;
  504. Shape.Frame.BackgroundColor := clGroupHeaderFooter2;
  505. Shape := TFPReportShape.Create(DataBand);
  506. Shape.Color := clNone;
  507. Shape.Layout.Left := 8;
  508. Shape.Layout.Top := 0;
  509. Shape.Layout.Width := 2;
  510. Shape.Layout.Height := DataBand.Layout.Height;
  511. Shape.Frame.Shape := fsRectangle;
  512. Shape.Frame.BackgroundColor := clWhite;
  513. Shape := TFPReportShape.Create(DataBand);
  514. Shape.Color := clGroupHeaderFooter3;
  515. Shape.Layout.Left := 10;
  516. Shape.Layout.Top := 0;
  517. Shape.Layout.Width := 3;
  518. Shape.Layout.Height := DataBand.Layout.Height;
  519. Shape.Frame.Shape := fsRectangle;
  520. Shape.Frame.BackgroundColor := clGroupHeaderFooter3;
  521. Shape := TFPReportShape.Create(DataBand);
  522. Shape.Color := clNone;
  523. Shape.Layout.Left := 13;
  524. Shape.Layout.Top := 0;
  525. Shape.Layout.Width := 2;
  526. Shape.Layout.Height := DataBand.Layout.Height;
  527. Shape.Frame.Shape := fsRectangle;
  528. Shape.Frame.BackgroundColor := clWhite;
  529. Memo := TFPReportMemo.Create(DataBand);
  530. Memo.Layout.Left := 17;
  531. Memo.Layout.Top := 2;
  532. Memo.Layout.Width := 45;
  533. Memo.Layout.Height := 5;
  534. Memo.Text := '[country]';
  535. Memo.Options := memo.Options + [moDisableWordWrap];
  536. Memo := TFPReportMemo.Create(DataBand);
  537. Memo.Layout.Left := 55;
  538. Memo.Layout.Top := 2;
  539. Memo.Layout.Width := 25;
  540. Memo.Layout.Height := 5;
  541. Memo.TextAlignment.Horizontal := taRightJustified;
  542. Memo.Text := '[formatfloat(''#,##0'', StrToFloat(population))]';
  543. Memo := TFPReportMemo.Create(DataBand);
  544. Memo.Layout.Left := 80;
  545. Memo.Layout.Top := 2;
  546. Memo.Layout.Width := 20;
  547. Memo.Layout.Height := 5;
  548. Memo.Text := '> DEU';
  549. Memo.UseParentFont := false;
  550. Memo.Font.Color := clGreen;
  551. Memo.VisibleExpr := 'StrToFloat(population) > 82667685';
  552. Memo := TFPReportMemo.Create(DataBand);
  553. Memo.Layout.Left := 80;
  554. Memo.Layout.Top := 2;
  555. Memo.Layout.Width := 20;
  556. Memo.Layout.Height := 5;
  557. Memo.Text := '< DEU';
  558. Memo.UseParentFont := false;
  559. Memo.Font.Color := clRed;
  560. Memo.VisibleExpr := 'StrToFloat(population) < 82667685';
  561. Memo := TFPReportMemo.Create(DataBand);
  562. Memo.Layout.Left := 95;
  563. Memo.Layout.Top := 2;
  564. Memo.Layout.Width := 15;
  565. Memo.Layout.Height := 5;
  566. Memo.TextAlignment.Horizontal := taRightJustified;
  567. Memo.Text := '[formatfloat(''#,##0.0'',StrToFloat(population)/grp3initial_sum_population*100)] %';
  568. Memo := TFPReportMemo.Create(DataBand);
  569. Memo.Layout.Left := 115;
  570. Memo.Layout.Top := 2;
  571. Memo.Layout.Width := 15;
  572. Memo.Layout.Height := 5;
  573. Memo.TextAlignment.Horizontal := taRightJustified;
  574. Memo.Text := '[formatfloat(''#,##0.0'',StrToFloat(population)/grp2subregion_sum_population*100)] %';
  575. Memo := TFPReportMemo.Create(DataBand);
  576. Memo.Layout.Left := 135;
  577. Memo.Layout.Top := 2;
  578. Memo.Layout.Width := 15;
  579. Memo.Layout.Height := 5;
  580. Memo.TextAlignment.Horizontal := taRightJustified;
  581. Memo.Text := '[formatfloat(''#,##0.0'',StrToFloat(population)/grp1region_sum_population*100)] %';
  582. Memo := TFPReportMemo.Create(DataBand);
  583. Memo.Layout.Left := 155;
  584. Memo.Layout.Top := 2;
  585. Memo.Layout.Width := 15;
  586. Memo.Layout.Height := 5;
  587. Memo.TextAlignment.Horizontal := taRightJustified;
  588. Memo.Text := '[formatfloat(''#,##0.0'',StrToFloat(population)/total_sum_population*100)] %';
  589. {**********************}
  590. {*** group footer ***}
  591. {**********************}
  592. {*** group footer 3 initial ***}
  593. GroupFooter3Initial := TFPReportGroupFooterBand.Create(Page);
  594. GroupFooter3Initial.Layout.Height := 2;
  595. GroupFooter3Initial.GroupHeader := GroupHeader3Initial;
  596. Shape := TFPReportShape.Create(GroupFooter3Initial);
  597. Shape.Color := clGroupHeaderFooter;
  598. Shape.Layout.Left := 0;
  599. Shape.Layout.Top := 0;
  600. Shape.Layout.Width := 3;
  601. Shape.Layout.Height := GroupFooter3Initial.Layout.Height;
  602. Shape.Frame.Shape := fsRectangle;
  603. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  604. Shape := TFPReportShape.Create(GroupFooter3Initial);
  605. Shape.Color := clGroupHeaderFooter2;
  606. Shape.Layout.Left := 5;
  607. Shape.Layout.Top := 0;
  608. Shape.Layout.Width := 3;
  609. Shape.Layout.Height := GroupFooter3Initial.Layout.Height;
  610. Shape.Frame.Shape := fsRectangle;
  611. Shape.Frame.BackgroundColor := clGroupHeaderFooter2;
  612. Shape := TFPReportShape.Create(GroupFooter3Initial);
  613. Shape.Color := clGroupHeaderFooter3;
  614. Shape.Layout.Left := 10;
  615. Shape.Layout.Top := 0;
  616. Shape.Layout.Width := 3;
  617. Shape.Layout.Height := GroupFooter3Initial.Layout.Height;
  618. Shape.Frame.Shape := fsRectangle;
  619. Shape.Frame.BackgroundColor := clGroupHeaderFooter3;
  620. ChildBand := TFPReportChildBand.Create(Page);
  621. ChildBand.Layout.Height := 15;
  622. ChildBand.Frame.Shape := fsRectangle;
  623. ChildBand.Frame.BackgroundColor := clGroupHeaderFooter3;
  624. GroupFooter3Initial.ChildBand := ChildBand;
  625. Shape := TFPReportShape.Create(ChildBand);
  626. Shape.Color := clGroupHeaderFooter;
  627. Shape.Layout.Left := 0;
  628. Shape.Layout.Top := 0;
  629. Shape.Layout.Width := 3;
  630. Shape.Layout.Height := ChildBand.Layout.Height;
  631. Shape.Frame.Shape := fsRectangle;
  632. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  633. Shape := TFPReportShape.Create(ChildBand);
  634. Shape.Color := clNone;
  635. Shape.Layout.Left := 3;
  636. Shape.Layout.Top := 0;
  637. Shape.Layout.Width := 2;
  638. Shape.Layout.Height := ChildBand.Layout.Height;
  639. Shape.Frame.Shape := fsRectangle;
  640. Shape.Frame.BackgroundColor := clWhite;
  641. Shape := TFPReportShape.Create(ChildBand);
  642. Shape.Color := clGroupHeaderFooter2;
  643. Shape.Layout.Left := 5;
  644. Shape.Layout.Top := 0;
  645. Shape.Layout.Width := 3;
  646. Shape.Layout.Height := ChildBand.Layout.Height;
  647. Shape.Frame.Shape := fsRectangle;
  648. Shape.Frame.BackgroundColor := clGroupHeaderFooter2;
  649. Shape := TFPReportShape.Create(ChildBand);
  650. Shape.Color := clNone;
  651. Shape.Layout.Left := 8;
  652. Shape.Layout.Top := 0;
  653. Shape.Layout.Width := 2;
  654. Shape.Layout.Height := ChildBand.Layout.Height;
  655. Shape.Frame.Shape := fsRectangle;
  656. Shape.Frame.BackgroundColor := clWhite;
  657. Memo := TFPReportMemo.Create(ChildBand);
  658. Memo.Layout.Left := 12;
  659. Memo.Layout.Top := 3;
  660. Memo.Layout.Width := 170;
  661. Memo.Layout.Height := 6;
  662. Memo.UseParentFont := False;
  663. Memo.Font.Size := 16;
  664. Memo.TextAlignment.Vertical := tlBottom;
  665. Memo.Text := 'Population [copy(country,1,1)]: [formatfloat(''#,##0'', grp3initial_sum_population)]';
  666. //Memo.Text := 'Population: [formatfloat(''#,##0'', grp3initial_sum_population)]';
  667. ChildBand := TFPReportChildBand.Create(Page);
  668. ChildBand.Layout.Height := 2;
  669. GroupFooter3Initial.ChildBand.ChildBand := ChildBand;
  670. Shape := TFPReportShape.Create(ChildBand);
  671. Shape.Color := clGroupHeaderFooter;
  672. Shape.Layout.Left := 0;
  673. Shape.Layout.Top := 0;
  674. Shape.Layout.Width := 3;
  675. Shape.Layout.Height := ChildBand.Layout.Height;
  676. Shape.Frame.Shape := fsRectangle;
  677. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  678. Shape := TFPReportShape.Create(ChildBand);
  679. Shape.Color := clGroupHeaderFooter2;
  680. Shape.Layout.Left := 5;
  681. Shape.Layout.Top := 0;
  682. Shape.Layout.Width := 3;
  683. Shape.Layout.Height := ChildBand.Layout.Height;
  684. Shape.Frame.Shape := fsRectangle;
  685. Shape.Frame.BackgroundColor := clGroupHeaderFooter2;
  686. {*** group footer 2 subregion ***}
  687. GroupFooter2SubRegion := TFPReportGroupFooterBand.Create(Page);
  688. GroupFooter2SubRegion.Layout.Height := 15;
  689. GroupFooter2SubRegion.GroupHeader := GroupHeader2Subregion;
  690. GroupFooter2SubRegion.Frame.Shape := fsRectangle;
  691. GroupFooter2SubRegion.Frame.BackgroundColor := clGroupHeaderFooter2;
  692. Shape := TFPReportShape.Create(GroupFooter2SubRegion);
  693. Shape.Color := clGroupHeaderFooter;
  694. Shape.Layout.Left := 0;
  695. Shape.Layout.Top := 0;
  696. Shape.Layout.Width := 3;
  697. Shape.Layout.Height := GroupFooter2SubRegion.Layout.Height;
  698. Shape.Frame.Shape := fsRectangle;
  699. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  700. Shape := TFPReportShape.Create(GroupFooter2SubRegion);
  701. Shape.Color := clNone;
  702. Shape.Layout.Left := 3;
  703. Shape.Layout.Top := 0;
  704. Shape.Layout.Width := 2;
  705. Shape.Layout.Height := GroupFooter2SubRegion.Layout.Height;
  706. Shape.Frame.Shape := fsRectangle;
  707. Shape.Frame.BackgroundColor := clWhite;
  708. Memo := TFPReportMemo.Create(GroupFooter2SubRegion);
  709. Memo.Layout.Left := 7;
  710. Memo.Layout.Top := 3;
  711. Memo.Layout.Width := 170;
  712. Memo.Layout.Height := 6;
  713. Memo.UseParentFont := False;
  714. Memo.Font.Size := 16;
  715. Memo.TextAlignment.Vertical := tlBottom;
  716. Memo.Text := 'Population [subregion]: [formatfloat(''#,##0'', grp2subregion_sum_population)]';
  717. //Memo.Text := 'Population: [formatfloat(''#,##0'', grp2subregion_sum_population)]';
  718. ChildBand := TFPReportChildBand.Create(Page);
  719. ChildBand.Layout.Height := 2;
  720. GroupFooter2SubRegion.ChildBand := ChildBand;
  721. Shape := TFPReportShape.Create(ChildBand);
  722. Shape.Color := clGroupHeaderFooter;
  723. Shape.Layout.Left := 0;
  724. Shape.Layout.Top := 0;
  725. Shape.Layout.Width := 3;
  726. Shape.Layout.Height := ChildBand.Layout.Height;
  727. Shape.Frame.Shape := fsRectangle;
  728. Shape.Frame.BackgroundColor := clGroupHeaderFooter;
  729. {*** group footer 1 region ***}
  730. GroupFooter1Region := TFPReportGroupFooterBand.Create(Page);
  731. GroupFooter1Region.Layout.Height := 15;
  732. GroupFooter1Region.GroupHeader := GroupHeader1Region;
  733. GroupFooter1Region.Frame.Shape := fsRectangle;
  734. GroupFooter1Region.Frame.BackgroundColor := clGroupHeaderFooter;
  735. Memo := TFPReportMemo.Create(GroupFooter1Region);
  736. Memo.Layout.Left := 3;
  737. Memo.Layout.Top := 3;
  738. Memo.Layout.Width := 170;
  739. Memo.Layout.Height := 6;
  740. Memo.UseParentFont := False;
  741. Memo.Font.Size := 16;
  742. Memo.TextAlignment.Vertical := tlBottom;
  743. Memo.Text := 'Population [region]: [formatfloat(''#,##0'', grp1region_sum_population)]';
  744. //Memo.Text := 'Population: [formatfloat(''#,##0'', grp1region_sum_population)]';
  745. ChildBand := TFPReportChildBand.Create(Page);
  746. ChildBand.Layout.Height := 2;
  747. GroupFooter1Region.ChildBand := ChildBand;
  748. {*******************}
  749. {*** page footer ***}
  750. {*******************}
  751. PageFooter := TFPReportPageFooterBand.Create(Page);
  752. PageFooter.Layout.Height := 20;
  753. PageFooter.Frame.Shape := fsRectangle;
  754. PageFooter.Frame.BackgroundColor := clPageHeaderFooter;
  755. Memo := TFPReportMemo.Create(PageFooter);
  756. Memo.Layout.Left := 123;
  757. Memo.Layout.Top := 13;
  758. Memo.Layout.Width := 50;
  759. Memo.Layout.Height := 5;
  760. Memo.Text := 'Page [PageNo] of [PageCount]';
  761. Memo.TextAlignment.Vertical := tlCenter;
  762. Memo.TextAlignment.Horizontal := taRightJustified;
  763. Memo := TFPReportMemo.Create(PageFooter);
  764. Memo.Layout.Left := 0;
  765. Memo.Layout.Top := 5;
  766. Memo.Layout.Width := Page.PageSize.Width - Page.Margins.Left - Page.Margins.Right;
  767. Memo.Layout.Height := 8;
  768. Memo.UseParentFont := False;
  769. Memo.TextAlignment.Horizontal := taCentered;
  770. Memo.Text := 'Population World: [formatfloat(''#,##0'', total_sum_population)]';
  771. Memo.Font.Size := 16;
  772. end;
  773. procedure TNestedGroupsDemo.LoadDesignFromFile(const AFilename: string);
  774. var
  775. rs: TFPReportJSONStreamer;
  776. fs: TFileStream;
  777. lJSON: TJSONObject;
  778. begin
  779. if AFilename = '' then
  780. Exit;
  781. if not FileExists(AFilename) then
  782. raise Exception.CreateFmt('The file "%s" can not be found', [AFilename]);
  783. fs := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyNone);
  784. try
  785. lJSON := TJSONObject(GetJSON(fs));
  786. finally
  787. fs.Free;
  788. end;
  789. rs := TFPReportJSONStreamer.Create(nil);
  790. rs.JSON := lJSON; // rs takes ownership of lJSON
  791. try
  792. rpt.ReadElement(rs);
  793. finally
  794. rs.Free;
  795. end;
  796. end;
  797. procedure TNestedGroupsDemo.HookupData(const AComponentName: string; const AData: TFPReportData);
  798. var
  799. b: TFPReportCustomBandWithData;
  800. begin
  801. b := TFPReportCustomBandWithData(rpt.FindRecursive(AComponentName));
  802. if Assigned(b) then
  803. b.Data := AData;
  804. end;
  805. constructor TNestedGroupsDemo.Create(AOWner: TComponent);
  806. begin
  807. inherited;
  808. FReportData := TFPReportUserData.Create(nil);
  809. FReportData.OnGetValue := @GetReportDataValue;
  810. FReportData.OnGetEOF := @GetReportDataEOF;
  811. FReportData.OnFirst := @GetReportDataFirst;
  812. FReportData.OnGetNames := @GetReportFieldNames;
  813. FReportData.OnNext := @ReportDataNext;
  814. end;
  815. destructor TNestedGroupsDemo.Destroy;
  816. begin
  817. FreeAndNil(FReportData);
  818. rec.DelimitedText := '';
  819. FreeAndNil(rec);
  820. FreeAndNil(sl);
  821. inherited Destroy;
  822. end;
  823. class function TNestedGroupsDemo.Description: string;
  824. begin
  825. Result:='Demo showing grouping';
  826. end;
  827. end.