Charts
ECharts
A chart drawn by Apache ECharts, configured with the library's own options. It loads the library on demand, follows the light and dark color scheme, resizes with its container 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, as wide as its container and 25rem tall. options is the object ECharts takes in chart.setOption(option), as a PHP array.
Updated by Livewire #
After addMonth() the chart already on the page animates to 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 16rem tall and with its own accessible name. Each chart fills the width of its grid cell and redraws when the cell changes size.
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, 10 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 ECharts option object. Attributes prefixed with chart: go to the chart.
Nothing to show
Chart types #
Each series has its own type: line, bar, pie, scatter, gauge... A height class on the tag replaces the default height.
Nothing to show
Nothing to show
Nothing to show
Events #
rendered fires on the tag when the chart is drawn, with the ECharts instance in $event.detail.chart; its on() listens to the library's events. Here a click on a bar calls a Livewire action.
Nothing to show
Nothing to show
Customization #
Classes go to the chart <div>, its height included; loadable: reaches the wrapper and loadable:loading: the spinner.
Nothing to show
Nothing to show
Reference
tk:echarts
| Prop | Type | Default | Description |
|---|---|---|---|
options |
array | null |
The ECharts option object: xAxis, yAxis, series, tooltip, legend... |
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:echarts is tk:chart with library="echarts": label, description and every other attribute are passed to it.
Events
| Event | Detail | When |
|---|---|---|
rendered |
{ chart }, the ECharts instance |
The chart was drawn: the first time and on each update of the options. |
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 ECharts 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 <div> 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 <div>. To reach the elements around it, prefix the attribute (listed in the Reference above).
Nothing to show
ECharts draws into the size of its <div>, which is w-full h-100 by default. A width or height class on the tag replaces the default one.
Accessibility
The chart <div> 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/echarts@6by the first chart on the page, once for all of them. Whenwindow.echartsalready exists, as when the app bundles the library, nothing is requested. data-optionsis merged deeply overoptions: nested objects are combined key by key ({ tooltip: { trigger: 'axis' } }keeps the PHPtooltip.backgroundColor), while arrays and other values are taken whole fromdata-options.- A Livewire update that changes
optionsis passed tosetOption()of the same instance, which merges it into the current chart. When the new options haveseries, the series are replaced instead of merged, so a series removed on the server disappears from the chart. A top-level key the new options no longer have (atitle,dataZoom) is removed from the chart. - The color scheme follows the
darkclass of<html>. In dark mode the chart uses ECharts'sdarktheme with a transparent background, so the page shows through. When the class changes, the same instance switches theme withsetTheme()and the latest options are applied again; the legend selection and the zoom go back to what the options say, andrendereddoes not fire. - The instance is created once;
getChart(), callable from Alpine expressions written on the tag, returns it. - The chart redraws at the new size when its
<div>is resized, by the window or by its container. 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 ECharts throws while drawing, the error message takes the place of the chart and
failedfires. A later Livewire update with valid options does not draw it again: the page has to be reloaded. - Options ECharts accepts without throwing but cannot draw, such as an unknown series
type, leave the chart blank, with no message and nofailed.
Related
library prop, when it is decided at runtime.
Chart.js
the same tag drawn by other libraries, each with its own options format.
ApexCharts
the same tag drawn by other libraries, each with its own options format.
Frappe Charts
the same tag for Frappe Charts.
Fetchable
loads data from a URL and, with chart="echarts", draws it.