program GroupedReport; // Atlas Analytics quick start: define a grouped report, run it over a // TAtlasTable, export it to HTML and prepare a print document, all without // a window. The program checks its own results and exits with code 1 on any // mismatch. {$APPTYPE CONSOLE} uses System.SysUtils, System.IOUtils, Atlas.Data, Atlas.Analytics, Atlas.Reports, Atlas.Printing; function SalesTable: TAtlasTable; begin Result := TAtlasTable.Create(nil); try Result.AddColumn('Region'); Result.AddColumn('Product'); Result.AddColumn('Revenue', ackNumber); Result.AddColumn('Units', ackNumber); Result.AddColumn('Booked', ackDate); Result.ImportCSV( 'Region,Product,Revenue,Units,Booked' + sLineBreak + 'East,Desktop,18000,12,2026-09-01' + sLineBreak + 'West,Desktop,22500,15,2026-09-02' + sLineBreak + 'East,Service,6400,8,2026-09-03' + sLineBreak + 'North,Desktop,12000,8,2026-09-04' + sLineBreak + 'West,Service,9600,12,2026-09-05' + sLineBreak + 'North,Service,4800,6,2026-09-06'); except Result.Free; raise; end; end; procedure Check(Condition: Boolean; const Message: string); begin if not Condition then raise Exception.Create('Check failed: ' + Message); end; procedure Expect(const Expected, Actual, What: string); begin Check(Expected = Actual, What + ': expected "' + Expected + '", got "' + Actual + '"'); end; var Source, Summary, Detail: TAtlasTable; Report: TAtlasReportDefinition; Document: TAtlasPrintDocument; HTML, FileName, JSON: string; I: Integer; begin try Source := SalesTable; Report := TAtlasReportDefinition.Create(nil); try Report.Title := 'Sales by region'; Report.Subtitle := 'Revenue, units and average sale per region'; Report.GroupBy(['Region'], [ TAtlasMeasure.Create('Revenue', 'Revenue', amSum), TAtlasMeasure.Create('Units', 'Units', amSum), TAtlasMeasure.Create('Revenue', 'Average sale', amAverage), TAtlasMeasure.Create('', 'Orders', amCount)]); // 1. Execute returns a new table that the caller owns. Summary := Report.Execute(Source); try Writeln('Grouped report: ', Summary.RowCount, ' rows, ', Summary.ColumnCount, ' columns'); for I := 0 to Summary.RowCount - 1 do Writeln(' ', Summary.Cell(0, I):6, ' revenue ', Summary.Cell(1, I):6, ' units ', Summary.Cell(2, I):3, ' average ', Summary.Cell(3, I):6, ' orders ', Summary.Cell(4, I)); Check(Summary.RowCount = 3, 'three regions'); Expect('East', Summary.Cell(0, 0), 'groups keep first-seen order'); Expect('24400', Summary.Cell(1, 0), 'East revenue'); Expect('20', Summary.Cell(2, 0), 'East units'); Expect('12200', Summary.Cell(3, 0), 'East average sale'); Expect('2', Summary.Cell(4, 0), 'East orders'); Expect('32100', Summary.Cell(1, 1), 'West revenue'); Expect('16800', Summary.Cell(1, 2), 'North revenue'); finally Summary.Free; end; // 2. Filters narrow the rows before they are grouped; the source is untouched. Report.SetFilters([TAtlasFilter.Create('Revenue', '5000', afGreaterEqual)]); Summary := Report.Execute(Source); try Expect('12000', Summary.Cell(1, 2), 'North revenue with filter'); Expect('1', Summary.Cell(4, 2), 'North orders with filter'); finally Summary.Free; end; Check(Source.RowCount = 6, 'source table unchanged'); // 3. RenderHTML writes a complete, escaped, printable page. Report.SetFilters([]); HTML := Report.RenderHTML(Source); FileName := TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), 'sales-by-region.html'); TFile.WriteAllText(FileName, HTML, TEncoding.UTF8); Check(Pos('Sales by region', HTML) > 0, 'HTML title'); Check(Pos('Average sale', HTML) > 0, 'HTML column header'); Check(Pos('24400', HTML) > 0, 'HTML numeric cell'); Check(Pos('

3 result rows

', HTML) > 0, 'HTML row count'); Writeln('Wrote ', FileName, ' (', Length(HTML), ' characters)'); // 4. The definition round-trips through JSON. JSON := Report.ToJSON; Report.LoadJSON(JSON); Expect(JSON, Report.ToJSON, 'JSON round trip'); Writeln('Definition: ', JSON); // 5. PreparePrint returns a paginated document the caller owns. Document := Report.PreparePrint(Source); try Check(Document.PageCount = 1, 'one printed page'); Writeln('Print document: ', Document.PageCount, ' page, title "', Document.Title, '"'); finally Document.Free; end; // 6. SelectFields turns the same definition into a detail report. Report.SelectFields(['Region', 'Product', 'Revenue']); Report.SetFilters([TAtlasFilter.Create('Product', 'Desktop')]); Detail := Report.Execute(Source); try Check(Detail.RowCount = 3, 'three Desktop rows'); Check(Detail.ColumnCount = 3, 'three selected columns'); Writeln('Detail report: ', Detail.RowCount, ' Desktop rows'); finally Detail.Free; end; finally Report.Free; Source.Free; end; Writeln('PASS'); except on E: Exception do begin Writeln(E.ClassName, ': ', E.Message); ExitCode := 1; end; end; end.