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.
Nothing to show
Nothing to show
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.
Nothing to show
Nothing to show
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.
Nothing to show
Nothing to show
Nothing to show
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.
Nothing to show
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.
Nothing to show
Nothing to show
Nothing to show
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.
Nothing to show
Nothing to show
Nothing to show
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.
Nothing to show
Nothing to show
Customization #
Classes go to the <canvas>; loadable: reaches the wrapper and loadable:loading: the spinner.
Nothing to show
Nothing to show
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:
Nothing to show
Calling a Livewire action:
Nothing to show
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).
Nothing to show
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@4by the first chart on the page, once for all of them. Whenwindow.Chartalready exists, as when the app bundles the library, nothing is requested. data-optionsis merged deeply overoptions: nested objects are combined key by key ({ options: { plugins: { legend: { display: false } } } }keeps the PHPoptions.scales), while arrays and other values are taken whole fromdata-options.- Numbers in ticks and tooltips are formatted in the page's language (the
langof<html>, else the browser's), unless the options setoptions.locale. - A Livewire update that changes
optionsreplacestype,dataandoptionsof the same Chart.js instance and callsupdate(). When the new options carryplugins, the instance is destroyed and a new one is created instead. - The color scheme follows the
darkclass 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 onChart.defaultsis kept. - A color scheme change and an update with
pluginscreate a new instance. Take it from the latestrenderedevent, or callgetChart(), which Alpine expressions written on the tag can use. optionsthat start empty and are filled by a later Livewire update draw the chart at that moment.- With neither
optionsnordata-optionsnothing is drawn, and no error shows. - When the library cannot be loaded, or Chart.js throws while drawing (a
datasetsthat is not a list, for instance), the error message takes the place of the chart andfailedfires. 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 nofailed.
Related
library prop, when it is decided at runtime.
ApexCharts
the same tag drawn by ApexCharts, with its own options format.
ECharts
the same tag for ECharts and Frappe Charts.
Frappe Charts
the same tag for ECharts and Frappe Charts.
Fetchable
loads data from a URL and, with chart="chartjs", draws it.