Synertech Web/ui v0.57.5 Site Storybook

Documentation / Charts

Sparkline

Server-compatible@synertech/uiOpen in Storybook

An axis-less line, read for its shape: the trend beside a figure, in a table cell or within a sentence. It does not anchor at zero and fits itself to its own values; two sparklines are therefore only comparable when they share a domain.

Without ariaLabel it stays out of the accessibility tree: beside a value, the value is what informs. Name it when it carries the information on its own. A null value breaks the line.

No hooks: the component renders on the server.

Examples

Basic usage

An array of numbers, in order; the default colour is the palette’s first slot.

Open in a tab
import { Sparkline } from "@synertech/ui";

const revenue = [18, 21, 19, 26, 29, 27, 31, 35];

export function Basic() {
  return <Sparkline data={revenue} width={160} height={36} />;
}

Area and last point

area adds a wash under the line, emphasizeLast marks the current value.

Open in a tab
import { Sparkline } from "@synertech/ui";

const revenue = [18, 21, 19, 26, 29, 27, 31, 35];

export function AreaAndLast() {
  return (
    <div className="flex items-center gap-6">
      <Sparkline data={revenue} width={160} height={36} area emphasizeLast />
      <Sparkline data={revenue} width={160} height={36} curve="step" strokeWidth={2} />
    </div>
  );
}

Shared scale

Without domain, each line fills its height and both seem to vary as much; with a common domain, their amplitudes become comparable again.

Open in a tab
import { Sparkline } from "@synertech/ui";

const revenue = [18, 21, 19, 26, 29, 27, 31, 35];

const churn = [12, 11, 13, 10, 9, 10, 8, 7];

export function SharedDomain() {
  return (
    <div className="grid grid-cols-2 gap-x-8 gap-y-2 text-sm text-gray-600 dark:text-gray-400">
      <span>Échelles libres</span>
      <span>Domaine partagé [0, 40]</span>
      <div className="flex gap-3">
        <Sparkline data={revenue} width={120} height={32} />
        <Sparkline data={churn} width={120} height={32} color="var(--color-chart-2)" />
      </div>
      <div className="flex gap-3">
        <Sparkline data={revenue} domain={[0, 40]} width={120} height={32} />
        <Sparkline
          data={churn}
          domain={[0, 40]}
          width={120}
          height={32}
          color="var(--color-chart-2)"
        />
      </div>
    </div>
  );
}

In a sentence or a table

Inline with text, the sparkline stays decorative; on its own, it gets an ariaLabel.

Open in a tab
import { Sparkline } from "@synertech/ui";

const revenue = [18, 21, 19, 26, 29, 27, 31, 35];

export function Inline() {
  return (
    <div className="flex flex-col gap-4 text-sm">
      <p>
        Le revenu passe de 18 k€ à 35 k€{" "}
        <Sparkline data={revenue} width={64} height={16} className="inline-block align-middle" />{" "}
        sur huit mois.
      </p>
      <Sparkline
        data={[18, 21, null, 26, 29, 27, 31, 35]}
        width={200}
        height={40}
        emphasizeLast
        ariaLabel="Revenu mensuel sur huit mois, de 18 k€ à 35 k€, mars manquant"
      />
    </div>
  );
}

API reference

Components

Sparkline

ComponentServer-compatible

A small, axis-less line showing the shape of a series.

Unlike a chart, it does not anchor its scale at zero: a sparkline is read for its shape, and forcing a zero baseline flattens exactly the variation it exists to show.

No hooks, so it renders on the server and inside a table cell as happily as in a stat tile.

import { Sparkline } from "@synertech/ui";

Props — SparklineProps

PropTypeDefaultDescription
data• readonly (number | null | undefined)[] —

Values in order. null marks a gap, which breaks the line.

area boolean false

Adds a wash under the line.

ariaLabel string —

Names the sparkline for assistive technology. Left out, the sparkline is hidden from it: inside a stat tile the value beside it is the information, and a second reading of the same figure is noise.

color string CHART_SERIES_COLORS[0]

Any CSS colour. Defaults to the first palette slot.

curve ChartCurve "monotone"
domain [number, number] —

Fixes the bounds. Two sparklines only compare if they share a domain — without this, each one rescales to its own values.

emphasizeLast boolean false

Marks the current value with a dot.

height number 28
strokeWidth number 2
width number 96

+ 483 inherited native attributes from SVGAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, event handlers…).