# Charts and every chart modifier Start with a one-line chart, then add only the options you need. Chart rendering depends on the output format and the way the document is generated; use the matrix below before building a large report. **Before you start:** Choose the fields and relationships in [Choose information](#choose-data). For an orientation and the format checklist, open [Find the right merge tag](#merge-tags). #### Charts Render **nine chart styles** inline in your generated documents — bar, column, pie, donut, pivot, stacked, clustered, line, and area. Charts use a prepared PNG image on supported paths, with browser rendering for interactive work and an Apex rasterizer for server work. HTML can also use CSS chart blocks or opt-in browser-only SVG. Excel output does not embed chart-tag images. Test the exact entry point you intend to use: | Output | How charts get there | | ------------------ | --------------------------------------------------------------------- | | HTML → browser | `` | | HTML → PDF | Same `` — Flying Saucer (`Blob.toPdf`) fetches it server-side | | Word `.docx` | `` referencing a PNG embedded in `word/media/` | | Word → PDF | Word → HTML → PDF chain; the embedded PNG flows through transparently | | PowerPoint `.pptx` | `` referencing a PNG in `ppt/media/` | | Excel `.xlsx` | (Coming in a follow-up release) | **Zero external callouts.** The chart engine is 100% native Apex — same constraint that makes Portwood suitable for AppExchange security review and managed-package customers. Charts work in Flow actions, batch jobs, Queueables, and Apex tests as well as the runner LWC. **Two authoring paths:** 1. **`{Chart:relationship:field:style:opts}`** — one-line tag. The engine resolves bucket data via SOQL aggregate (constant cost regardless of row count), rasterizes to PNG, embeds the image. Works on supported Word, HTML/PDF, Canvas, and PowerPoint paths; Excel image embedding is not supported. **This is the canonical path — use it for 95% of cases.** 2. **`{#ChartBucket:relationship:field}…{/ChartBucket}`** — hand-authored loop. You write the chart HTML body yourself with `{key}` / `{count}` / `{percent}` placeholders. Use only when you need a custom layout the engine doesn't ship (e.g., embedded sparklines inside a table cell). #### One-line `{Chart:...}` blocks A single tag that expands into a complete chart image at render time. **Tag syntax:** ``` {Chart:RELATIONSHIP:FIELD:STYLE:OPTS} ``` | Position | Required | Meaning | | -------------- | -------- | ------------------------------------------------------ | | `RELATIONSHIP` | yes | Child relationship name on the parent record (one-hop) | | `FIELD` | yes | Field on the child to group by | | `STYLE` | no | One of nine styles (default: `bar`) | | `OPTS` | no | `key=value&key=value` modifiers | **All nine styles:** | Style | Visual | Cross-tab? | Best for | | ----------- | ---------------------------------------------------------------------------- | ---------- | -------------------------------------------------------- | | `bar` | Horizontal bar per bucket, label left, count+% right | no | One question, one dimension. Long category labels. | | `column` | Vertical bar per bucket, labels under, % above | no | One question, one dimension. Short labels. | | `pie` | Pie chart with right-side legend showing count + % | no | "Share of total" framing, ≤8 slices | | `donut` | Pie with a center hole, same legend | no | Same as pie, lighter visual weight | | `pivot` | Cross-tab table — rows = buckets, cols = `groupBy` values + Total | required | Numeric matrix readout. CSS-bar path (HTML-friendly). | | `stacked` | Horizontal stacked bar — one row per bucket, segments by `groupBy` | required | "How does each row break down across the 2nd dimension?" | | `clustered` | Vertical clustered bars — one cluster per bucket, mini-bar per col | required | Side-by-side comparison across the 2nd dimension | | `line` | Polyline through (bucket index, count). Multi-series when `groupBy` present. | optional | Trend / ordering matters. Single or multi-series. | | `area` | Line chart with semi-transparent fill below each series | optional | Trend + accumulated volume | **Worked examples** (against the canonical Commute Survey Demo — 425 responses, 6 modes, 2 locations): ``` {Chart:Survey_Responses__r:Selected_Answer__c:bar:title=Commute Mode Distribution} {Chart:Survey_Responses__r:Selected_Answer__c:column:title=Commute Mode (Vertical)} {Chart:Survey_Responses__r:Selected_Answer__c:pie:title=Commute Mode Share} {Chart:Survey_Responses__r:Selected_Answer__c:donut:title=Commute Mode Share (Donut)} {Chart:Survey_Responses__r:Selected_Answer__c:stacked:groupBy=Location__c&colSort=8000 Marina,3260 Bayshore&title=Location Mix per Mode} {Chart:Survey_Responses__r:Selected_Answer__c:clustered:groupBy=Location__c&colSort=8000 Marina,3260 Bayshore&title=Mode by Location Comparison} {Chart:Survey_Responses__r:Selected_Answer__c:line:groupBy=Location__c&colSort=8000 Marina,3260 Bayshore&title=Commute Trend by Location} {Chart:Survey_Responses__r:Selected_Answer__c:area:groupBy=Location__c&colSort=8000 Marina,3260 Bayshore&title=Commute Volume by Location} {Chart:Survey_Responses__r:Selected_Answer__c:pivot:groupBy=Location__c&colSort=8000 Marina,3260 Bayshore&title=Mode by Location (Table)} ``` #### Complete modifier reference Modifiers come after the style, joined with `&`. **Order doesn't matter.** Values containing `&` or `=` are not supported (use `where=` for SOQL with operators — see below). | Modifier | Applies to | Example | What it does | | ------------- | --------------------------------- | -------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `title=` | all styles | `title=How satisfied are you with your role?` | Chart header text drawn above the chart. Becomes the `…` attribute on the HTML side. | | `width=` | all styles | `width=420` | Logical-pixel width of the chart. Defaults: 540 (stacked/clustered), 500 (bar/column/line/area), 360 (pie/donut). Word/PDF downsample bigger PNGs for free AA. | | `height=` | all styles | `height=240` | Logical-pixel height. Defaults vary per style (see source); bar auto-grows by bucket count. | | `fontSize=` | all styles | `fontSize=16` | Axis-label size in pixels. Default 12; titles and other text scale with it, keeping their proportions. Raise it when the chart lands in a small frame. On a Canvas chart use the **Label size** box rather than writing the tag. Honoured by all three renderers — browser, Word/PowerPoint, and HTML-to-PDF. (v3.56+) | | `where=` | all styles | `where=Survey_Question__r.Display_Order__c=1` | SOQL fragment appended to the chart's `WHERE`. Identifier-only, sanitized through the same keyword blocklist as Query Builder. Forces server-side aggregation. | | `groupBy=` | stacked/clustered/pivot/line/area | `groupBy=Department__c` | Cross-tab dimension. **Required** for `stacked`/`clustered`/`pivot`; **optional** for `line`/`area` (omit → single-series). Field on the same child object as `FIELD`. | | `colSort=` | stacked/clustered/pivot/line/area | `colSort=Engineering,Sales,Marketing,Support,Operations` | Author-controlled column ordering. Named values appear first in this order; remaining values alpha-sorted; synthetic `Total` always last (pivot path only). | | `colors=` | all styles | `colors=#1e40af,#b91c1c,#16a34a` | Override the default 8-color palette. Cycles by row/series index. Each color is `#hex` (6 chars). | | `split=` | all (bucket-shape only) | `split=;` | Multi-select delimiter. Each respondent's pick contributes to every value they selected. Percentages sum to >100% by design. | | `scale=` | all (raster path only) | `scale=2` | Supersample multiplier. Default 1 for stacked/clustered/line/area, 2 for bar/column/pie/donut. Larger = sharper PNG but slower; 4 is the practical ceiling. | | `htmlRender=` | HTML browser preview | `htmlRender=svg` | (HTML only, opt-in.) Inline `` instead of ``. **Browser-only — Flying Saucer drops SVG**. Prepared PNG images work in browser and PDF; without image preparation, supported CSS chart styles are the fallback. | **Composability:** every modifier composes with every other modifier where it makes sense: ``` {Chart:Survey_Responses__r:Mode__c:stacked:groupBy=Location__c&colSort=Marina,Bayshore&colors=#1e40af,#b91c1c&where=CreatedDate=THIS_YEAR&title=Modes by Office, 2026} ``` **Inside a relationship loop**, the chart's relationship resolves against the iterating record: ```html {#Survey_Questions__r}

