Skip to Content
Cristal

Data

Sparkline

Small chart without axes that shows the shape of a series of numbers.

<cr-sparkline>CrSparkline

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

New data
'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 by label and 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

PropertyTypeDefaultDescription
label (obligatoire)string—Ce que mesurent les valeurs (« Ventes de la semaine ») ; début du nom lu par les lecteurs d'écran (obligatoire).
valuesnumber[][]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) { … }

PartDescription
svgLe dessin.

On this page