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.

Blade

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.

3 bars
Blade

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.

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, 10 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 ECharts option object. Attributes prefixed with chart: go to the chart.

Blade

Chart types #

Each series has its own type: line, bar, pie, scatter, gauge... A height class on the tag replaces the default height.

Blade

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.

Click a bar. Selected: —
Blade

Customization #

Classes go to the chart <div>, its height included; loadable: reaches the wrapper and loadable:loading: the spinner.

Blade

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:

blade

Calling a Livewire action:

blade

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).

blade

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@6 by the first chart on the page, once for all of them. When window.echarts 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 ({ tooltip: { trigger: 'axis' } } keeps the PHP tooltip.backgroundColor), while arrays and other values are taken whole from data-options.
  • A Livewire update that changes options is passed to setOption() of the same instance, which merges it into the current chart. When the new options have series, 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 (a title, dataZoom) is removed from the chart.
  • The color scheme follows the dark class of <html>. In dark mode the chart uses ECharts's dark theme with a transparent background, so the page shows through. When the class changes, the same instance switches theme with setTheme() and the latest options are applied again; the legend selection and the zoom go back to what the options say, and rendered does 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.
  • 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 ECharts throws while drawing, 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 ECharts accepts without throwing but cannot draw, such as an unknown series type, leave the chart blank, with no message and no failed.