ECharts
echarts fence, with colors that follow light and dark mode.An echarts fence draws a chart with axes, series and a legend: table sizes,
release timelines, the share of each category in a total. You describe the
chart as an ECharts option object in YAML or JSON, so the fence holds data,
not code, and the numbers sit in your Markdown where a diff shows every change.
Hugo checks the options while it builds, the theme’s bundled copy of ECharts
draws the chart in the reader’s browser, and the colors follow light and dark
mode. There is nothing to configure, and pages without a chart load nothing.
For relationships and flows, use Mermaid; for
order and hierarchy, use Infographic.
Shortest form
A bar chart needs xAxis, yAxis and series. This one shows the size of six
tables in an application database:
YAML and JSON both work; YAML is shorter because it needs no quotes or commas. If the options do not parse, or parse to a list instead of a mapping, the build warns with the source line and the page shows your source instead of an empty chart.
Multiple line series
series is a list, with one entry per line, and legend lets readers hide a
line by clicking its name. This chart shows when each PostgreSQL major version
was released and when the community’s five-year support ends:
The version numbers are quoted because YAML reads a bare 10 or 9.6 as a
number, and labels on a category axis must be strings.
Pie and doughnut charts
Give radius two values to cut a hole in a pie and get a doughnut. This one
groups OINK’s 29 shortcodes by purpose:
{b}, {c} and {d} are ECharts placeholders for the name, the value and
the percentage. A formatter string with placeholders covers most labels and
tooltips without a function.
Height and full width
height defaults to 400px and accepts px, rem, em, vh, vw and %.
full=true lets the chart leave the reading column and use the whole content
area, which gives many data points or long labels room:
An invalid height such as 360 or 36pt warns and uses 400px.
Light and dark
Without theme, a chart takes the reader’s current color scheme and redraws
in place when the reader switches, without a reload; it also resizes with its
container. Switch this page to dark mode to watch the charts above change
their background and text.
A fixed theme keeps the same colors in both modes:
dark is the only theme built into ECharts. Leave theme out unless your
brand requires fixed colors, so that charts match the rest of the page.
For a theme of your own, define it on window.OinkEchartsThemes under the name
the fence uses. ECharts loads only when the first chart comes on screen, so a
site script has no moment to call echarts.registerTheme() itself; instead
each chart registers every theme it finds there before it draws:
A theme is a plain object in ECharts’ theme format, such as the JSON its theme
builder exports. To offer it on every page, put the script in
layouts/_partials/hooks/body-end.html instead of a page. A name defined
nowhere draws with ECharts’ default colors and no warning, and an entry named
dark replaces the palette that charts without theme use in dark mode.
Callbacks with $fn:
A fence holds data and cannot contain JavaScript. When an option needs a
function, such as a tooltip that computes a share or a color that depends on
the value, write the string "$fn:name" in the options and register a
function under that name on window.OinkEchartsFunctions:
Hover over a bar to see the sentence the function builds. A name that is not
registered becomes undefined, and the chart draws as if the option were
missing, with no warning at build time or in the browser, so keep the script
next to the fence where both change together. The script is your site’s own
code: review it like any other, and on a site with a Content Security Policy
allow it as described under Deploy. Use a function
only when a formatter string with {b}, {c} and {d} cannot do the job.
Where the data lives
Hugo does not expand shortcodes, front matter values or data/ files inside a
fence, so the numbers are written in the fence itself. Two charts cannot share
one dataset this way, but each chart travels with its data and a diff shows
exactly which number changed.
Data that changes often, such as version matrices or download lists, should
not be a chart. Use a table, or the data/-driven
components of a release page.
Output
Print and RSS show the fence’s option source instead of a chart, and the page’s Markdown output keeps the fence as written. Say what the chart shows in the surrounding prose, so that those readers get the conclusion too.
Parameter reference
The fence attribute line (```echarts {…}):
Unknown attributes warn and are ignored; Hugo takes style as a highlighting
option and strips on*, so both are ignored without a warning. An empty
fence warns and renders nothing. The option keys themselves belong to ECharts
and are listed in its option manual.
ECharts has no site-level setting in hugo.yml.
Limits
- A fence cannot contain JavaScript; functions go through
$fn:and themes throughwindow.OinkEchartsThemes, and a name that is not registered fails silently. - A fence cannot read
data/, front matter or shortcodes; the numbers must be in the fence. - Quote category values such as
10,9.6,onandyes, which YAML would otherwise turn into numbers or booleans. - Do not rely on color alone to tell series apart. Vary the line style or marker shape as well, and check the legend’s contrast in both color 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