SVG chart component with progressive enhancement: semantic table → CSS chart → SVG chart. Supports bar, column, line, area, pie, ring, scatter, and bubble types.
Overview
An SVG chart web component with built-in charting engine. Supports eight chart types with five data source options. Progressive enhancement means a semantic <table> is visible without JavaScript — with JS, the SVG chart renders and the table becomes the screen-reader (and print) version. Every chart has a data table: data supplied as JSON gets one generated. See Accessibility.
Chart Types
Set data-type to one of: bar, column, line, area, pie, ring, scatter, or bubble.
Line Chart
123456789101112
<chart-wc data-type="line" data-title="Weekly Users" data-label-x="Day of Week" data-label-y="Users" data-legend data-tooltip data-values='[ {"name": "Desktop", "values": {"Mon": 120, "Tue": 180, "Wed": 150, "Thu": 210, "Fri": 190, "Sat": 80, "Sun": 60}}, {"name": "Mobile", "values": {"Mon": 80, "Tue": 120, "Wed": 160, "Thu": 140, "Fri": 200, "Sat": 220, "Sun": 180}} ]'></chart-wc>
Pie Chart
Pie charts accept a flat object (label → value) as data.
123456
<chart-wc data-type="pie" data-title="Market Share" data-legend data-values='{"Chrome": 65, "Safari": 18, "Firefox": 8, "Edge": 5, "Other": 4}'></chart-wc>
Ring Chart
A ring (donut) chart is a pie with a transparent center hole. Same data shape as pie: a flat object of label → value. Slices below an angular threshold render with external leader-line labels; the percentage labels inside the slices are suppressed in ring mode to keep the chart readable at small sizes.
1234567
<chart-wc data-type="ring" data-title="Storage Used" data-legend data-tooltip data-values='{"Documents": 42, "Photos": 28, "Music": 15, "Apps": 10, "Free": 5}'></chart-wc>
Tunables specific to ring (and pie) charts — pass via data-config:
| Path | Default | Purpose |
center.size | "40%" | Hole diameter as a percent of the chart's viewBox. Larger values produce a thinner ring. |
center.style.fill | var(--color-surface, #fff) | Hole fill. Follows the active VB theme by default; override with a custom --color-surface or an explicit fill value. |
plot.node.label.placement | "auto" | auto uses external labels for small slices and hides internal labels in ring mode. Force with "outside". |
plot.node.label.threshold | 12 | Slice angle (degrees) below which auto-placement switches to an external leader-line label. |
1234567
<chart-wc data-type="ring" data-title="Storage" data-legend data-config='{"center":{"size":"55%","style":{"fill":"transparent"}}}' data-values='{"Used": 78, "Free": 22}'></chart-wc>
Every chart except a sparkline has a legend and hover tooltips unless you turn them off. data-legend="false" hides the legend, which a single-series chart rarely needs, and data-tooltip="false" turns the tooltips off. The bare attribute or ="true" states the default. On a table source the same attributes can go on the <table>. An attribute on <chart-wc> wins over data-config and over the table.
123
<chart-wc data-type="column" data-title="Page views by day" data-legend="false" data-values='[{"name": "Views", "values": {"Mon": 4400, "Tue": 5500, "Wed": 4900}}]'></chart-wc>
The legend sits beside the plot. When the chart is 400px wide or less it moves under the plot as a wrapping row, so the plot keeps the full width.
Sparkline mode
Add data-size="sparkline" to render a minimal trend chart with no axes, gridlines, tick labels, axis labels, title, legend, or tooltip. The chart becomes a pure inline shape sized by its container — ideal for dashboard tiles, data tables, and the sparkline slot of <score-card>.
Sparkline mode is a deterministic opt-in: shrinking the container alone won't strip chrome. Add the attribute when you want a sparkline, omit it for the full chart.
1234567
<chart-wc class="revenue-trend" data-type="line" data-size="sparkline" data-title="Revenue, last 7 days" data-values='[{"name":"Revenue","values":{"Mon":120,"Tue":135,"Wed":128,"Thu":150,"Fri":162,"Sat":174,"Sun":190}}]'></chart-wc>
1
.revenue-trend { block-size: 2.5rem; }
A sparkline gets a generated data table like any other chart; data-title becomes its caption, so screen-reader users hear "Revenue, last 7 days" plus the values.
The line, area, and column/bar chart types are the recommended sparkline shapes. pie and ring still render but ignore the chrome-strip (their chrome is the chart). Pair with a CSS custom property override on --chart-series-1 to colour the trend by tone:
123
chart-wc[data-size="sparkline"].trend-up { --chart-series-1: var(--color-success); }chart-wc[data-size="sparkline"].trend-down { --chart-series-1: var(--color-error); }chart-wc[data-size="sparkline"].trend-flat { --chart-series-1: var(--color-text-muted); }
Data Sources
The component resolves data using this priority chain:
| Priority | Source | Description |
| 1 | JS property (.data) | Set data programmatically |
| 2 | data-values | Inline JSON attribute |
| 3 | <script type="application/json"> | JSON script block child |
| 4 | <template data-chart-data> | Template child with JSON |
| 5 | <table> | Child table (progressive enhancement) |
Table Source (Progressive Enhancement)
Wrap a semantic <table> inside <chart-wc>. The chart extracts data from the table and renders SVG. Without JavaScript, the table remains visible. The class="vb-chart" CSS-only chart styling (the no-JS layer) comes from the charts CSS bundle, vanilla-breeze-charts.css — it is not part of the core stylesheet.
1234567891011121314
<chart-wc> <table class="vb-chart" data-type="column" data-chart="replace" data-tooltip> <caption>Quarterly Results</caption> <thead> <tr><th></th><th>Revenue</th><th>Expenses</th><th>Profit</th></tr> </thead> <tbody> <tr><th>Q1</th><td>45</td><td>32</td><td>13</td></tr> <tr><th>Q2</th><td>52</td><td>35</td><td>17</td></tr> <tr><th>Q3</th><td>48</td><td>30</td><td>18</td></tr> <tr><th>Q4</th><td>61</td><td>38</td><td>23</td></tr> </tbody> </table></chart-wc>
Script Block
12345678
<chart-wc data-type="area" data-title="Traffic Sources" data-legend data-tooltip> <script type="application/json"> [ {"name": "Organic", "values": {"Jan": 3200, "Feb": 4100, "Mar": 3800, "Apr": 5200}}, {"name": "Referral", "values": {"Jan": 1200, "Feb": 1400, "Mar": 1100, "Apr": 1800}} ] </script></chart-wc>
Template
12345
<chart-wc data-type="bar" data-title="Team Velocity" data-tooltip> <template data-chart-data> [{"name": "Story Points", "values": {"Sprint 1": 21, "Sprint 2": 34, "Sprint 3": 29, "Sprint 4": 42}}] </template></chart-wc>
Table Column Control
Use data-chart-label, data-chart-series, and data-chart-ignore on <th> elements to control how table columns are extracted.
123456789101112131415161718
<chart-wc> <table class="vb-chart" data-type="column" data-chart="replace" data-tooltip> <caption>Product Sales</caption> <thead> <tr> <th data-chart-label>Product</th> <th data-chart-ignore>ID</th> <th>Units Sold</th> <th>Revenue</th> </tr> </thead> <tbody> <tr><td>Alpha</td><td>P001</td><td>120</td><td>3600</td></tr> <tr><td>Beta</td><td>P002</td><td>85</td><td>4250</td></tr> <tr><td>Gamma</td><td>P003</td><td>200</td><td>6000</td></tr> </tbody> </table></chart-wc>
Attributes
| Attribute | Values | Default | Description |
|---|
data-type | "bar", "column", "line", "area", "pie", "ring", "scatter", "bubble" | — | Chart type to render |
data-values | string | — | Chart data as JSON string |
data-config | string | — | Chart config overrides as JSON string |
data-title | string | — | Chart title text; also the caption of the generated data table |
data-subtitle | string | — | Subtitle shown under data-title (requires data-title) |
data-label-x | string | — | X / category axis label; also the category column header of the generated data table |
data-label-y | string | — | Y / value axis label |
data-legend | "", "true", "false" | true | Series legend with show/hide checkboxes. On by default; "false" hides it (bare or "true" keeps it on). Wins over data-config. Moves under the plot when the chart is 400px wide or less. |
data-tooltip | "", "true", "false" | true | Hover tooltips. On by default; "false" turns them off (bare or "true" keeps them on). Wins over data-config. |
data-palette | string | — | Custom color palette as JSON array |
data-chart | "replace", "enhance" | — | How to handle an authored source table: replace (default) visually hides it after the chart mounts but keeps it in the accessibility tree; enhance keeps it visible. Also readable from the table's own data-chart. |
data-size | "sparkline" | — | Render mode. sparkline strips all chrome (axes, ticks, gridlines, labels, title, legend, tooltip) for use as an inline trend chart sized by its container. |
Structure
| Element | Required | Description |
|---|
<table> | no | Optional source table — data is extracted; the table stays the screen-reader (and print) version of the chart. Without one, chart-wc generates a table[data-chart-generated] from the data. |
<script type='application/json'> | no | Optional inline JSON data source |
<template data-chart-data> | no | Optional template-based JSON data source |
Events
| Event | Detail | Description |
|---|
chart-wc:render | { type, seriesCount } | Fired after SVG chart renders successfully |
chart-wc:error | { message } | Fired when no data is found or chart rendering fails (the data table is then left visible) |
chart-wc:config-resolved | { type, config } | Fired before each render with the merged engine config (mutable, for last-minute tweaks) |
chart-wc:data-changed | { data, config, source: 'property' } | Fired when .data or .config is assigned |
chart-wc:upgraded | — | Fired once after the component upgrades; safe signal for the first .data assignment |
1234567891011121314
const chart = document.querySelector('chart-wc'); chart.addEventListener('chart-wc:render', (e) => { console.log('Rendered:', e.detail.type, e.detail.seriesCount, 'series');}); chart.addEventListener('chart-wc:error', (e) => { console.error('Chart error:', e.detail.message);}); chart.addEventListener('chart-wc:config-resolved', (e) => { e.detail.config.legend = { enabled: false };});
Accessibility
The rendered SVG is decorative to assistive technology (aria-hidden="true"); the chart's text alternative is always a real data table:
- Table source: once the chart has mounted, the authored table gets
class="visually-hidden". It stays in the accessibility tree — caption, headers and cells are all announced. With data-chart="enhance" it stays visible.
- JSON sources (
.data, data-values, <script>, <template>): chart-wc generates a visually hidden <table data-chart-generated> from the data and keeps it in sync on every render. Its caption is data-title (plus data-subtitle); data-label-x / data-label-y name the category and value columns. Pie/ring data becomes label/value rows, scatter/bubble data one row per point.
- If the data can't be tabulated, the SVG is exposed instead as
role="img" named by data-title.
- Keyboard: the engine's own keyboard navigation (arrow keys between data points when tooltips are on, focusable pie slices) lives inside the
aria-hidden SVG, so chart-wc removes those elements from the tab order (tabindex="-1") — a focusable element inside aria-hidden would be a silent tab stop for screen-reader users. Mouse tooltips still work; keyboard and screen-reader users get the same values from the table. Use data-chart="enhance" when sighted keyboard users need the numbers on screen.
- Failure: the table is only hidden after the chart mounts successfully. If rendering throws,
chart-wc:error fires and the table stays visible.
- Print: the data table prints and the SVG is hidden (sparklines are the exception: they print the trend shape).
The table-hiding and print rules live in the charts CSS bundle. Without vanilla-breeze-charts.css a long hidden table can still extend the page's scroll height.
JavaScript API
| Property / Method | Type | Description |
.data | Array|Object | Get/set chart data. Triggers re-render on set. |
.config | Object | Get/set chart config overrides. Triggers re-render on set. |
.refresh() | void | Re-extract table data and re-render the chart. |
.toSVG() | string|null | Get the current SVG markup for export. |
12345678910111213141516
const chart = document.querySelector('chart-wc'); chart.data = [ {name: '2024', values: {Q1: 120, Q2: 180, Q3: 150, Q4: 210}}, {name: '2025', values: {Q1: 140, Q2: 200, Q3: 170, Q4: 240}},]; chart.config = { legend: { enabled: false } }; chart.refresh(); const svg = chart.toSVG();
Reactive updates (JS-first)
For frameworks pushing live data, wait for chart-wc:upgraded before the first assignment, then react to changes via chart-wc:data-changed. The source field lets consumers ignore echoes from their own assignments.
1234567891011121314151617
const chart = document.querySelector('chart-wc'); await new Promise(r => chart.addEventListener('chart-wc:upgraded', r, { once: true })); chart.data = visibleSeries.get(); effect(() => { chart.data = visibleSeries.get(); }); chart.addEventListener('chart-wc:data-changed', (e) => { if (e.detail.source !== 'property') { syncStore(e.detail.data); }});
See the Data API concepts guide for the dual HTML-first / JS-first contract that <chart-wc> shares with <kanban-board>, <work-item>, and <user-story>.
| Event | Detail | When |
chart-wc:upgraded | — | After each connect (again after the element is moved); safe signal for the first .data assignment. |
chart-wc:data-changed | { data, config, source: 'property' } | Fires when .data or .config is assigned via the property API. |
Theme Integration
Charts read VB design tokens via CSS custom properties. The theme bridge converts these into palette and axis configuration automatically. Each chart property falls back to the theme token beside it, so charts follow light and dark mode without any set-up; set a property on <chart-wc> or an ancestor to override it.
| Custom Property | Falls back to | Purpose |
--chart-series-1 … --chart-series-6 | --color-primary, --color-accent, … | Chart color palette per series |
--chart-title-color | --color-text | Title color |
--chart-label-color | --color-text-muted | Axis, legend and tooltip label color; subtitle color unless --chart-subtitle-color is set |
--chart-axis-color | --color-border-strong | Axis line color |
--chart-grid-color | --color-border | Grid line color |
--chart-tick-color | --color-border | Tick mark color |
--chart-baseline-color | --color-text-muted | Zero line color when the data goes negative |
Progressive Enhancement
| Layer | Environment | Experience |
| 1 | No CSS, no JS | Plain semantic table |
| 2 | CSS only | CSS chart via .vb-chart class (needs vanilla-breeze-charts.css) |
| 3 | CSS + JS | Full SVG chart with interactivity |
Loading
<chart-wc> is part of the optional charts bundle (main-charts.js), not the core bundle. Include both its script and its stylesheet — the CSS carries the .vb-chart CSS-only charts, the chart sizing, and the accessible table-hiding and print rules:
12
<link rel="stylesheet" href="/cdn/vanilla-breeze-charts.css"><script type="module" src="/cdn/vanilla-breeze-charts.js"></script>