Charts

Frappe Charts

A chart drawn by Frappe Charts, a small SVG chart library, configured with its own options. It loads the library on demand, redraws when its container is resized and 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. options is the object Frappe Charts takes in new frappe.Chart(element, options), as a PHP array: type, data, height, colors...

Updated by Livewire #

After addMonth() the chart on the page is drawn again with the new options and shows a fourth point.

3 points
Blade

Rendering from data #

One line chart per region, side by side, each with its own accessible name, each as wide as 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 tooltip reads 30 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 Frappe Charts options. Attributes prefixed with chart: go to the chart.

Blade

Chart types and colors #

type picks the kind of chart: bar, line, pie, donut, percentage or heatmap. colors sets one color per dataset or slice.

Blade

Events #

rendered fires on the tag each time the chart is drawn, with the Frappe Charts instance in $event.detail.chart. Here buttons outside add and remove points through its addDataPoint() and removeDataPoint().

Blade

Customization #

Classes go to the chart <div>; loadable: reaches the wrapper and loadable:loading: the spinner. Frappe Charts has no dark theme, so pick colors that read on both.

Blade

Reference

tk:frappe-charts

Prop Type Default Description
options array null The Frappe Charts options: type, data, height, colors, axisOptions...
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:frappe-charts is tk:chart with library="frappe-charts": label, description and every other attribute are passed to it.

Events

Event Detail When
rendered { chart }, the Frappe Charts 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 Frappe Charts 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

The chart takes the width of its <div>; its height comes from options.height, not from CSS.

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.

Behavior notes

  • The library is loaded from https://cdn.jsdelivr.net/npm/frappe-charts@1 by the first chart on the page, once for all of them. When window.frappe.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 ({ axisOptions: { xIsSeries: true } } keeps the PHP axisOptions.xAxisMode), while arrays and other values are taken whole from data-options.
  • A Livewire update that changes options destroys the chart and draws a new one with them, so a new instance comes with each rendered event; getChart(), callable from Alpine expressions on the tag, returns the current one.
  • The chart redraws at the new width when its <div> is resized, by the window or by its container.
  • Frappe Charts has no dark theme: the colors stay the same in dark mode. Set colors and style the axis text with CSS when the page has a dark mode.
  • 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 Frappe Charts 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.