Q{Display_Order__c}: {Question_Text__c}

{Chart:Survey_Responses__r:Selected_Answer__c:bar} {/Survey_Questions__r} ``` Here `Survey_Question__c.Survey_Responses__r` is the child relationship from Question (not from the outer Survey). One chart per question, no `where=` needed. > **If chart labels come out too small, raise `fontSize=`.** Label sizes are in canvas > pixels, so how big they look in the finished document depends on how far the image is > scaled to fit its frame. > > On a **Canvas** template that is straightforward: the chart block renders at its own > size, so the 12px default prints at about 9pt whatever size the block is. Set **Label > size** in the chart properties to change it. > > On a **PowerPoint or Word** template the image is stretched to fit the shape you drew, > and `width=` does not change that — a chart squeezed into a 3-inch shape has small > labels however many pixels it was rendered at. Raising `width=` sharpens the picture > while making the text smaller relative to the frame, which is the opposite of what > most people expect. `fontSize=` is the knob: adjust it until it reads well at the size > the shape actually is. **Error handling.** Malformed tags render an inline error block in HTML output (red border, monospace tag dump) and a `[Chart error: …]` text placeholder in Word — you see the problem at first preview rather than chasing a silent miss. Errors include the original tag verbatim so you can diff it against a working sample. #### Output-format matrix — what flows where The chart engine can emit prepared PNGs, HTML CSS chart blocks, or opt-in browser-only inline SVG. The result depends on the calling path and whether chart images were prepared. The runner LWC, Flow actions, batch jobs, and the Apex `generate*` entry points all orchestrate chart preparation automatically — **you do not call `prepareChartImages` in your template; just write the tags.** | Template Type | Output | Image format | Engine path | | ------------- | ---------------- | --------------------------------------------- | --------------------------------------------------------------------------------------------- | | HTML | HTML download | `` referencing CV URL | Browser fetches the CV via lightning subdomain | | HTML | PDF | `` referencing CV URL | Flying Saucer's `Blob.toPdf` fetches the CV server-side (relative URL — managed-package safe) | | HTML | Browser preview | `` (default) OR inline `` (opt-in) | Lightning UI / Experience Cloud render natively | | Word | DOCX | PNG embedded in `word/media/` | LWC orchestrates: SVG → canvas → PNG → CV → client-side ZIP assembly fetches CV bytes | | Word | PDF | PNG embedded in `word/media/`, then converted | Same DOCX assembly, then DocGenHtmlRenderer → `Blob.toPdf` | | Word | Sample PDF | PNG embedded | Same path | | PowerPoint | PPTX | PNG in `ppt/media/` via `` | Server-side OOXML embed, or client-side Chart.js on the giant-query path (below) | | Excel | XLSX | Chart-tag image embedding is not supported | Use a supported Word/PDF or PowerPoint output when chart images are required | | Canvas | PDF | `` referencing CV URL | Chart block previews live via Chart.js; the tag renders like any other HTML chart | | Flow / batch | any of the above | PNG via pure-Apex rasterizer | `DocGenChartImageController.prepareChartImagesServerSide` — no browser canvas, no callouts | #### Charts on large datasets — client-side rendering (v3.55+) The pure-Apex rasterizer runs inside a synchronous transaction, and both the chart data and the rendered PNGs have to fit in a 6 MB Apex heap. On a record with thousands of child rows that is not a limit you can tune around — a PowerPoint or Excel template over a few thousand child records used to fail outright with _"too large for sync PowerPoint/Excel output"_. For PowerPoint and Excel, Portwood now takes the same route it has always taken for Word: **the browser does the work.** The runner pages the child records down in chunks, aggregates each page into buckets as it arrives, and renders the finished chart with **Chart.js** on a real `` before embedding the PNG. Nothing large is ever held in Apex at once, so the heap ceiling stops being the constraint — verified against 30,000 child records. You do not opt in and you do not change your tags. Write `{Chart:...}` exactly as documented above; the runner picks the path. What you should know: - **It needs a browser.** Flow, batch and Apex entry points still use the server-side rasterizer, which is why the heap guidance in [§14.8](https://github.com/Portwood-Global-Solutions/Portwood/blob/main/UserGuide.md#148-template--output-size-guidance) still applies to unattended generation. - **Non-groupable fields are refused with a message.** A Long Text Area cannot be used in a SOQL `GROUP BY`, and attempting it throws an uncatchable platform exception — so Portwood checks the field first and tells you, rather than letting the transaction die. Group by a Text, Picklist, Number, Date or Checkbox field instead. - **Charts still render on the Canvas artboard** while you design, using the same library. **Cleanup.** Each generated chart produces a transient `docgen_chart_*` ContentVersion. The runner cleans them up after the document downloads/saves. A daily orphan reaper (`DocGenChartCleanupSchedulable`) sweeps stragglers (failures or browser-closed-mid-flow). #### LLM authoring prompt — copy this to generate a template Paste the block below into Claude / GPT / Gemini with one substitution: `${MY_DATA_DESCRIPTION}` should describe your child relationship name, the dimension field you want bucketed, and (optionally) the cross-tab field plus its expected values. The LLM will produce a complete HTML template you can upload. ```text You are writing a Portwood HTML template that renders 8 chart styles using Portwood's {Chart:...} tag syntax. Portwood is a native Salesforce 2GP package; the template is a single HTML file uploaded as a ContentVersion against a DocGen_Template__c with Type__c='HTML'. Hard constraints (Flying Saucer PDF engine, CSS 2.1 + small CSS 3 subset): - Use //
for layout OR
with display:table/table-row/table-cell. - Do NOT use: flex, grid, gap, linear-gradient, calc(), CSS variables. Silently ignored. - Do NOT use for charts — Flying Saucer drops it. {Chart:...} emits by default. - Wrap @page in a