ECharts
echarts fence; Hugo validates them at build time and the browser draws a theme-aware chart with the local ECharts.The body of an echarts fence is an ECharts option object in YAML or JSON — not
code. Use it for quantitative charts that need axes, series and a legend. For
relationships and flows use Mermaid; for order and
hierarchy use Infographic. Hugo parses the
options at build time; invalid input warns and leaves its source readable in an
ordinary preview, while strict publishing rejects the warning. The browser
draws with the ECharts copy the theme ships, and only a page that uses it loads
the runtime.
Shortest form
A bar chart needs three parts: xAxis, yAxis, series. Below is how many
pages each of the six documentation sections has.
Both formats are accepted; YAML needs no quotes or commas and is shorter to write. Broken indentation, or a body that parses to an array instead of a map, warns on that line and renders the source instead of a blank chart. Strict publishing rejects the warning.
Multiple line series
series is an array, so another entry is another line, and legend lets the
reader hide one. Below are the release years of PostgreSQL major versions and
the end-of-support years implied by the community’s five-year policy.
Quote the version numbers: unquoted 10 is a number in YAML and so is 9.6,
but as category-axis labels they have to be strings.
Pie and doughnut charts
Give radius two values for a doughnut. Below is how OINK’s 29 shortcodes break
down by purpose.
{b}, {c} and {d} are ECharts template placeholders — name, value,
percentage. Writing them in a string is enough; no function is needed.
Height and full width
height defaults to 400px and accepts px rem em vh vw %. full=true drops
the reading-column limit so the chart fills the content area, which suits charts
with many points or long labels.
An invalid height (360, 36pt) warns and uses the default in ordinary
preview; strict publishing rejects the warning.
Light and dark
Without theme, a chart initializes in the reader’s current colour scheme and
redraws in place when that changes — no page reload. It resizes automatically
when its container does. Switch this page to dark and the ground and text of
every chart above change with it.
A fixed theme pins the colours in both modes:
dark is the only theme built into the runtime; any other ECharts theme has to
be registered with echarts.registerTheme() before it can be named here.
Without a branding requirement, leave theme out and let the chart follow the
site.
Callbacks with $fn:
A fence is data and cannot carry JavaScript. When an option needs a function — a
tooltip formatter, a data-driven colour — write the string "$fn:name" in the
options and register that name on window.OinkEchartsFunctions:
Hover any bar and the tooltip is the sentence that function builds. An
unregistered name resolves to undefined, the chart is drawn as if the option
were not set, and neither the build nor the runtime complains. Keep the script
next to the fence so they change together.
That script is site code and deserves code review. Formatting a string template
({b}, {c}, {d}) can express does not need a function.
Where the data lives
A fence body is a literal. Hugo does not expand shortcodes, front matter
variables or files under data/ inside it — the numbers are written in the
fence. The cost is that data cannot be shared; the benefit is that the chart and
its data go into Git together and a diff shows which number moved.
Do not draw data that changes often (version matrices, asset lists). Use a
table or the data/-driven components on a
release page.
Output
| Output | Shape |
|---|---|
| HTML | A canvas container inside <div class="td-echarts"> plus an application/json options block; the local ECharts draws it |
No chart; the fence source inside <pre class="td-echarts-source"> |
|
| Markdown | The echarts fence and its option source, kept as written |
| RSS | Same as print — source only |
Whatever the chart shows, say it in the prose too: print and RSS have no chart.
Parameter reference
The fence attribute line (```echarts {…}):
style, on*, and unknown attributes warn and are ignored. A fence body that
does not parse to a YAML/JSON map warns and renders as source. Strict publishing
rejects all these warnings. The option keys themselves are ECharts’, documented in the
official option manual.
There is no site-level parameter: ECharts needs no switch in hugo.yml and
loads only where it is used.
Limits
- No JavaScript in the fence: bridge through
$fn:when a function is needed, and remember an unregistered name resolves toundefinedwith no error. - The fence reads no external data:
data/, front matter and shortcodes are all out of reach; the numbers live in the fence. - Print and RSS carry the source only, so the conclusion belongs in the prose.
- YAML type coercion:
10,9.6,onandyeson a category axis become numbers or booleans and need quotes. - Colour is not the only distinction: in a multi-series chart vary line style or marker shape too, and check legend contrast in both colour schemes.
Related
- Infographic — structure and order, not statistics
- Tables — for few values that must be read exactly
- Mermaid — relationship and flow diagrams
- Code blocks — the general rules for fence attribute lines