How the mix of a whole changes over time, as 100% columns with each part's current share.
| Search | Direct | Referral | Social | ||
|---|---|---|---|---|---|
| Nov 1 | 3,800 | 2,630 | 1,280 | 2,560 | 550 |
| Nov 2 | 4,010 | 2,680 | 1,260 | 2,480 | 1,670 |
| Nov 3 | 4,100 | 2,620 | 1,340 | 2,380 | 520 |
| Nov 4 | 4,220 | 2,600 | 1,350 | 2,230 | 600 |
| Nov 5 | 4,270 | 2,650 | 1,350 | 2,080 | 520 |
| Nov 6 | 4,330 | 2,550 | 1,410 | 1,960 | 590 |
| Nov 7 | 4,400 | 2,510 | 1,410 | 2,000 | 560 |
| Nov 8 | 4,470 | 2,480 | 1,350 | 1,870 | 540 |
| Nov 9 | 4,340 | 2,310 | 1,340 | 1,950 | 1,640 |
| Nov 10 | 4,300 | 2,330 | 1,400 | 1,990 | 570 |
| Nov 11 | 4,240 | 2,280 | 1,310 | 1,970 | 590 |
| Nov 12 | 4,200 | 2,230 | 1,340 | 2,030 | 500 |
| Nov 13 | 4,240 | 2,320 | 1,320 | 2,090 | 570 |
| Nov 14 | 4,290 | 2,200 | 1,310 | 2,030 | 590 |
| Nov 15 | 4,320 | 2,320 | 1,330 | 1,990 | 600 |
| Nov 16 | 4,290 | 2,340 | 1,290 | 1,820 | 1,630 |
| Nov 17 | 4,410 | 2,340 | 1,380 | 1,750 | 560 |
| Nov 18 | 4,530 | 2,370 | 1,400 | 1,600 | 560 |
| Nov 19 | 4,550 | 2,390 | 1,390 | 1,420 | 590 |
| Nov 20 | 4,780 | 2,500 | 1,430 | 1,430 | 560 |
| Nov 21 | 4,880 | 2,610 | 1,500 | 1,470 | 560 |
| Nov 22 | 5,010 | 2,610 | 1,580 | 1,390 | 600 |
| Nov 23 | 5,050 | 2,640 | 1,550 | 1,470 | 1,620 |
| Nov 24 | 5,180 | 2,720 | 1,620 | 1,550 | 600 |
| Nov 25 | 5,190 | 2,720 | 1,650 | 1,550 | 520 |
| Nov 26 | 5,200 | 2,740 | 1,650 | 1,500 | 560 |
| Nov 27 | 5,170 | 2,690 | 1,590 | 1,450 | 540 |
| Nov 28 | 5,140 | 2,620 | 1,560 | 1,350 | 500 |
| Nov 29 | 5,020 | 2,700 | 1,620 | 1,210 | 520 |
| Nov 30 | 5,040 | 2,600 | 1,590 | 1,120 | 1,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.
| Free | Pro | Team | Enterprise | |
|---|---|---|---|---|
| Jun | 4,120 | 1,380 | 420 | 64 |
| Jul | 4,310 | 1,520 | 470 | 71 |
| Aug | 4,260 | 1,710 | 540 | 78 |
| Sep | 4,480 | 1,890 | 630 | 92 |
| Oct | 4,390 | 2,140 | 720 | 104 |
| Nov | 4,550 | 2,360 | 810 | 121 |
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.
| Token | Hue | Token | Hue |
|---|---|---|---|
--chart-series-1 | blue | --chart-series-5 | magenta |
--chart-series-2 | orange | --chart-series-6 | green |
--chart-series-3 | aqua | --chart-series-7 | violet |
--chart-series-4 | amber | --chart-series-8 | red |
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
revealrole, once per mount. - Morph: when
data,configor the visible parts change, each part moves and resizes to its new share on themoverole. 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
gliderole.
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>.
| Prop | Type | Default |
|---|---|---|
data | TDatum[] | required |
config | ChartConfig | required |
index | keyof TDatum | required |
label | string | required: the accessible name |
formatValue | (value: number, key: string) => string | 1,234 |
formatIndex | (value: string, datum: TDatum) => string | the field as text |
legend | boolean | on for two or more parts |
tooltip | boolean | true |
animate | boolean | true |
loading | boolean | false |
empty | ReactNode | "No data" |
onActiveChange | (active: ChartActive<TDatum> | null) => void | - |
ChartConfig#
| Field | Type | Default |
|---|---|---|
label | string | required |
color | ChartColor | the next palette slot |
Data attributes#
| Attribute | Element | Present when |
|---|---|---|
data-slot="composition-chart" | root | always |
data-loading | root | loading is true |
data-slot="chart-plot" | the plot | always |
data-slot="chart-bar" | a part | always; data-dimmed while another is spotlit |
data-slot="chart-band" | highlight | a column is inspected |
data-slot="chart-legend" | the legend | the legend shows |
data-slot="composition-chart-share" | a part's share | the part is shown |
data-slot="chart-tooltip" | the tooltip | a 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.
loadingsetsaria-busyon the root and dims the last drawing until new data arrives.