Ir al contenido
Español
Gráfico de barras apiladas viene con Pro. La vista previa aparece al desbloquearlo.

Cómo funciona

Hand-written SVG laid out in real pixels. Each column segment is a rounded rect with a 3px gap to the one below, and the whole column rises out of a clip at the baseline, so the caps keep their shape while it moves. The distribution bar is a row of 12px pills with 6px gaps that sweep in from the left. Selection changes only colour (soft to solid) and the counting numbers; new data moves every segment from its old size through CSS geometry transitions.

Úsalo para

  • A total per day or week and what it is made of: study time by kind, energy by source, tickets by channel.
  • One whole split into two to four parts, read as shares: patients by visit type, storage by file type.
  • Two outcomes of one flow, each with its own number and change: orders against abandoned carts.

Busca otra opción cuando

  • More than four layers or many thin parts: they become slivers; group the small ones or use a table.
  • Comparing one layer across days precisely: only the bottom layer shares a baseline; use a line or grouped bars.
  • Values that can go negative: this chart treats them as 0.

Uso

import { StackedBarChart } from './StackedBarChart.jsx'

const minutes = (m) => (m >= 60 ? `${Math.floor(m / 60)}h ${m % 60}m` : `${m}m`)

export function StudyCard() {
  return (
    <StackedBarChart
      title="Study time"
      subtitle="This week"
      onOpen={() => {}}
      caption={(day) => day}
      categories={['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']}
      series={[
        { name: 'Lessons', data: [45, 30, 50, 25, 40, 70, 55], color: 1 },
        { name: 'Practice', data: [30, 45, 20, 40, 35, 50, 40], color: 7 },
        { name: 'Review', data: [15, 10, 25, 15, 20, 30, 20], color: 4 },
      ]}
      formatValue={minutes}
    />
  )
}

export function PatientsCard() {
  return (
    <StackedBarChart
      variant="distribution"
      title="Patients"
      subtitle="This month"
      delta={{ value: 142, label: 'vs last month' }}
      parts={[
        { name: 'Inpatients', value: 120, color: 4 },
        { name: 'Outpatients', value: 200, color: 3 },
        { name: 'Emergency', value: 45, color: 2 },
      ]}
    />
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
variant'columns' | 'distribution''columns'columns stacks series per category over time; distribution splits one whole into pill segments.
categoriesstring[]—Columns: the categories, one column each. The selected one shows as an ink pill. `labels` is the older name of this prop and still works (deprecated: in blocks `labels` means text overrides).
series{ name: string, data: number[], color?: number | string }[]—Columns: the layers from bottom to top. Two to four work best; color is 1–8 for --ds-chart-N or a CSS colour. Negative values count as 0.
parts{ name: string, value: number, color?: number | string, delta?: number, positive?: 'up' | 'down', formatDelta?: (n) => string, note?: string }[]—Distribution: the parts of the whole, left to right. delta, positive, formatDelta and note are used by the rows layout.
partsLayout'legend' | 'rows''legend'Distribution: legend puts counting names and values under the bar; rows gives each part a stat row with its share, change and note (best for two or three parts).
title / subtitle / iconReactNode—Card header: a tinted icon tile, a 15px title and a quieter subtitle after it.
onOpen / action() => void / ReactNode—onOpen adds a chevron button at the end of the header. action replaces it with any node.
value / unit / captionnumber | string / string / string | (category, index) => string—Hero. By default it is the selected column total (columns) or the whole (distribution), and it counts. A caption function follows the selected column.
delta{ value: number, label?: string, positive?: 'up' | 'down', format?: (n) => string }—Change badge after the hero. positive: 'down' makes a fall green.
selected / onSelectnumber / (index: number) => void—Columns: the column at rest, solid while the rest stay soft. Defaults to the last one; a click or Enter pins another.
heightnumber120Columns: plot height in px, not counting the axis labels.
barWidthnumber—Columns: bar width in px. By default 56% of the column, at most 28px.
showLegendbooleantrueLegend with counting values: the selected column parts, or the distribution parts.
formatValue / formatPercent(n: number) => string(n) => n.toLocaleString('en-US') / (n) => n.toFixed(1) + '%'Number formats for values and shares.
labelstring—Accessible name of the plot and caption of the hidden table. Defaults to the title.
stringsobject—Replaces built-in text: open, total, category, share, plot(name, count), column(category, total, parts), part(name, value, share). Named strings because labels holds the categories.
replayKeystring | number—Change it to play the entrance again. Changing categories also replays it; changing only values glides.
className / style / ...restHTMLAttributes—Passed to the card root, which fills its container width.

Accesibilidad

  • The plot is a focusable group named after the chart. Left and Right move between columns or segments, Home and End jump to the ends, Enter pins a column, Escape goes back to the pinned one.
  • Each move is announced through a polite live region with the total and every part.
  • A visually hidden table holds every value; the drawing is hidden from screen readers.
  • Legend names sit next to each value, so parts are not told apart by colour alone; change badges also carry an arrow.
  • Reduced motion shows the final chart at once and the numbers do not count.

Movimiento

Entrance waits until 30% of the card is visible. Columns rise out of the baseline over 900ms on cubic-bezier(0.22, 1, 0.36, 1), 28ms apart, and the ink axis pill pops at 560ms; distribution segments sweep in 140ms apart. The hero and legend count over 900ms and legend items rise 60ms apart. Moving the selection counts in 380ms; new data glides over 900ms.

Añádelo a tu proyecto

Los miembros Pro copian un prompt y su agente de programación instala el código, igual que con los gratuitos.

StackedBarChart.jsxstacked-bar-chart.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Gráficos
Trilateración viene con Pro. La vista previa aparece al desbloquearlo.