Skip to component content

Render Bootstrap-token-themed charts with Chart.js-native area, line, bar, pie, doughnut, polar, radar, scatter, and bubble types.

Chart

Basic

A token-themed area chart initializes from public .chart markup and serialized JSON.

<div class="chart w-100" id="chart-area-default" data-chart="area" data-chart-data='{"datasets": [{"data": [186, 305, 237, 273, 209, 214], "label": "Desktop"}, {"data": [80, 200, 120, 190, 130, 140], "label": "Mobile"}], "labels": ["Apr", "May", "Jun", "Jul", "Aug", "Sep"]}' style="height: 17.5rem">
  <canvas role="img" aria-label="Area chart comparing desktop and mobile visitors over six months"></canvas>
</div>

Usage

Render a .chart root with one direct child <canvas>. The data-chart attribute accepts area, line, bar, pie, doughnut, polarArea, radar, scatter, and bubble, while data-chart-data carries the serialized {labels, datasets} data.

Use data-chart-options for JSON-object Chart.js options such as stacked scales, interaction mode, cutout, rotation, or legend display. Functions and callbacks are not serializable in HTML; pass them through the programmatic getOrCreateInstance config instead.

Give the root an explicit height. Radial Charts are represented by Chart.js doughnut and polarArea, not by a separate public radial type.

HTML Anatomy

These are the public HTML hooks that applications can inspect or target. They describe rendered markup, not the repository's internal Jinja build helpers.

Selector Purpose
Root
.chart Public root element. It owns the Chart.js instance and the theme observer.
id Required stable identifier for the chart root and its server-rendered contract.
Data Contract
[data-chart] Chart type and initializer attribute. Supported values are area, line, bar, pie, doughnut, polarArea, radar, scatter, and bubble.
[data-chart-data] Serialized {labels, datasets} JSON. Scatter and bubble data keep labels: [] with object points inside datasets.
[data-chart-options] Optional JSON-object Chart.js options pass-through. Unsafe prototype keys are ignored and programmatic config wins.
Render Target
.chart > canvas The first child canvas. It is resolved deterministically and is required.
<div class="chart" id="chart-html-example" data-chart="line" data-chart-data='{"datasets": [{"data": [4000, 3200, 4800], "label": "Revenue"}], "labels": ["Jan", "Feb", "Mar"]}' data-chart-options='{"plugins": {"legend": {"display": false}}}' style="height: 17.5rem">
  <canvas role="img" aria-label="Monthly revenue line chart"></canvas>
</div>

JavaScript

Chart.js is bundled into the published chart.js entrypoint through chart.js/auto; the wrapper never reads a browser global Chart object and never loads a runtime CDN script. Importing the module never scans the document. Initialize only a .chart root your application owns: repeated initialization returns the same instance, and dispose() destroys the Chart.js instance, disconnects the theme observer, cancels pending work, and releases the instance registration.

Initialize a chart

Use this when a page already renders a .chart root and needs the wrapper to attach Chart.js.

import MooChart from "@wpmoo/ui/chart.js";

const element = document.querySelector(".chart");

if (element) {
  const chart = MooChart.getOrCreateInstance(element);

  // getOrCreateInstance is idempotent: the same instance is returned.
  const again = MooChart.getOrCreateInstance(element);
  console.assert(chart === again);

  // Later, release Chart.js and the theme observer.
  chart.dispose();
}

Customize tooltips

Use this for non-serializable Chart.js options, such as tooltip callback functions.

import MooChart from "@wpmoo/ui/chart.js";

MooChart.getOrCreateInstance(document.querySelector("#chart-tooltip-custom"), {
  options: {
    plugins: {
      tooltip: {
        callbacks: {
          label(context) {
            return `${context.dataset.label}: ${context.formattedValue}`;
          },
        },
      },
    },
  },
});

Theming

Chart colors are derived from --moo-chart-1 through --moo-chart-5, falling back to Bootstrap's semantic --bs-* tokens at runtime. Cartesian, radar, scatter, and bubble charts assign that palette by dataset; pie, doughnut, and polar area charts cycle it by slice. Explicit Chart.js dataset color fields are preserved, while unset color fields continue to follow the active palette. A MutationObserver watches the nearest data-bs-theme scope, falling back to the document element; when that scope or its inline chart-token styles change, each chart re-reads the tokens and re-themes its datasets, grid lines, legend, and tooltip without a reload. Legend and tooltip markers use matching compact circular swatches with the same border color and text offset, while thin line, area, and radar strokes keep point markers readable. Line chart points use their series color for the point fill, border, and hover state. Charts also reflow on resize: they are responsive and drop aspect-ratio locking, so the same markup fills a desktop card or a narrow mobile viewport without layout-specific switches.

Accessibility

The canvas is a raster of pixels, so give it a text alternative: set role="img" and a descriptive aria-label on the <canvas>, or provide an adjacent data table for the same series. Keep legends visible rather than encoding meaning in color alone, and preserve the default tooltip so individual values remain reachable on touch and with a pointer.

Component reference

Status
Accepted
Runtime
optional Moo UI ESM
Markup
Moo UI documented extension
Reference

Rendering and the chart API build on Chart.js; see the Chart.js documentation.

For the color tokens the palette reads, see the Bootstrap CSS variables documentation.

Settings

Theme
Choose how Moo UI looks on this device.
Sidebar
Choose how the sidebar sits on the page.
Theme Builder
Tune catalog tokens and chart colors for this browser.
Base color
Theme color
Chart color
Heading font
Body font
Radius
Direction
Choose the reading direction. RTL flips the layout live.
Reset

Restore the default appearance.