rptjson.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. unit rptjson;
  2. {$mode objfpc}{$H+}
  3. {$I demos.inc}
  4. interface
  5. uses
  6. Classes,
  7. SysUtils,
  8. fpreport,
  9. fpreportjson,
  10. udapp;
  11. type
  12. { TJSONDemo }
  13. TJSONDemo = class(TReportDemoApp)
  14. private
  15. FReportData : TFPReportJSONData;
  16. Protected
  17. procedure InitialiseData; override;
  18. procedure CreateReportDesign;override;
  19. procedure LoadDesignFromFile(const AFilename: string);
  20. procedure HookupData(const AComponentName: string; const AData: TFPReportData);
  21. public
  22. constructor Create(AOWner :TComponent); override;
  23. destructor Destroy; override;
  24. Class function Description : string; override;
  25. end;
  26. implementation
  27. uses
  28. fpReportStreamer,
  29. fpTTF,
  30. fpJSON,
  31. jsonparser;
  32. { TJSONDemo }
  33. procedure TJSONDemo.InitialiseData;
  34. begin
  35. FReportData.Path:='Data';
  36. FReportData.LoadFromFile('countries.json');
  37. rpt.ReportData.AddReportData(FReportData);
  38. end;
  39. procedure TJSONDemo.CreateReportDesign;
  40. var
  41. p: TFPReportPage;
  42. TitleBand: TFPReportTitleBand;
  43. DataBand: TFPReportDataBand;
  44. GroupHeader: TFPReportGroupHeaderBand;
  45. Memo: TFPReportMemo;
  46. PageFooter: TFPReportPageFooterBand;
  47. begin
  48. Inherited;
  49. rpt.Author := 'Graeme Geldenhuys';
  50. rpt.Title := 'FPReport Demo 12 - JSON Data';
  51. p := TFPReportPage.Create(rpt);
  52. p.Orientation := poPortrait;
  53. p.PageSize.PaperName := 'A4';
  54. { page margins }
  55. p.Margins.Left := 30;
  56. p.Margins.Top := 20;
  57. p.Margins.Right := 30;
  58. p.Margins.Bottom := 20;
  59. p.Data := FReportData;
  60. p.Font.Name := 'LiberationSans';
  61. TitleBand := TFPReportTitleBand.Create(p);
  62. TitleBand.Layout.Height := 40;
  63. {$ifdef ColorBands}
  64. TitleBand.Frame.Shape := fsRectangle;
  65. TitleBand.Frame.BackgroundColor := clReportTitleSummary;
  66. {$endif}
  67. Memo := TFPReportMemo.Create(TitleBand);
  68. Memo.Layout.Left := 35;
  69. Memo.Layout.Top := 20;
  70. Memo.Layout.Width := 80;
  71. Memo.Layout.Height := 10;
  72. Memo.Text := 'COUNTRY AND POPULATION AS OF 2014';
  73. GroupHeader := TFPReportGroupHeaderBand.Create(p);
  74. GroupHeader.Layout.Height := 15;
  75. GroupHeader.GroupCondition := 'copy(Name,1,1)';
  76. {$ifdef ColorBands}
  77. GroupHeader.Frame.Shape := fsRectangle;
  78. GroupHeader.Frame.BackgroundColor := clGroupHeaderFooter;
  79. {$endif}
  80. Memo := TFPReportMemo.Create(GroupHeader);
  81. Memo.Layout.Left := 0;
  82. Memo.Layout.Top := 5;
  83. Memo.Layout.Width := 10;
  84. Memo.Layout.Height := 8;
  85. Memo.UseParentFont := False;
  86. Memo.Text := '[copy(Name,1,1)]';
  87. Memo.Font.Size := 16;
  88. DataBand := TFPReportDataBand.Create(p);
  89. DataBand.Layout.Height := 8;
  90. {$ifdef ColorBands}
  91. DataBand.Frame.Shape := fsRectangle;
  92. DataBand.Frame.BackgroundColor := clDataBand;
  93. {$endif}
  94. Memo := TFPReportMemo.Create(DataBand);
  95. Memo.Layout.Left := 15;
  96. Memo.Layout.Top := 0;
  97. Memo.Layout.Width := 50;
  98. Memo.Layout.Height := 5;
  99. Memo.Text := '[Name]';
  100. Memo := TFPReportMemo.Create(DataBand);
  101. Memo.Layout.Left := 70;
  102. Memo.Layout.Top := 0;
  103. Memo.Layout.Width := 30;
  104. Memo.Layout.Height := 5;
  105. Memo.Text := '[formatfloat(''#,##0'', Population)]';
  106. PageFooter := TFPReportPageFooterBand.Create(p);
  107. PageFooter.Layout.Height := 20;
  108. {$ifdef ColorBands}
  109. PageFooter.Frame.Shape := fsRectangle;
  110. PageFooter.Frame.BackgroundColor := clPageHeaderFooter;
  111. {$endif}
  112. Memo := TFPReportMemo.Create(PageFooter);
  113. Memo.Layout.Left := 130;
  114. Memo.Layout.Top := 13;
  115. Memo.Layout.Width := 20;
  116. Memo.Layout.Height := 5;
  117. Memo.Text := 'Page [PageNo]';
  118. Memo.TextAlignment.Vertical := tlCenter;
  119. Memo.TextAlignment.Horizontal := taRightJustified;
  120. end;
  121. procedure TJSONDemo.LoadDesignFromFile(const AFilename: string);
  122. var
  123. rs: TFPReportJSONStreamer;
  124. fs: TFileStream;
  125. lJSON: TJSONObject;
  126. begin
  127. if AFilename = '' then
  128. Exit;
  129. if not FileExists(AFilename) then
  130. raise Exception.CreateFmt('The file "%s" can not be found', [AFilename]);
  131. fs := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyNone);
  132. try
  133. lJSON := TJSONObject(GetJSON(fs));
  134. finally
  135. fs.Free;
  136. end;
  137. rs := TFPReportJSONStreamer.Create(nil);
  138. rs.JSON := lJSON; // rs takes ownership of lJSON
  139. try
  140. rpt.ReadElement(rs);
  141. finally
  142. rs.Free;
  143. end;
  144. end;
  145. procedure TJSONDemo.HookupData(const AComponentName: string; const AData: TFPReportData);
  146. var
  147. b: TFPReportCustomBandWithData;
  148. begin
  149. b := TFPReportCustomBandWithData(rpt.FindRecursive(AComponentName));
  150. if Assigned(b) then
  151. b.Data := AData;
  152. end;
  153. constructor TJSONDemo.Create(AOwner: TComponent);
  154. begin
  155. inherited;
  156. FReportData := TFPReportJSONData.Create(nil);
  157. end;
  158. destructor TJSONDemo.Destroy;
  159. begin
  160. FreeAndNil(FReportData);
  161. inherited Destroy;
  162. end;
  163. class function TJSONDemo.Description: string;
  164. begin
  165. Result:='Demo of native JSON data support';
  166. end;
  167. end.