rptjson.pp 4.5 KB

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