Chart

A line chart takes rows and a series

Each chart is a canvas with an aria-label inside a .chart box, and one call to tsunami.chart() draws it. A series with surface: true fills a flat, translucent area under its line.

Bars with a legend

legend: true shows the legend under the chart. The default type is bar; the series take the colours of the palette in turn.

The legend aligns to the start, center or end

legendAlign places the legend under the chart: start, center (the default) or end.

Two lines: this year against last year

A color takes any CSS colour or var(--ok-fg). dataset goes straight to Chart.js, here to draw the comparison dashed.

Stacked bars

Stacking is a Chart.js option: set stacked on both scales through options.

Bars with a line on a second axis

A series with its own type is drawn as that type, and an axis such as y1 gives it a scale of its own on the right.

A doughnut: each row is a slice

pie, doughnut and polarArea take one series. Each row is a slice, named by labelKey, and a row may carry its own color; the others take the palette in turn.

Performance: stacked bars and a total line

A series with total: true is listed last in the tooltip, under a line. Chart.js options format the axis and the tooltip.

A wide box

The box is 16:9. Another ratio is an inline aspect-ratio on the .chart wrapper; the canvas fills it.

Calling it again redraws the chart

A second call on the same canvas replaces the data and returns a new Chart.js instance. Here two buttons choose the year.

A page loads Chart.js itself, before the Tsunami script

The drawing is Chart.js 4.5.1 (MIT licence). It is not in tsunami.min.js: a page that has a chart loads dist/chart.min.js with its own <script> first. A page with no chart does not load it and is not affected. With the script missing, tsunami.chart() throws an error that says so.

<script src="https://cdn.jsdelivr.net/gh/Djirasak/tsunami-framework@v0.1.3/dist/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/Djirasak/tsunami-framework@v0.1.3/dist/tsunami.min.js"></script>

Attributes

Attribute / element Values Description
.chart The box. It is 16:9 with a minimum height of 10rem. Another ratio is an inline aspect-ratio. The canvas fills it.
canvas The element Chart.js draws in. It is the first argument of tsunami.chart(), as an element or a selector.
aria-label text On the canvas. Names the chart for a screen reader; every canvas needs one.
data-chart-initialized true Set by the script once the chart is drawn.

Options

tsunami.chart(selector | element, options) takes these options.

Option Values Default Description
type bar, line, pie, doughnut, polarArea, radar, any Chart.js type bar The chart type.
labelKey a field of the rows label The field that names a row: a point on the category axis, or a slice.
data array of objects [] One object per point or slice. Its other fields hold the values to draw.
series object every field but the label and color One entry per field to draw, keyed by the field name.
legend true, false false Shows the legend under the chart.
legendAlign start, center, end center Where the legend sits under the chart.
tooltip true, false true Shows the tooltip at the pointer.
options Chart.js options object Merged over the defaults at any depth. Arrays and functions replace.

Series

Each entry of series takes these options. With no series, every field of the rows but the label and color is drawn.

Series option Values Default Description
label text the field name The name in the legend and the tooltip.
color any CSS colour, var(--ok-fg) the palette in turn The palette is --accent, --info-fg, --ok-fg, --warning-fg, --danger-fg. A var() is resolved from the computed style.
type a Chart.js type the chart type Draws this series as another type, for example a line over bars.
axis a scale id, for example y1 Puts the series on a second scale, on the right.
surface true A flat translucent area under a line.
total true Lists the series last in the tooltip, under a line, and not with the others.
dataset Chart.js dataset object Passed to Chart.js, for example { borderDash: [4, 4] }.

Pie, doughnut and polar area charts have one series. Each row is a slice, and a row's own color field colours it.

API

tsunami.chart(selector | element, options) draws the chart and returns the Chart.js instance. Calling it again on the same canvas destroys that chart and draws a new one.

  • tsunami.chart.get(el) returns the Chart.js instance on a canvas, or null.
  • tsunami.chart.destroy(el) destroys the chart and removes data-chart-initialized.

A chart is destroyed by itself when its canvas leaves the page. Colours are read from the CSS variables when a chart is drawn, so on tsunami:themechange every chart is drawn again, with no animation. Under prefers-reduced-motion the animation is off.

Events

Event Detail Description
tsunami:chartready { chart } Bubbles from the canvas every time the chart is drawn, including a redraw and a redraw after a theme change.

Accessibility

A canvas has no text of its own: give every one an aria-label that says what the chart shows, and put the figures that matter in the page as text or a table. The tooltip is drawn on the canvas, so it is for the pointer.