Charts

Chart.js

A chart drawn by Chart.js on a <canvas>, configured with the library's own options. It loads the library on demand, follows the light and dark color scheme and redraws when Livewire sends new options.

Blade

A spinner shows while the library loads, then a bar chart with three bars takes its place. options is the configuration Chart.js takes in new Chart(canvas, config), as a PHP array: type, data and options.

Updated by Livewire #

After addMonth() the chart already on the page is updated with the new options and shows a fourth bar.

3 bars
Blade

Rendering from data #

One line chart per region, side by side, each with its own accessible name. Chart.js sizes each canvas to the width of its grid cell.

Blade

Options written in JavaScript #

data-options is a JavaScript object merged over options, for what a PHP array cannot carry, such as callbacks. Here the labels of the vertical axis read 0 un, 5 un... It also works alone, with no options at all.

Blade

Options from an endpoint #

tk:fetchable requests the URL and draws the JSON it answers, which is the Chart.js configuration. Attributes prefixed with chart: go to the chart.

Blade

Chart types #

type picks the kind of chart: bar, line, pie, doughnut, radar, polarArea... Each dataset is one line or set of bars on the same axes.

Blade

Events #

rendered fires on the tag each time the chart is drawn, with the Chart.js instance in $event.detail.chart. Here buttons outside hide and show a dataset through it.

Blade

Customization #

Classes go to the <canvas>; loadable: reaches the wrapper and loadable:loading: the spinner.

Blade

Reference

tk:chartjs

Prop Type Default Description
options array null The Chart.js configuration: type, data, options, plugins.
label string Chart The accessible name of the chart, translated.
description string | slot null A text alternative read by screen readers, linked with aria-describedby: what the chart shows ("Sales grew 20% in March").

data-options is an attribute, not a prop: a JavaScript expression that returns an object of options.

tk:chartjs is tk:chart with library="chartjs": label, description and every other attribute are passed to it.

Events

Event Detail When
rendered { chart }, the Chart.js instance The chart was drawn: the first time, on each update of the options and when the color scheme changes.
started The library started loading. Not fired with loadable:silent.
completed The library is loaded; the chart is drawn right after.
failed The library could not be loaded, or Chart.js threw while drawing.

Prefixes

Prefix Goes to
loadable: The wrapper around the chart, the spinner and the error. loadable:silent hides the spinner.
loadable:loading: The spinner shown while the library loads.
loadable:error: The error message.

Events

rendered is dispatched on the <canvas>, the element the tag's attributes go to, and does not bubble: listen on the tag itself. started, completed and failed are dispatched on the wrapper around it and do not bubble either, so they are listened to with the loadable: prefix.

With Alpine:

blade

Calling a Livewire action:

blade

Customization

Classes and attributes on the tag go to the <canvas>. To reach the elements around it, prefix the attribute (listed in the Reference above).

blade

Chart.js sizes the canvas to the width of its container and takes the height from the chart's aspect ratio (options.aspectRatio), so the width of the container is what sets the size.

Accessibility

The <canvas> has role="figure" and an aria-label: the label prop, or "Chart" translated. Give each chart a label that says what it shows and a description with its main takeaway. The spinner and the error message are role="status" regions. Keyboard interaction inside the chart is the library's own.

Behavior notes

  • The library is loaded from https://cdn.jsdelivr.net/npm/chart.js@4 by the first chart on the page, once for all of them. When window.Chart already exists, as when the app bundles the library, nothing is requested.
  • data-options is merged deeply over options: nested objects are combined key by key ({ options: { plugins: { legend: { display: false } } } } keeps the PHP options.scales), while arrays and other values are taken whole from data-options.
  • Numbers in ticks and tooltips are formatted in the page's language (the lang of <html>, else the browser's), unless the options set options.locale.
  • A Livewire update that changes options replaces type, data and options of the same Chart.js instance and calls update(). When the new options carry plugins, the instance is destroyed and a new one is created instead.
  • The color scheme follows the dark class of <html>. When the class changes, the global text and grid colors (Chart.defaults.color, Chart.defaults.scale.grid.color) switch to light or dark, and the chart is destroyed and created again with them. They switch only while they still hold Chart.js's own defaults, so a color the app set on Chart.defaults is kept.
  • A color scheme change and an update with plugins create a new instance. Take it from the latest rendered event, or call getChart(), which Alpine expressions written on the tag can use.
  • options that start empty and are filled by a later Livewire update draw the chart at that moment.
  • With neither options nor data-options nothing is drawn, and no error shows.
  • When the library cannot be loaded, or Chart.js throws while drawing (a datasets that is not a list, for instance), the error message takes the place of the chart and failed fires. A later Livewire update with valid options does not draw it again: the page has to be reloaded.
  • Options Chart.js accepts without throwing but cannot draw, such as an unknown type, leave the chart blank, with no message and no failed.