Charts and every chart modifier

Choose chart types, data sources, styling, labels, and rendering paths.

Read as Markdown · Open in the interactive guide

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. For an orientation and the format checklist, open Find the right merge tag.

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 <img src="/sfc/servlet.shepherd/version/download/CV_ID">
HTML → PDF Same <img> — Flying Saucer (Blob.toPdf) fetches it server-side
Word .docx <w:drawing> referencing a PNG embedded in word/media/
Word → PDF Word → HTML → PDF chain; the embedded PNG flows through transparently
PowerPoint .pptx <p:pic> 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 <img alt="…"> 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 <svg> instead of <img>. 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:

{#Survey_Questions__r}
<h2>Q{Display_Order__c}: {Question_Text__c}</h2>
{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 <img> referencing CV URL Browser fetches the CV via lightning subdomain
HTML PDF <img> referencing CV URL Flying Saucer's Blob.toPdf fetches the CV server-side (relative URL — managed-package safe)
HTML Browser preview <img> (default) OR inline <svg> (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 <p:pic> 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 <img> 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 <canvas> 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 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.

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 <table>/<tr>/<td> for layout OR <div> with display:table/table-row/table-cell.
- Do NOT use: flex, grid, gap, linear-gradient, calc(), CSS variables. Silently ignored.
- Do NOT use <svg> for charts — Flying Saucer drops it. {Chart:...} emits <img> by default.
- Wrap @page in a <style> at the top. Do not also set Page_Size__c / Page_Orientation__c /
  Custom_Margins__c on the template if the source HTML declares @page.

Chart tag syntax: {Chart:RELATIONSHIP:FIELD:STYLE:OPTS}
  - RELATIONSHIP: child relationship name on the parent record (e.g. Survey_Responses__r)
  - FIELD: field on the child to bucket by (e.g. Selected_Answer__c)
  - STYLE: bar, column, pie, donut, pivot, stacked, clustered, line, area
  - OPTS: key=value&key=value (any order). All optional unless noted.

Available modifiers:
  - title=...                — chart header text, becomes img alt text
  - width=N  height=N        — logical pixels (defaults: 500x ~by-style)
  - where=SOQL_FRAGMENT      — appended to chart's WHERE. Identifier-only.
  - groupBy=FIELD            — cross-tab dimension. REQUIRED for stacked/clustered/pivot;
                               optional for line/area (omit → single-series).
  - colSort=val1,val2,...    — author-controlled column order. Required when groupBy is used.
  - colors=#hex,#hex,...     — palette override, cycles by row/series index
  - split=DELIM              — multi-select picklist delimiter (sums to >100% by design)
  - scale=N                  — PNG supersample (default 1-2). 4 max.

Required output:
1. A single self-contained HTML file (<!DOCTYPE html>...</html>) with inline <style>.
2. A title row with {Name} of the parent record.
3. Eight H2 sections — one per chart style. Each section:
   - H2 heading numbered and named (e.g. "5. Stacked (cross-tab)")
   - A one-line description of when to use this style
   - The {Chart:...} tag itself, wrapped in <div class="chart">
   - <h2 class="page-break"> on sections 2-8 so each chart gets its own page
4. A footer line acknowledging "Portwood Chart Engine — pure-Apex PNG rasterization".

Data shape — ${MY_DATA_DESCRIPTION}
(Example: "Survey__c parent. Child relationship Survey_Responses__r. Bucket by Selected_Answer__c.
Cross-tab dimension Location__c with values 8000 Marina, 3260 Bayshore.")

Use the bucket field for bar/column/pie/donut single-dimension styles. Use the cross-tab field
in groupBy for stacked/clustered/line/area/pivot. ColSort must match the cross-tab values.

Return the complete HTML file, ready to upload as a Portwood HTML template. No commentary.

Reference templates

A ready-to-upload showcase ships in docs/:

  • docs/ChartEngineShowcase.html — all 8 chart styles against the Commute Survey Demo (Selected_Answerc bucketed by Locationc). Upload as Type=HTML, set Base_Object_API__c=Survey__c, set Query_Config__c=Survey_Questions__r (omit Survey_Responses__r — see §7.6.5 for why).
  • docs/ChartEngineShowcase.docx — same template, Word format. Same chart tags, drops directly into the Word body as plain text.
  • docs/SurveyChartExample.html — per-question single-dimension chart pattern (one chart per question via outer {#Survey_Questions__r} loop).
  • docs/CommuteSurveyExample.html — full composition of all five chart modifiers using the <div> table-row layout.

The showcase HTML is short enough to read end-to-end. Highly recommended starting point if you're authoring a template from scratch.

Resolution paths — Query_Config__c setup

How you configure Query_Config__c determines whether your charts work at 30K rows, 300K rows, or fail. The engine picks one of four resolution paths automatically:

  1. In-memory — the child collection is already loaded into the data map (small relationship, eagerly queried), no where= / groupBy= used. Aggregation runs in Apex. Fastest path, zero extra SOQL.
  2. SOQL fallback (recommended for >2000 rows) — the relationship is NOT in Query_Config__c, OR where= / groupBy= is present. The engine issues one GROUP BY aggregate. Constant cost regardless of row count.
  3. Parent-level — chart sits outside any loop (header/summary). Same in-memory / SOQL behavior as above.
  4. Giant-query parent — chart targets a relationship in giant-query templates (>2000 rows under the "giant" child). Same modifiers, same shape, server-side aggregation.

Author's rule of thumb — applies to the chart's target relationship specifically:

✅ DON'T pre-load the relationship the chart aggregates. Leave it out of Query_Config__c (or omit it from any nested subquery). The chart aggregates it server-side at constant cost.

✅ DO pre-load the relationship the chart's parent loop iterates. If the chart sits inside {#Survey_Questions__r}, the Questions subquery is still required — but it must NOT include a nested (SELECT … FROM Survey_Responses__r) underneath it.

❌ DON'T pre-load the chart's target AND also chart against it at >2000 rows — that triggers the giant-query path looking for a traditional loop body that isn't there.

The engine throws an actionable error pointing you to the fix when this misconfiguration is detected.

Worked example — Survey with 30K responses, per-question chart inside a Question loop:

Name, (SELECT Id, Question_Text__c, Display_Order__c FROM Survey_Questions__r ORDER BY Display_Order__c ASC)
  • Name loads onto the Survey for the title bar.
  • The Survey_Questions__r subquery loads each Question's Id, prompt, and order — needed because the template iterates {#Survey_Questions__r}.
  • Id in the question subquery is required — the chart's SOQL fallback uses each question's Id as the parent record to scope its GROUP BY aggregate.
  • No nested (SELECT … FROM Survey_Responses__r) — that's what keeps responses out of Apex heap and lets the chart's server-side aggregate handle them at constant cost.

The per-question chart tag inside the loop runs against each question's Id automatically. CPU stays under 500ms and SOQL queries stay under 10 regardless of whether the survey has 425 responses or 30,000.

Subquery syntax note (V1 Query Config): the V1 flat-string format uses standard SOQL-style subqueries — (SELECT field1, field2 FROM RelationshipName). Bare RelationshipName(field1,field2) does NOT parse and silently drops the relationship.

Security — FLS, sharing, and SOQL injection

Charts run with AccessLevel.USER_MODE — Salesforce enforces object permissions, field-level security, sharing rules, and record-level access at the database layer. A user without read access to the child object or grouped field sees empty buckets (catch with {:else}), never a leaked aggregate.

The chart resolver validates every dynamic identifier (relationship, child object, group field, lookup field, groupBy column) against Schema.SObjectType.fields.getMap() and Schema.SObjectType.fields.ChildRelationship before interpolation, and sanitizes where= fragments through the same keyword blocklist that protects Query Builder. SOQL injection is structurally impossible — identifiers are checked, values are bound.

Governor budget

Portwood caps chart aggregates at 50 SOQL queries per transaction (static budget inside DocGenChartBucketResolver). When the budget is exhausted, remaining charts render a single sentinel bucket labeled "Chart limit reached" — they never silently render as empty. Re-author the template to consolidate charts (or use a pivot instead of N single-dimension charts) if you hit the ceiling.

Per-chart CPU budget is tracked too — the rasterizer auto-tunes scale= defaults so an 8-chart document fits comfortably in the 10-second synchronous Apex limit.

Hand-authored {#ChartBucket:...} loops

When you want a custom layout the {Chart:...} engine doesn't ship (e.g., a chart inside a Word table cell, or a sparkline next to a text column), drop down to the underlying section tag. The aggregation is identical — same modifiers, same body fields, same SOQL fallback at scale.

Basic syntax:

{#ChartBucket:Survey_Responses__r:Selected_Answer__c}
<tr>
    <td>{key_label}</td>
    <td><div style="background:{color};width:{percent}%;height:14px;"></div></td>
    <td>{count} ({percent}%)</td>
</tr>
{/ChartBucket}

Buckets sort descending by count, alpha by key for ties. Null/blank values collapse into a single bucket labeled "Not Specified".

Body fields:

Tag Type What it returns
{key} String Raw group value (HTML-escaped)
{key_label} String Picklist label if available, else {key}; "Not Specified" for blanks
{count} Integer Records in this bucket
{percent} Decimal Percent of total, 1 decimal place (use directly as width:{percent}%)
{percent_int} Integer Rounded integer percent
{max_percent} Decimal Percent normalized so the largest bucket = 100 (for "longest bar = full" charts)
{index} Integer 1-based bucket index
{color} String Cycled palette color, #hex form (override with colors= modifier)
{color_hex} String Same color without the leading # — for Word w:shd w:fill attributes

Empty bucket lists render the body zero times. Add {:else}No responses yet. before the closing {/ChartBucket} for a fallback ({:else} splits the branch — it has no closing tag of its own).

Modifiers (pass as a third colon segment using key=value&key=value):

Modifier Example What it does
colors= colors=#1e40af,#b91c1c,#16a34a Override the default 8-color palette; cycles by row index.
where= where=Mode__c='Drive Alone' AND Reasons__c != null SOQL fragment appended to the chart's WHERE. Forces server-side aggregation.
split=; split=; Treat the field as multi-select (semicolon-separated).
groupBy= groupBy=Location__c Cross-tab pivot. Each bucket exposes a cols sub-list — iterate with {#cols}{key}={count} ({percent}%){/cols}. Total column appended.
colSort= colSort=8000 Marina,3260 Bayshore (Pairs with groupBy=.) Column ordering.

Pivot tables (groupBy=). The body becomes a row containing another loop, {#cols}. Each col is one value of the groupBy field plus a final Total column:

<div class="row">
    <div class="cell">{key_label}</div>
    {#cols}
    <div class="cell">{count} ({percent}%)</div>
    {/cols}
</div>

Inside {#cols} you get the standard bucket fields ({key} / {key_label} / {count} / {percent} / {percent_int} / {color} / {color_hex} / {index}) plus two pivot-only fields:

  • {percent_of_row} — Decimal — this cell's count as a percent of its row's total.
  • {percent_of_row_int} — Integer — same, rounded.

Use these when you want each row's segments to sum to 100% (stacked-bar composition) rather than each cell's value as a percent of the overall total.

Layout gotcha for pivot bodies: Portwood's HTML container auto-expansion reacts to nested loops by duplicating the nearest open <tr>. If you put {#cols} directly inside <tr>, every column gets a whole row. Use <div> + display:table-row + display:table-cell for pivot bodies, not <tr>/<td>. See docs/CommuteSurveyExample.html for the canonical pattern.

CSS 2.1 reminders (PDF output)

Because Flying Saucer renders PDFs as CSS 2.1:

  • ✅ <table> + <tr> + <td> for non-pivot charts
  • ✅ <div> with display: table / table-row / table-cell for pivot charts
  • ✅ Solid background:#hex; for bar fills ({color} returns #hex strings)
  • ❌ flex, grid, gap, linear-gradient(...), calc(...) — silently ignored, layout collapses
  • ❌ CSS variables — var(--brand) is dropped

For DOCX output, an HTML-authored chart template still renders the same way; Word treats the chart <div> blocks as styled boxes.

Word templates — supported as of v1.92

The {Chart:...} PNG-via-CV pipeline (shipped this release) made Word a peer of HTML for chart output. All 9 styles render correctly in .docx output. The legacy "Word doesn't do pivot/stacked/clustered" caveat is no longer in effect.

What's supported in Word today:

  • ✅ All 9 styles via {Chart:...} tags
  • ✅ All five modifiers (colors=, where=, split=, groupBy=, colSort=)
  • ✅ Outer record loops driving page-per-iteration layout (page-breaks via <w:pageBreakBefore/>)
  • ✅ Hand-authored {#ChartBucket:...} simple-bar pattern (legacy)

What's still Word-specific:

  • Hand-authored cross-tab {#ChartBucket} with {#cols} inside <w:tr> — still hits the row auto-expansion limitation. Use {Chart:...:stacked} or {Chart:...:clustered} instead, which generate PNGs server-side and embed them via <w:drawing>.

Reference Word template: docs/ChartEngineShowcase.docx (all 8 chart styles, one per page). Open in Word to see how the {Chart:...} tags drop in as plain paragraph text. The engine handles every conversion downstream.