Chart
Bar, line, area, stacked, scatter and donut charts whose data is also a summary, a table and keyboard-explorable points.
Example
<div class="full"> <focsd-chart id="chart-demo" label="Revenue by quarter" x="quarter"></focsd-chart></div>
<script type="module">const chart = document.getElementById('chart-demo');chart.series = [ { key: 'online', label: 'Online' }, { key: 'stores', label: 'Stores' },];chart.format = { style: 'currency', currency: 'USD', maximumFractionDigits: 0 };chart.data = [ { quarter: 'Q1', online: 1200, stores: 900 }, { quarter: 'Q2', online: 1500, stores: 950 }, { quarter: 'Q3', online: 1700, stores: 1100 }, { quarter: 'Q4', online: 2100, stores: 1300 },];</script>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/charts';</script>
<focsd-chart label="Revenue by quarter" x="quarter"></focsd-chart>import { useEffect, useRef } from 'react';import '@focsd-dev/charts';
export function Example() { const ref = useRef(null); return <focsd-chart ref={ref} label="Revenue by quarter" x="quarter"></focsd-chart>;}<script setup>import '@focsd-dev/charts';</script>
<template> <focsd-chart label="Revenue by quarter" x="quarter"></focsd-chart></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/charts';
// template<focsd-chart label="Revenue by quarter" x="quarter"></focsd-chart><script> import '@focsd-dev/charts';</script>
<focsd-chart label="Revenue by quarter" x="quarter"></focsd-chart>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
label | label | string | — |
description | description | string | — |
type | type | 'bar' | 'line' | 'area' | 'scatter' | 'donut' | bar |
x | x | ChartKey | label |
y | y | ChartKey | — |
stacked | stacked | boolean | false |
mirror | mirror | boolean | false |
height | height | number | DEFAULT_HEIGHT |
show-table | showTable | boolean | false |
CSS parts
Style the inside of the component with ::part(), for example focsd-chart::part(figure).
| Part | Description |
|---|---|
figure | The figure. |
caption | The caption (the chart's label). |
chart | The drawing area. |
point | Each data point (bar, marker or slice); `point-active` when focused. |
tooltip | The value shown for the focused or hovered point. |
legend | The legend list. |
table-toggle | The "Show data table" button. |
table | The data table. |
Accessibility
- A figure named by its caption and described by a generated summary ("Bar chart, 4 categories, 2 series. Highest: Online, Q4, $2,100."), or your own
description. - Data points are one tab stop: arrow keys move between categories (left and right) and series (up and down), Home and End jump to the ends, and each point is named "Series, category: value".
- The Show data table button reveals the same numbers as a real table with row and column headers.
- Series differ by pattern and marker shape as well as color, so the chart works in grayscale and for color-blind readers.
- Values are formatted with
Intl.NumberFormat(formatproperty) in the page language. - Set
typetoareaorscatter, addstackedto bars or areas, andmirrorto flip the horizontal direction. See the charts guide for each type.
Keyboard
| Key | Action |
|---|---|
| Tab | Move to the chart, then to the table button |
| Arrow Left / Right | Previous / next category |
| Arrow Up / Down | Previous / next series |
| Home / End | First / last category in the series |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.