Charts
Chart
A chart drawn by one of four JavaScript libraries, chosen with library and configured with that library's own options. It loads the library on demand and redraws when Livewire sends new options. tk:apexcharts, tk:chartjs, tk:echarts and tk:frappe-charts are this same tag with library already set; use tk:chart when the library is decided at runtime.
Nothing to show
Nothing to show
A spinner shows while Chart.js loads, then a bar chart with three bars takes its place. options is the configuration of the chosen library, as a PHP array: here the one Chart.js takes in new Chart(canvas, config).
Choosing the library #
library takes apexcharts, chartjs, echarts or frappe-charts, and options must be in that library's format: the same array does not draw the same chart in another library. <tk:echarts :options="$options" /> renders exactly the same as this example.
Nothing to show
Nothing to show
Updated by Livewire #
After addMonth() the chart already on the page is updated with the new options and shows a fourth bar. The same holds for every library.
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 formatter functions. Here the vertical axis reads 30 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 with tk:chart, so the JSON is the configuration of the library named in chart. Attributes prefixed with chart: go to the chart.
Nothing to show
Nothing to show
Nothing to show
Switching the library #
Each library has its own options, so switching the library also switches the options. A wire:key per library draws a new chart when it changes.
Nothing to show
Nothing to show
Label and description #
label names the chart for screen readers and description gives its main takeaway, linked with aria-describedby.
Nothing to show
Europe brings almost half of the sales.
Nothing to show
Events #
rendered fires on the tag each time the chart is drawn, with the instance in $event.detail.chart. loadable:x-on:completed fires when the library is loaded.
Nothing to show
Nothing to show
Customization #
Classes go to the chart element; loadable: reaches the wrapper and loadable:loading: the spinner. The ECharts element takes its height from a class.
Nothing to show
Nothing to show
Reference
tk:chart
| Prop | Type | Default | Description |
|---|---|---|---|
library |
string | null |
apexcharts, chartjs, echarts or frappe-charts. Required: without it, or with another value, nothing is drawn. |
options |
array | null |
The configuration of the chosen library. |
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:apexcharts, tk:chartjs, tk:echarts and tk:frappe-charts take options and pass everything else, label and the prefixed attributes included, to tk:chart.
Events
| Event | Detail | When |
|---|---|---|
rendered |
{ chart }, the library's chart instance |
The chart was drawn: the first time and on each update of the options. ApexCharts and Chart.js also fire it 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 the library 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 chart element, the one 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 chart element: a <canvas> for Chart.js, a <div> for the others. To reach the elements around it, prefix the attribute (listed in the Reference above).
Nothing to show
The size comes from each library: ApexCharts and Frappe Charts take it from the options (chart.height, height), Chart.js sizes the canvas to its container, and the ECharts element defaults to full width and h-100, which a height class on the tag replaces, as in this example.
Accessibility
The chart element 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 canvas itself is invisible to screen readers, so put a table next to it when the exact values matter. The spinner and the error message are role="status" regions. Keyboard interaction inside the chart is the library's own.
Behavior notes
- Each library is loaded from jsDelivr by the first chart that uses it, once for all of them:
apexcharts@5,chart.js@4,echarts@6andfrappe-charts@1. When the library is already onwindow(ApexCharts,Chart,echarts,frappe.Chart), as when the app bundles it, nothing is requested. data-optionsis merged deeply overoptions: nested objects are combined key by key (with Chart.js,{ options: { plugins: { legend: { display: false } } } }keeps the PHPoptions.scales), while arrays and other values are taken whole fromdata-options.- Updates from Livewire are applied differently by each library. ApexCharts merges the new options into the same instance, and creates a new one when a top-level key of the previous options is missing, so an option the server no longer sends (a
title,annotations) disappears. Chart.js replacestype,dataandoptionsof the same instance, and creates a new one when the new options carryplugins. ECharts merges them with its ownsetOption, replacing the wholeserieswhenseriesis present and removing the components of a top-level key the new options no longer have. Frappe Charts draws a new chart from the new options alone. - The color scheme follows the
darkclass of<html>and is applied again when the class changes. ApexCharts draws in the matching mode with a transparent background, unless the options settheme.mode. ECharts uses its dark theme with a transparent background. Chart.js switches the global text and grid colors, only while they still hold its own defaults, so a color the app set onChart.defaultsis kept. Frappe Charts does not follow the color scheme. - ECharts and Frappe Charts redraw when the chart element changes size.
- A color scheme change and some updates create a new chart instance. Take it from the latest
renderedevent, 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. - If the library cannot be loaded, the error message takes the place of the chart. A later Livewire update with valid options does not draw it again: the page has to be reloaded. Options a library accepts without throwing but cannot draw leave the chart blank, with no message.
Related
tk:chart with the library fixed, when it is known in advance.
Chart.js
tk:chart with the library fixed, when it is known in advance.
ECharts
tk:chart with the library fixed, when it is known in advance.
Frappe Charts
tk:chart with the library fixed, when it is known in advance.
Fetchable
loads data from a URL and, with chart, draws it.