Search documentation

Search pages, components, blocks, styles and colours.

Get started
Skip to content

Composition Chart

PreviousNext

How the mix of a whole changes over time, as 100% columns with each part's current share.

Traffic by source
Acme Inc, share of visits over 30 days
Share of visits by source
SearchDirectReferralSocialEmail
Nov 13,8002,6301,2802,560550
Nov 24,0102,6801,2602,4801,670
Nov 34,1002,6201,3402,380520
Nov 44,2202,6001,3502,230600
Nov 54,2702,6501,3502,080520
Nov 64,3302,5501,4101,960590
Nov 74,4002,5101,4102,000560
Nov 84,4702,4801,3501,870540
Nov 94,3402,3101,3401,9501,640
Nov 104,3002,3301,4001,990570
Nov 114,2402,2801,3101,970590
Nov 124,2002,2301,3402,030500
Nov 134,2402,3201,3202,090570
Nov 144,2902,2001,3102,030590
Nov 154,3202,3201,3301,990600
Nov 164,2902,3401,2901,8201,630
Nov 174,4102,3401,3801,750560
Nov 184,5302,3701,4001,600560
Nov 194,5502,3901,3901,420590
Nov 204,7802,5001,4301,430560
Nov 214,8802,6101,5001,470560
Nov 225,0102,6101,5801,390600
Nov 235,0502,6401,5501,4701,620
Nov 245,1802,7201,6201,550600
Nov 255,1902,7201,6501,550520
Nov 265,2002,7401,6501,500560
Nov 275,1702,6901,5901,450540
Nov 285,1402,6201,5601,350500
Nov 295,0202,7001,6201,210520
Nov 305,0402,6001,5901,1201,640

Give it rows of data, the field that names each column (index) and a config that names the parts. Each row becomes a column stretched to the whole, split into its parts in the next colours of the chart palette, so the chart reads as a mix rather than a size. Columns are wider than a Bar Chart's, at most 40px, with less air between them, and touching parts keep a 2px gap.

The legend lists every part with its share. With nothing inspected it reads the latest column; point at a column, drag a finger across the chart or tab to it and use the arrow keys, and the shares follow the inspected column while a tooltip gives each part's value and share. Pressing a part in the legend hides it, and the rest share the whole between them; pointing at it spotlights it.

Composition Chart is part of the Fabricator style. It's drawn in SVG, so the same component ships for every base, and its layout comes from the same model as the React Native version.

Installation

bunx --bun fabricator-ui@latest add composition-chart

Usage

import { type ChartConfig } from "@/components/ui/chart-kit"
import { CompositionChart } from "@/components/ui/composition-chart"
const data = [
  { date: "Nov 1", search: 3_800, direct: 2_630, social: 2_560 },
  { date: "Nov 2", search: 4_010, direct: 2_680, social: 2_480 },
  { date: "Nov 3", search: 4_100, direct: 2_620, social: 2_380 },
]
 
const config = {
  search: { label: "Search" },
  direct: { label: "Direct" },
  social: { label: "Social" },
} satisfies ChartConfig
 
<CompositionChart
  data={data}
  index="date"
  config={config}
  label="Share of visits by source"
/>

Give the parts as plain values; the chart works out each one's share of its column. A missing value leaves that part out of its column, and a negative one counts as nothing. The chart fills its container's width and is 18rem tall until you give it a height with className.

Examples

Fewer columns

With a few columns each one stops at 40px and the rest of its band is air, so a short series still reads as a row of columns.

Accounts by plan
FreeProTeamEnterprise
Jun4,1201,38042064
Jul4,3101,52047071
Aug4,2601,71054078
Sep4,4801,89063092
Oct4,3902,140720104
Nov4,5502,360810121

Colours

Parts take the chart palette in the order of config, and keep their colour when another is hidden. Give a part its own with color: a palette slot, an upstream chart token (chart-1 … chart-5), a role (primary, info, success, …), a CSS variable (var(--brand)) or any CSS colour.

TokenHueTokenHue
--chart-series-1blue--chart-series-5magenta
--chart-series-2orange--chart-series-6green
--chart-series-3aqua--chart-series-7violet
--chart-series-4amber--chart-series-8red

The palette ships with the foundations, chosen separately for light and dark, every slot at 3:1 or more against the page and the card. Slots never repeat: a ninth part is grey, so fold small parts into an "Other" one before that.

Motion

  • Grow: the first time the chart scrolls into view, the columns grow from the baseline in a quick wave, on the reveal role, once per mount.
  • Morph: when data, config or the visible parts change, each part moves and resizes to its new share on the move role. A part being shown grows in; one being hidden shrinks away while the others take its room.
  • Glide: the column highlight and the tooltip glide between columns on the glide role.

All three follow the motion speed of the region they sit in. Reduce Motion and the instant speed draw the chart at once and drop the glide; animate={false} does the same for one chart.

API Reference

CompositionChart

All other props go to the root <div>.

PropTypeDefault
dataTDatum[]required
configChartConfigrequired
indexkeyof TDatumrequired
labelstringrequired: the accessible name
formatValue(value: number, key: string) => string1,234
formatIndex(value: string, datum: TDatum) => stringthe field as text
legendbooleanon for two or more parts
tooltipbooleantrue
animatebooleantrue
loadingbooleanfalse
emptyReactNode"No data"
onActiveChange(active: ChartActive<TDatum> | null) => void-

ChartConfig

FieldTypeDefault
labelstringrequired
colorChartColorthe next palette slot

Data attributes

AttributeElementPresent when
data-slot="composition-chart"rootalways
data-loadingrootloading is true
data-slot="chart-plot"the plotalways
data-slot="chart-bar"a partalways; data-dimmed while another is spotlit
data-slot="chart-band"highlighta column is inspected
data-slot="chart-legend"the legendthe legend shows
data-slot="composition-chart-share"a part's sharethe part is shown
data-slot="chart-tooltip"the tooltipa column is inspected

Accessibility

  • The plot is a slider named after label ("Share of visits by source: inspect values"). Tab to it, then the arrow keys step through the columns, Home and End jump to the first and last, and Escape lets go. Its value text reads the tooltip: the column and every part's value and share.
  • Every value is also in a table hidden from sight, so a screen reader can read the data without inspecting it.
  • With two or more parts the legend always shows, so no part is told apart by colour alone. Its items are toggle buttons with aria-pressed, and each one's name includes its share.
  • Text never takes a part's colour: names and shares stay in the text colours, beside a coloured key.
  • loading sets aria-busy on the root and dims the last drawing until new data arrives.