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.
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. 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.
Nothing to show
Nothing to show
Rendering from data #
One line chart per region, side by side, each with its own accessible name, each as wide as 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 tooltip 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, which is the Frappe Charts options. Attributes prefixed with chart: go to the chart.
Nothing to show
Nothing to show
Nothing to show
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.
Nothing to show
Nothing to show
Nothing to show
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().
Nothing to show
Nothing to show
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.
Nothing to show
Nothing to show
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:
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
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@1by the first chart on the page, once for all of them. Whenwindow.frappe.Chartalready exists, as when the app bundles the library, nothing is requested. data-optionsis merged deeply overoptions: nested objects are combined key by key ({ axisOptions: { xIsSeries: true } }keeps the PHPaxisOptions.xAxisMode), while arrays and other values are taken whole fromdata-options.- A Livewire update that changes
optionsdestroys the chart and draws a new one with them, so a new instance comes with eachrenderedevent;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
colorsand style the axis text with CSS when the page has a dark mode. 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 Frappe Charts 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.
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.
ECharts
the same tag drawn by other libraries, each with its own options format.
Fetchable
loads data from a URL and, with chart="frappe-charts", draws it.