Sparkline
A word-sized graphic of a series of numbers, named by a sentence that gives its range, last value and change.
Example
Signups
Visits
Orders
<div class="stack"> <p> Signups <focsd-sparkline id="spark-line" label="Signups per day" show-last></focsd-sparkline> up this week. </p> <p> Visits <focsd-sparkline id="spark-area" label="Visits per hour" type="area"></focsd-sparkline> </p> <p> Orders <focsd-sparkline id="spark-bar" label="Orders per day" type="bar"></focsd-sparkline> </p></div>
<script type="module">document.getElementById('spark-line').values = [4, 8, 6, 12, 19, 17];document.getElementById('spark-area').values = [30, 42, 38, 55, 61, 49, 70];document.getElementById('spark-bar').values = [5, 9, 3, 12, 8, 14];</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-sparkline label="Signups per day" show-last></focsd-sparkline>import { useEffect, useRef } from 'react';import '@focsd-dev/charts';
export function Example() { const ref = useRef(null); return <focsd-sparkline ref={ref} label="Signups per day" show-last></focsd-sparkline>;}<script setup>import '@focsd-dev/charts';</script>
<template> <focsd-sparkline label="Signups per day" show-last></focsd-sparkline></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/charts';
// template<focsd-sparkline label="Signups per day" show-last></focsd-sparkline><script> import '@focsd-dev/charts';</script>
<focsd-sparkline label="Signups per day" show-last></focsd-sparkline>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
label | label | string | — |
type | type | 'line' | 'area' | 'bar' | line |
show-last | showLast | boolean | false |
min | min | number | undefined | — |
max | max | number | undefined | — |
CSS parts
Style the inside of the component with ::part(), for example focsd-sparkline::part(svg).
| Part | Description |
|---|---|
svg | The drawing. |
line | The line (also the outline of an area). |
area | The filled area under the line. |
bar | Each bar. |
last | The dot on the last value (`show-last`). |
Accessibility
- An image named by its
labeland a generated sentence: "Signups per day: 6 values, from 4 to 19, last 17, up 325% from the first". The words are localized like every other component. - It has no focusable points and is not a tab stop. When the detail matters, use a chart with its data table.
- It is drawn at the size of the text (
--focsd-sparkline-height,--focsd-sparkline-width), in one color. Forced colors use the system text color. - Empty data is named "No data"; non-numbers are skipped.
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.