rptgrouping2.pp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. unit rptgrouping2;
  2. {$mode objfpc}{$H+}
  3. {$I demos.inc}
  4. interface
  5. uses
  6. Classes,
  7. SysUtils,
  8. fpreport,
  9. strutils,
  10. udapp;
  11. type
  12. { TGrouping2Demo }
  13. TGrouping2Demo = class(TReportDemoApp)
  14. private
  15. lReportData: TFPReportUserData;
  16. sl: 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 GetReportFieldKind(Sender: TObject; aName: String; var AKind: TFPReportFieldKind);
  21. procedure GetReportFieldNames(Sender: TObject; List: TStrings);
  22. Protected
  23. procedure InitialiseData; override;
  24. procedure CreateReportDesign;override;
  25. procedure LoadDesignFromFile(const AFilename: string);
  26. procedure HookupData(const AComponentName: string; const AData: TFPReportData);
  27. public
  28. constructor Create(AOWner :TComponent); override;
  29. destructor Destroy; override;
  30. Class function Description : string; override;
  31. end;
  32. implementation
  33. uses
  34. fpReportStreamer,
  35. fpTTF,
  36. fpJSON,
  37. jsonparser,
  38. fpexprpars;
  39. { TGrouping2Demo }
  40. procedure TGrouping2Demo.GetReportDataFirst(Sender: TObject);
  41. begin
  42. {$IFDEF gdebug}
  43. writeln('GetReportDataFirst');
  44. {$ENDIF}
  45. end;
  46. procedure TGrouping2Demo.GetReportDataValue(Sender: TObject; const AValueName: String; var AValue: Variant);
  47. Var
  48. W : Integer;
  49. S : String;
  50. begin
  51. {$IFDEF gdebug}
  52. writeln(Format('GetReportDataValue - %d', [lReportData.RecNo]));
  53. {$ENDIF}
  54. W:=0;
  55. Case LowerCase(AValueName) of
  56. 'continent' : W:=1;
  57. 'region' : W:=2;
  58. 'country' : W:=3;
  59. 'iso' : W:=4;
  60. 'population' : W:=5;
  61. end;
  62. If W=0 then
  63. AValue:=''
  64. else
  65. begin
  66. S:=ExtractWord(W,sl[lReportData.RecNo-1],[';']);
  67. if W=5 then
  68. AValue:=StrToIntDef(S,5)
  69. else
  70. AValue:=S;
  71. end;
  72. end;
  73. procedure TGrouping2Demo.GetReportDataEOF(Sender: TObject; var IsEOF: Boolean);
  74. begin
  75. {$IFDEF gdebug}
  76. writeln(Format('GetReportDataEOF - %d', [lReportData.RecNo]));
  77. {$ENDIF}
  78. if lReportData.RecNo > sl.Count then
  79. IsEOF := True
  80. else
  81. IsEOF := False;
  82. end;
  83. procedure TGrouping2Demo.GetReportFieldKind(Sender: TObject; aName: String; var AKind: TFPReportFieldKind);
  84. begin
  85. if CompareText('population',aname)=0 then
  86. aKind:=rfkInteger;
  87. end;
  88. procedure TGrouping2Demo.GetReportFieldNames(Sender: TObject; List: TStrings);
  89. begin
  90. {$IFDEF gdebug}
  91. writeln('********** GetReportFieldNames');
  92. {$ENDIF}
  93. List.Add('continent');
  94. List.Add('region');
  95. List.Add('country');
  96. List.Add('ISO');
  97. List.Add('population');
  98. end;
  99. procedure TGrouping2Demo.InitialiseData;
  100. begin
  101. sl := TStringList.Create;
  102. {$I countries2.inc}
  103. sl.Sort;
  104. end;
  105. procedure TGrouping2Demo.CreateReportDesign;
  106. var
  107. p: TFPReportPage;
  108. TitleBand: TFPReportTitleBand;
  109. DataBand: TFPReportDataBand;
  110. GroupHeader: TFPReportGroupHeaderBand;
  111. Memo: TFPReportMemo;
  112. PageFooter: TFPReportPageFooterBand;
  113. GroupFooter: TFPReportGroupFooterBand;
  114. begin
  115. Inherited;
  116. rpt.Author := 'Graeme Geldenhuys';
  117. rpt.Title := 'FPReport Demo 3 - Grouping';
  118. {*** page ***}
  119. p := TFPReportPage.Create(rpt);
  120. p.Orientation := poPortrait;
  121. p.PageSize.PaperName := 'A4';
  122. { page margins }
  123. p.Margins.Left := 30;
  124. p.Margins.Top := 20;
  125. p.Margins.Right := 30;
  126. p.Margins.Bottom := 20;
  127. p.Data := lReportData;
  128. p.Font.Name := 'LiberationSans';
  129. {*** title ***}
  130. TitleBand := TFPReportTitleBand.Create(p);
  131. TitleBand.Layout.Height := 40;
  132. {$IFDEF ColorBands}
  133. TitleBand.Frame.Shape := fsRectangle;
  134. TitleBand.Frame.BackgroundColor := clReportTitleSummary;
  135. {$ENDIF}
  136. Memo := TFPReportMemo.Create(TitleBand);
  137. Memo.Layout.Left := 0;
  138. Memo.Layout.Top := 20;
  139. Memo.Layout.Width := p.PageSize.Width - p.Margins.Left - p.Margins.Right;
  140. Memo.Layout.Height := 10;
  141. Memo.TextAlignment.Horizontal := taCentered;
  142. Memo.Text := 'COUNTRY AND POPULATION AS OF 2014';
  143. GroupHeader := TFPReportGroupHeaderBand.Create(p);
  144. GroupHeader.Layout.Height := 15;
  145. GroupHeader.GroupCondition := 'data.continent';
  146. {$ifdef ColorBands}
  147. GroupHeader.Frame.Shape := fsRectangle;
  148. GroupHeader.Frame.BackgroundColor := clGroupHeaderFooter;
  149. {$endif}
  150. Memo := TFPReportMemo.Create(GroupHeader);
  151. Memo.Layout.Left := 5;
  152. Memo.Layout.Top := 3;
  153. Memo.Layout.Width := 10;
  154. Memo.Layout.Height := 8;
  155. Memo.UseParentFont := False;
  156. Memo.Text := '[data.continent]';
  157. Memo.Font.Size := 16;
  158. {*** detail ***}
  159. DataBand := TFPReportDataBand.Create(p);
  160. DataBand.Layout.Height := 8;
  161. {$ifdef ColorBands}
  162. DataBand.Frame.Shape := fsRectangle;
  163. DataBand.Frame.BackgroundColor := clDataBand;
  164. {$endif}
  165. //DataBand.VisibleExpr := 'StrToFloat(''[population]'') > 50000000';
  166. Memo := TFPReportMemo.Create(DataBand);
  167. Memo.Layout.Left := 15;
  168. Memo.Layout.Top := 2;
  169. Memo.Layout.Width := 45;
  170. Memo.Layout.Height := 5;
  171. Memo.Text := '[data.country]';
  172. Memo := TFPReportMemo.Create(DataBand);
  173. Memo.Layout.Left := 55;
  174. Memo.Layout.Top := 2;
  175. Memo.Layout.Width := 25;
  176. Memo.Layout.Height := 5;
  177. Memo.TextAlignment.Horizontal := taRightJustified;
  178. Memo.Text := '[data.population]';
  179. {*** group footer ***}
  180. GroupFooter := TFPReportGroupFooterBand.Create(p);
  181. GroupFooter.Layout.Height := 15;
  182. GroupFooter.GroupHeader := GroupHeader;
  183. {$ifdef ColorBands}
  184. GroupFooter.Frame.Shape := fsRectangle;
  185. GroupFooter.Frame.BackgroundColor := clGroupHeaderFooter;
  186. {$endif}
  187. Memo := TFPReportMemo.Create(GroupFooter);
  188. Memo.Layout.Left := 25;
  189. Memo.Layout.Top := 5;
  190. Memo.Layout.Width := 100;
  191. Memo.Layout.Height := 8;
  192. Memo.UseParentFont := False;
  193. Memo.TextAlignment.Horizontal := taRightJustified;
  194. Memo.Text := 'Total for [data.continent]: [FormatFloat(''#,###0.00'',sum(data.population/1000000))] million.';
  195. // Memo.Options:=Memo.Options+[moNoResetAggregateOnPrint];
  196. Memo.Font.Size := 16;
  197. {*** page footer ***}
  198. PageFooter := TFPReportPageFooterBand.Create(p);
  199. PageFooter.Layout.Height := 20;
  200. {$ifdef ColorBands}
  201. PageFooter.Frame.Shape := fsRectangle;
  202. PageFooter.Frame.BackgroundColor := clPageHeaderFooter;
  203. {$endif}
  204. Memo := TFPReportMemo.Create(PageFooter);
  205. Memo.Layout.Left := 100;
  206. Memo.Layout.Top := 13;
  207. Memo.Layout.Width := 50;
  208. Memo.Layout.Height := 5;
  209. Memo.Text := 'Page [PageNo] of [PAGECOUNT]';
  210. Memo.TextAlignment.Vertical := tlCenter;
  211. Memo.TextAlignment.Horizontal := taRightJustified;
  212. end;
  213. procedure TGrouping2Demo.LoadDesignFromFile(const AFilename: string);
  214. var
  215. rs: TFPReportJSONStreamer;
  216. fs: TFileStream;
  217. lJSON: TJSONObject;
  218. begin
  219. if AFilename = '' then
  220. Exit;
  221. if not FileExists(AFilename) then
  222. raise Exception.CreateFmt('The file "%s" can not be found', [AFilename]);
  223. fs := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyNone);
  224. try
  225. lJSON := TJSONObject(GetJSON(fs));
  226. finally
  227. fs.Free;
  228. end;
  229. rs := TFPReportJSONStreamer.Create(nil);
  230. rs.JSON := lJSON; // rs takes ownership of lJSON
  231. try
  232. rpt.ReadElement(rs);
  233. finally
  234. rs.Free;
  235. end;
  236. end;
  237. procedure TGrouping2Demo.HookupData(const AComponentName: string; const AData: TFPReportData);
  238. var
  239. b: TFPReportCustomBandWithData;
  240. begin
  241. b := TFPReportCustomBandWithData(rpt.FindRecursive(AComponentName));
  242. if Assigned(b) then
  243. b.Data := AData;
  244. end;
  245. constructor TGrouping2Demo.Create(AOwner: TComponent);
  246. begin
  247. inherited;
  248. lReportData := TFPReportUserData.Create(nil);
  249. lReportData.Name:='Data';
  250. lReportData.OnGetValue := @GetReportDataValue;
  251. lReportData.OnGetEOF := @GetReportDataEOF;
  252. lReportData.OnFirst := @GetReportDataFirst;
  253. lReportData.OnGetNames := @GetReportFieldNames;
  254. lReportData.OnGetFieldKind:=@GetReportFieldKind;
  255. end;
  256. destructor TGrouping2Demo.Destroy;
  257. begin
  258. FreeAndNil(lReportData);
  259. FreeAndNil(sl);
  260. inherited Destroy;
  261. end;
  262. class function TGrouping2Demo.Description: string;
  263. begin
  264. Result:='Demo showing grouping and totals';
  265. end;
  266. end.