Data
Sparkline
Small chart without axes that shows the shape of a series of numbers.
Example
cr-sparkline draws values (an array of numbers) as a 120 × 32 pixel line by default, to slip into a sentence, a card or a table cell; change its size in CSS (width, height). variant="area" tints the surface under the line. A dot marks the last value.
The line draws itself from left to right on first display. When values changes, the line slides point by point to the new values, even if their count changes.
Monthly sales
'use client';
import { useState } from 'react';
import { CrBox, CrButton, CrSparkline } from '@cristal-ui/react';
const random = () => Array.from({ length: 8 }, () => Math.round(20 + Math.random() * 80));
export default function SparklineBasic() {
const [visits, setVisits] = useState([42, 48, 45, 60, 58, 71, 69, 84]);
return (
<CrBox display="flex" direction="column" gap="lg" align="start">
<p>
Monthly sales <CrSparkline label="Monthly sales" values={[120, 135, 128, 160, 152, 190]} />
</p>
<CrSparkline label="Daily visits" variant="area" values={visits} style={{ width: 240, height: 64 }} />
<CrButton size="s" variant="secondary" onClick={() => setVisits(random())}>
New data
</CrButton>
</CrBox>
);
}values is a property: in HTML, set it from JavaScript (element.values = [1, 2, 3]), not as an attribute.
If the user asked their system to reduce motion, the line shows at once, and jumps straight to new values.
Accessibility
- The sparkline is an image (
role="img") named bylabeland a summary of the values, in the page language: "Monthly sales: last value 190, minimum 120, maximum 190, trending up". The trend compares the last value with the first one. - The name follows the values, but it is not announced by itself: if the update matters, say it in text or in a
role="status"region. - The line has a contrast of at least 3:1 with the background. It only shows a shape: if exact values matter, show them as text or in a table too.
API
Properties
| Property | Type | Default | Description |
|---|---|---|---|
label (obligatoire) | string | — | Ce que mesurent les valeurs (« Ventes de la semaine ») ; début du nom lu par les lecteurs d'écran (obligatoire). |
values | number[] | [] | Les valeurs, dans l'ordre (en JavaScript ou en React : un tableau de nombres). |
variant | "area" | "line" | 'line' | Style : line (ligne seule) ou area (ligne et surface teintée dessous). |
CSS parts
To style it from outside: cr-sparkline::part(name) { … }
| Part | Description |
|---|---|
svg | Le dessin. |
Chart
Columns, bars, lines, areas or donut, readable on hover, with the keyboard and as a table. cr-chart tag, CrChart React component.
Animated number
Number that animates when its value changes, as an odometer, a split-flap board, a blur, a stagger, a decode or a count. Tag cr-number, React component CrNumber.