Stacked Bar Chart
Charts
Data Visualization
Visualize composition and breakdown across categories over time with stacked horizontal bars.
Preview
Traffic by device type with stacked segments
- Desktop
- Mobile
- Tablet
Jan
90
Feb
105
Mar
106
Apr
124
May
125
Jun
146
Percentage Mode
100% stacked bars showing proportional distribution
- Desktop
- Mobile
- Tablet
Jan
50%
33%
17%
Feb
50%
33%
17%
Mar
45%
36%
19%
Apr
48%
34%
18%
May
44%
36%
20%
Jun
47%
34%
19%
Props
Component API reference
| Prop | Type | Default | Description |
|---|---|---|---|
data* | Record<string, unknown>[] | — | Array of data objects with category values |
categories* | string[] | — | Keys for stacked categories |
labelKey* | string | — | Key for row labels |
colors | string[] | — | Fill classes per category, e.g. 'bg-info-default text-info-foreground' |
mode | 'stacked' | 'percent' | 'stacked' | Chart display mode |
barHeight | number | 40 | Height of each bar in pixels |
showTotal | boolean | true | Show total value on right |
showLegend | boolean | true | Show color legend |
Usage
Import and implementation examples
import { StackedBarChart } from '@/components/ui/stacked-bar-chart'
const data = [ { month: 'Jan', desktop: 45, mobile: 30, tablet: 15 }, { month: 'Feb', desktop: 52, mobile: 35, tablet: 18 }, { month: 'Mar', desktop: 48, mobile: 38, tablet: 20 },]
<StackedBarChart data={data} categories={['desktop', 'mobile', 'tablet']} labelKey="month"/>
// 100% mode shows each share inside its segment<StackedBarChart data={data} categories={['desktop', 'mobile', 'tablet']} labelKey="month" mode="percent" showTotal={false} />Features
Built-in functionality
- Multiple modes: Stacked, grouped, and percent modes
- Interactive legend: Toggle series visibility
- Segment gaps: Optional spacing between segments
- Hover states: Highlight segments on hover
- Tooltips: Show values on hover
Accessibility
Accessibility considerations
ARIA Attributes
Uses role="img" for chart containeraria-label describes chart contentSegments have title attributesKeyboard Navigation
| Key | Action |
|---|---|
| N/A | Chart is not keyboard interactive |
Notes
- Legend provides text labels for colors
- Values shown in tooltips and totals
- Consider data table for complex data