Skip to content

Sparkbar (Mini Bar Chart) for React

A React sparkbar is a mini bar chart for stat tiles and table cells, the bar twin of the sparkline. Pass values as a readonly number[], oldest first; a missing entry leaves a gap, never a zero.

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/sparkbar.json

Signups, weekly

1,186▲ 12.7% vs last week

In stat tiles

The latest bar is drawn at full strength; earlier bars recede to a lighter tint. Values below zero hang from a zero line. Use highlight="none" when no period is special.

Signups

1,284

Weekly, last 12 weeks

Orders

3,912

Daily, last 14 days

Net cash flow

+$18.4K

Weekly, US$ thousands

Files to copy

5 folders · 12 files · 3 packages
From theme/src/
  • components/charts/{sparkbar.tsx, chart-utils.ts}
  • hooks/{use-controllable.ts, use-intersection.ts, use-latest.ts, use-media-query.ts, use-preferences.ts}
  • motion/tokens.ts
  • lib/{cn.ts, form.ts, math.ts, warn-once.ts}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/sparkbar.jsonRegistry item: sparkbar. The styles need the Tailwind v4 glass UI setup.
The examples also use
  • components/glass-card/
  • motion/light-follower.ts
  • lib/heading.ts
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/glass-card.json
Packages
pnpm add clsx motion tailwind-merge

Next.js: samples here pass functions (formatters, seriesLegend, a dashboard's load), so put them in a file that starts with "use client".

Win / loss in a table

Only the sign counts: up for a win, down for a loss, a short dash for a draw. Wins and losses use two distinct palette hues (not success and danger: a loss isn't an error).

League form, last 12 matches
TeamLast 12W–D–L
Harbour City
9–1–2
Northfield Rovers
7–2–3
Ashdown Athletic
5–2–5
Kestrel Town
3–2–7

Props

PropTypeDefaultDescription
values (required)readonly (number | null | undefined)[]NoneValues, oldest first. A missing (non-numeric) entry leaves its slot empty: a gap, never a zero.
variant"bar" | "winloss""bar""bar": tiny columns, sized by value (negative values hang below a zero line). "winloss": only the sign counts: up for a win (> 0), down for a loss (< 0), a dash for a draw (0).
highlightnumber | "none" | "last"variant === "bar" ? "last" : "none"Which bar to emphasise at full strength; the rest step back to a lighter step of the same hue. "last" (the latest value; default for "bar"), "none" (default for "winloss"), or an index into values (negative counts from the end: -1 is the last entry). An index outside values, or on a gap, emphasises nothing.
colorChartColor1Colour slot of the bars ("bar"), or of wins ("winloss").
negativeColorChartColor2Sets the colour slot of losses ("winloss").
heightnumber32Height of the chart in px; the width follows the container.
format((n: number) => string)formatCompactFormats the low / high / latest values in the spoken summary.
aria-labelstringNoneName the trend, e.g. "Weekly signups". A summary of the values is appended. Without it the sparkbar is decorative.
labelsPartial<SparkbarLabels>defaultSparkbarLabelsEvery other string, for translation: labels={{ barSummary: "…" }}; omitted keys keep the English of defaultSparkbarLabels. All 3 keys of SparkbarLabels are listed in the key table under the props table.
classNamestringNoneAdds extra classes to the outer element.
SparkbarLabels keys and English defaults (3)
Pass any key in labels; the rest keep the defaults of defaultSparkbarLabels.
KeyEnglish default
barSummary(low, high, latest) => `low ${low}, high ${high}, latest ${latest}`
winLossSummary(wins, losses, draws) => [ `${wins} ${wins === 1 ? "win" : "wins"}`, `${losses} ${losses === 1 ? "loss" : "...
emptyNo data

Keyboard

KeysAction
TabNot applicable: the sparkbar is not interactive, so it is not a tab stop.

Accessibility

  • With an aria-label it is an image named by that label plus a short summary (or “No data”); without one it is decorative.
  • Put the number it summarizes next to it as text: the bars are a glanceable hint, not the only source of the value.
  • Under reduced motion nothing morphs: the bars render at their final values and new data replaces the old at once.

All exports on this page

  • Sparkbar — A tiny bar chart for stat tiles and table cells: magnitude ("bar") or wins and losses ("winloss").
  • defaultSparkbarLabels — The built-in English strings of Sparkbar; spread it into your own labels and change what you need.

When to use the sparkbar chart

  • Use it as a sparkline bar chart inside a stat tile to show the last twelve periods of a metric beside the current number, with the latest bar at full strength and history in a lighter tint.
  • It fits a table cell showing a team's recent record, using variant winloss so only the sign of each result is drawn: up for a win, down for a loss, a short dash for a draw.
  • It works for tiny trend hints in a list of accounts or projects, where hundreds of rows need a glanceable shape and not a full chart with axes.
  • Skip it when readers need to hover for values or compare axes, since a sparkbar is not interactive; use BarChart for that, or Sparkline when the series is a continuous line rather than discrete periods.
  • Pair it with Sparkline for continuous trends, Badge or a plain number for the headline value next to it, and DataTable to place a sparkbar in a column.

Frequently asked questions

48 charts pages in all: browse them on Charts.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .