Component Library
Browse and explore components
Bar chart
A Bar Chart is a data visualization component used to represent categorical data with rectangular bars, emphasizing comparisons between discrete groups or values by the length or height of the bars.
Default
Component Code
Copy Code
'use client'
import { Bar, BarChart, CartesianGrid, XAxis } from 'recharts'
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from '@/components/ui/chart'
export const description = 'A bar chart'
const chartData = [
{ month: 'January', desktop: 186 },
{ month: 'February', desktop: 305 },
{ month: 'March', desktop: 237 },
{ month: 'April', desktop: 73 },
{ month: 'May', desktop: 209 },
{ month: 'June', desktop: 214 },
{ month: 'July', desktop: 186 },
{ month: 'August', desktop: 305 },
{ month: 'september', desktop: 237 },
]
const chartConfig = {
desktop: {
label: 'Desktop',
},
} satisfies ChartConfig
export default function ChartBarDefaultCode() {
return (
<>
<ChartContainer config={chartConfig}>
<BarChart accessibilityLayer data={chartData} barSize={30}>
<CartesianGrid vertical={false} />
<XAxis
dataKey='month'
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) => value.slice(0, 3)}
/>
<ChartTooltip
cursor={false}
content={<ChartTooltipContent hideLabel />}
/>
<Bar dataKey='desktop' fill='var(--color-primary)' radius={8} />
</BarChart>
</ChartContainer>
</>
)
}
Horizontal
Component Code
Copy Code
'use client'
import { Bar, BarChart, XAxis, YAxis } from 'recharts'
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from '@/components/ui/chart'
export const description = 'A horizontal bar chart'
const chartData = [
{ month: 'January', desktop: 186 },
{ month: 'February', desktop: 305 },
{ month: 'March', desktop: 237 },
{ month: 'April', desktop: 73 },
{ month: 'May', desktop: 209 },
{ month: 'June', desktop: 214 },
]
const chartConfig = {
desktop: {
label: 'Desktop',
},
} satisfies ChartConfig
export default function ChartBarHorizontalCode() {
return (
<>
<ChartContainer config={chartConfig}>
<BarChart
accessibilityLayer
data={chartData}
barSize={30}
layout='vertical'
margin={{
left: -20,
}}>
<XAxis type='number' dataKey='desktop' hide />
<YAxis
dataKey='month'
type='category'
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) => value.slice(0, 3)}
/>
<ChartTooltip
cursor={false}
content={<ChartTooltipContent hideLabel />}
/>
<Bar dataKey='desktop' fill='var(--color-secondary)' radius={5} />
</BarChart>
</ChartContainer>
</>
)
}
Multiple
Component Code
Copy Code
'use client'
import { Bar, BarChart, CartesianGrid, XAxis } from 'recharts'
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from '@/components/ui/chart'
export const description = 'A multiple bar chart'
const chartData = [
{ month: 'January', desktop: 186, mobile: 80 },
{ month: 'February', desktop: 305, mobile: 200 },
{ month: 'March', desktop: 237, mobile: 120 },
{ month: 'April', desktop: 73, mobile: 190 },
{ month: 'May', desktop: 209, mobile: 130 },
{ month: 'June', desktop: 214, mobile: 140 },
{ month: 'July', desktop: 198, mobile: 150 },
{ month: 'August', desktop: 225, mobile: 160 },
{ month: 'September', desktop: 245, mobile: 170 },
]
const chartConfig = {
desktop: {
label: 'Desktop',
},
mobile: {
label: 'Mobile',
},
} satisfies ChartConfig
export default function ChartBarMultipleCode() {
return (
<>
<ChartContainer config={chartConfig}>
<BarChart accessibilityLayer data={chartData} barSize={30}>
<CartesianGrid vertical={false} />
<XAxis
dataKey='month'
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) => value.slice(0, 3)}
/>
<ChartTooltip
cursor={false}
content={<ChartTooltipContent indicator='dashed' />}
/>
<Bar dataKey='desktop' fill='var(--color-primary)' radius={4} />
<Bar dataKey='mobile' fill='var(--color-secondary)' radius={4} />
</BarChart>
</ChartContainer>
</>
)
}
Stacked + Legend
Component Code
Copy Code
'use client'
import { Bar, BarChart, CartesianGrid, XAxis } from 'recharts'
import {
ChartConfig,
ChartContainer,
ChartLegend,
ChartLegendContent,
ChartTooltip,
ChartTooltipContent,
} from '@/components/ui/chart'
export const description = 'A stacked bar chart with a legend'
const chartData = [
{ month: 'January', desktop: 186, mobile: 80 },
{ month: 'February', desktop: 305, mobile: 200 },
{ month: 'March', desktop: 237, mobile: 120 },
{ month: 'April', desktop: 73, mobile: 190 },
{ month: 'May', desktop: 209, mobile: 130 },
{ month: 'June', desktop: 214, mobile: 140 },
{ month: 'July', desktop: 190, mobile: 175 },
{ month: 'August', desktop: 250, mobile: 145 },
{ month: 'September', desktop: 198, mobile: 160 },
]
const chartConfig = {
desktop: {
label: 'Desktop',
},
mobile: {
label: 'Mobile',
},
} satisfies ChartConfig
export default function ChartBarStackedCode() {
return (
<>
<ChartContainer config={chartConfig}>
<BarChart accessibilityLayer data={chartData} barSize={30}>
<CartesianGrid vertical={false} />
<XAxis
dataKey='month'
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) => value.slice(0, 3)}
/>
<ChartTooltip content={<ChartTooltipContent hideLabel />} />
<ChartLegend content={<ChartLegendContent />} />
<Bar
dataKey='desktop'
stackId='a'
fill='var(--color-primary)'
radius={[0, 0, 4, 4]}
/>
<Bar
dataKey='mobile'
stackId='a'
fill='var(--color-secondary)'
radius={[4, 4, 0, 0]}
/>
</BarChart>
</ChartContainer>
</>
)
}
Label
Component Code
Copy Code
'use client'
import { Bar, BarChart, CartesianGrid, LabelList, XAxis } from 'recharts'
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from '@/components/ui/chart'
export const description = 'A bar chart with a label'
const chartData = [
{ month: 'January', desktop: 186 },
{ month: 'February', desktop: 305 },
{ month: 'March', desktop: 237 },
{ month: 'April', desktop: 73 },
{ month: 'May', desktop: 209 },
{ month: 'June', desktop: 214 },
{ month: 'July', desktop: 190 },
{ month: 'August', desktop: 260 },
{ month: 'September', desktop: 178 },
]
const chartConfig = {
desktop: {
label: 'Desktop',
},
} satisfies ChartConfig
export default function ChartBarLabelCode() {
return (
<>
<ChartContainer config={chartConfig}>
<BarChart
accessibilityLayer
data={chartData}
barSize={30}
margin={{
top: 20,
}}>
<CartesianGrid vertical={false} />
<XAxis
dataKey='month'
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) => value.slice(0, 3)}
/>
<ChartTooltip
cursor={false}
content={<ChartTooltipContent hideLabel />}
/>
<Bar dataKey='desktop' fill='var(--color-secondary)' radius={8}>
<LabelList
position='top'
offset={12}
className='fill-foreground'
fontSize={12}
/>
</Bar>
</BarChart>
</ChartContainer>
</>
)
}
Custom Label
Component Code
Copy Code
'use client'
import { Bar, BarChart, CartesianGrid, LabelList, XAxis, YAxis } from 'recharts'
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from '@/components/ui/chart'
export const description = 'A bar chart with a custom label'
const chartData = [
{ month: 'January', desktop: 186, mobile: 80 },
{ month: 'February', desktop: 305, mobile: 200 },
{ month: 'March', desktop: 237, mobile: 120 },
{ month: 'April', desktop: 73, mobile: 190 },
{ month: 'May', desktop: 209, mobile: 130 },
{ month: 'June', desktop: 214, mobile: 140 },
]
const chartConfig = {
desktop: {
label: 'Desktop',
},
mobile: {
label: 'Mobile',
},
label: {
color: 'var(--background)',
},
} satisfies ChartConfig
export default function ChartBarLabelCustomCode() {
return (
<>
<ChartContainer config={chartConfig}>
<BarChart
accessibilityLayer
data={chartData}
barSize={30}
layout='vertical'
margin={{
right: 16,
}}>
<CartesianGrid horizontal={false} />
<YAxis
dataKey='month'
type='category'
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) => value.slice(0, 3)}
hide
/>
<XAxis dataKey='desktop' type='number' hide />
<ChartTooltip
cursor={false}
content={<ChartTooltipContent indicator='line' />}
/>
<Bar
dataKey='desktop'
layout='vertical'
fill='var(--color-primary)'
radius={4}>
<LabelList
dataKey='month'
position='insideLeft'
offset={8}
className='fill-(--color-white)'
fontSize={12}
/>
<LabelList
dataKey='desktop'
position='right'
offset={8}
className='fill-foreground'
fontSize={12}
/>
</Bar>
</BarChart>
</ChartContainer>
</>
)
}
Mixed
Component Code
Copy Code
'use client'
import { Bar, BarChart, XAxis, YAxis } from 'recharts'
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from '@/components/ui/chart'
export const description = 'A mixed bar chart'
const chartData = [
{ browser: 'chrome', visitors: 275, fill: 'var(--color-primary)' },
{ browser: 'safari', visitors: 200, fill: 'var(--color-secondary)' },
{ browser: 'firefox', visitors: 187, fill: 'var(--color-warning)' },
{ browser: 'edge', visitors: 173, fill: 'var(--color-error)' },
{ browser: 'other', visitors: 90, fill: 'var(--color-info)' },
]
const chartConfig = {
visitors: {
label: 'Visitors',
},
chrome: {
label: 'Chrome',
},
safari: {
label: 'Safari',
},
firefox: {
label: 'Firefox',
},
edge: {
label: 'Edge',
},
other: {
label: 'Other',
},
} satisfies ChartConfig
export default function ChartBarMixedCode() {
return (
<>
<ChartContainer config={chartConfig}>
<BarChart
accessibilityLayer
data={chartData}
barSize={30}
layout='vertical'
margin={{
left: 0,
}}>
<YAxis
dataKey='browser'
type='category'
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) =>
chartConfig[value as keyof typeof chartConfig]?.label
}
/>
<XAxis dataKey='visitors' type='number' hide />
<ChartTooltip
cursor={false}
content={<ChartTooltipContent hideLabel />}
/>
<Bar dataKey='visitors' layout='vertical' radius={5} />
</BarChart>
</ChartContainer>
</>
)
}
Active
Component Code
Copy Code
'use client'
import { Bar, BarChart, CartesianGrid, Rectangle, XAxis } from 'recharts'
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from '@/components/ui/chart'
export const description = 'A bar chart with an active bar'
const chartData = [
{ browser: 'chrome', visitors: 187, fill: 'var(--color-primary)' },
{ browser: 'safari', visitors: 200, fill: 'var(--color-secondary)' },
{ browser: 'firefox', visitors: 275, fill: 'var(--color-warning)' },
{ browser: 'edge', visitors: 173, fill: 'var(--color-error)' },
{ browser: 'other', visitors: 90, fill: 'var(--color-info)' },
]
const chartConfig = {
visitors: {
label: 'Visitors',
},
chrome: {
label: 'Chrome',
},
safari: {
label: 'Safari',
},
firefox: {
label: 'Firefox',
},
edge: {
label: 'Edge',
},
other: {
label: 'Other',
},
} satisfies ChartConfig
export default function ChartBarActiveCode() {
return (
<>
<ChartContainer config={chartConfig}>
<BarChart accessibilityLayer data={chartData} barSize={30}>
<CartesianGrid vertical={false} />
<XAxis
dataKey='browser'
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) =>
chartConfig[value as keyof typeof chartConfig]?.label
}
/>
<ChartTooltip
cursor={false}
content={<ChartTooltipContent hideLabel />}
/>
<Bar
dataKey='visitors'
strokeWidth={2}
radius={8}
activeIndex={2}
activeBar={({ ...props }) => {
return (
<Rectangle
{...props}
fillOpacity={0.8}
stroke={props.payload.fill}
strokeDasharray={4}
strokeDashoffset={4}
/>
)
}}
/>
</BarChart>
</ChartContainer>
</>
)
}
Negative
Component Code
Copy Code
'use client'
import { Bar, BarChart, CartesianGrid, Cell, LabelList } from 'recharts'
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from '@/components/ui/chart'
export const description = 'A bar chart with negative values'
const chartData = [
{ month: 'January', visitors: 186 },
{ month: 'February', visitors: 205 },
{ month: 'March', visitors: -207 },
{ month: 'April', visitors: 173 },
{ month: 'May', visitors: -209 },
{ month: 'June', visitors: 214 },
{ month: 'July', visitors: -180 },
{ month: 'August', visitors: 230 },
{ month: 'September', visitors: -160 },
]
const chartConfig = {
visitors: {
label: 'Visitors',
},
} satisfies ChartConfig
export default function ChartBarNegativeCode() {
return (
<>
<ChartContainer config={chartConfig}>
<BarChart accessibilityLayer data={chartData} barSize={30}>
<CartesianGrid vertical={false} />
<ChartTooltip
cursor={false}
content={<ChartTooltipContent hideLabel hideIndicator />}
/>
<Bar dataKey='visitors'>
<LabelList position='top' dataKey='month' fillOpacity={1} />
{chartData.map((item) => (
<Cell
key={item.month}
fill={
item.visitors > 0
? 'var(--color-primary)'
: 'var(--color-secondary)'
}
/>
))}
</Bar>
</BarChart>
</ChartContainer>
</>
)
}
Interactive
Component Code
Copy Code
'use client'
import * as React from 'react'
import { Bar, BarChart, CartesianGrid, XAxis } from 'recharts'
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from '@/components/ui/chart'
export const description = 'An interactive bar chart'
const chartData = [
{ date: '2024-04-01', desktop: 222, mobile: 150 },
{ date: '2024-04-02', desktop: 97, mobile: 180 },
{ date: '2024-04-03', desktop: 167, mobile: 120 },
{ date: '2024-04-04', desktop: 242, mobile: 260 },
{ date: '2024-04-05', desktop: 373, mobile: 290 },
{ date: '2024-04-06', desktop: 301, mobile: 340 },
{ date: '2024-04-07', desktop: 245, mobile: 180 },
{ date: '2024-04-08', desktop: 409, mobile: 320 },
{ date: '2024-04-09', desktop: 59, mobile: 110 },
{ date: '2024-04-10', desktop: 261, mobile: 190 },
{ date: '2024-04-11', desktop: 327, mobile: 350 },
{ date: '2024-04-12', desktop: 292, mobile: 210 },
{ date: '2024-04-13', desktop: 342, mobile: 380 },
{ date: '2024-04-14', desktop: 137, mobile: 220 },
{ date: '2024-04-15', desktop: 120, mobile: 170 },
{ date: '2024-04-16', desktop: 138, mobile: 190 },
{ date: '2024-04-17', desktop: 446, mobile: 360 },
{ date: '2024-04-18', desktop: 364, mobile: 410 },
{ date: '2024-04-19', desktop: 243, mobile: 180 },
{ date: '2024-04-20', desktop: 89, mobile: 150 },
{ date: '2024-04-21', desktop: 137, mobile: 200 },
{ date: '2024-04-22', desktop: 224, mobile: 170 },
{ date: '2024-04-23', desktop: 138, mobile: 230 },
{ date: '2024-04-24', desktop: 387, mobile: 290 },
{ date: '2024-04-25', desktop: 215, mobile: 250 },
{ date: '2024-04-26', desktop: 75, mobile: 130 },
{ date: '2024-04-27', desktop: 383, mobile: 420 },
{ date: '2024-04-28', desktop: 122, mobile: 180 },
{ date: '2024-04-29', desktop: 315, mobile: 240 },
{ date: '2024-04-30', desktop: 454, mobile: 380 },
{ date: '2024-05-01', desktop: 165, mobile: 220 },
{ date: '2024-05-02', desktop: 293, mobile: 310 },
{ date: '2024-05-03', desktop: 247, mobile: 190 },
{ date: '2024-05-04', desktop: 385, mobile: 420 },
{ date: '2024-05-05', desktop: 481, mobile: 390 },
{ date: '2024-05-06', desktop: 498, mobile: 520 },
{ date: '2024-05-07', desktop: 388, mobile: 300 },
{ date: '2024-05-08', desktop: 149, mobile: 210 },
{ date: '2024-05-09', desktop: 227, mobile: 180 },
{ date: '2024-05-10', desktop: 293, mobile: 330 },
{ date: '2024-05-11', desktop: 335, mobile: 270 },
{ date: '2024-05-12', desktop: 197, mobile: 240 },
{ date: '2024-05-13', desktop: 197, mobile: 160 },
{ date: '2024-05-14', desktop: 448, mobile: 490 },
{ date: '2024-05-15', desktop: 473, mobile: 380 },
{ date: '2024-05-16', desktop: 338, mobile: 400 },
{ date: '2024-05-17', desktop: 499, mobile: 420 },
{ date: '2024-05-18', desktop: 315, mobile: 350 },
{ date: '2024-05-19', desktop: 235, mobile: 180 },
{ date: '2024-05-20', desktop: 177, mobile: 230 },
{ date: '2024-05-21', desktop: 82, mobile: 140 },
{ date: '2024-05-22', desktop: 81, mobile: 120 },
{ date: '2024-05-23', desktop: 252, mobile: 290 },
{ date: '2024-05-24', desktop: 294, mobile: 220 },
{ date: '2024-05-25', desktop: 201, mobile: 250 },
{ date: '2024-05-26', desktop: 213, mobile: 170 },
{ date: '2024-05-27', desktop: 420, mobile: 460 },
{ date: '2024-05-28', desktop: 233, mobile: 190 },
{ date: '2024-05-29', desktop: 78, mobile: 130 },
{ date: '2024-05-30', desktop: 340, mobile: 280 },
{ date: '2024-05-31', desktop: 178, mobile: 230 },
{ date: '2024-06-01', desktop: 178, mobile: 200 },
{ date: '2024-06-02', desktop: 470, mobile: 410 },
{ date: '2024-06-03', desktop: 103, mobile: 160 },
{ date: '2024-06-04', desktop: 439, mobile: 380 },
{ date: '2024-06-05', desktop: 88, mobile: 140 },
{ date: '2024-06-06', desktop: 294, mobile: 250 },
{ date: '2024-06-07', desktop: 323, mobile: 370 },
{ date: '2024-06-08', desktop: 385, mobile: 320 },
{ date: '2024-06-09', desktop: 438, mobile: 480 },
{ date: '2024-06-10', desktop: 155, mobile: 200 },
{ date: '2024-06-11', desktop: 92, mobile: 150 },
{ date: '2024-06-12', desktop: 492, mobile: 420 },
{ date: '2024-06-13', desktop: 81, mobile: 130 },
{ date: '2024-06-14', desktop: 426, mobile: 380 },
{ date: '2024-06-15', desktop: 307, mobile: 350 },
{ date: '2024-06-16', desktop: 371, mobile: 310 },
{ date: '2024-06-17', desktop: 475, mobile: 520 },
{ date: '2024-06-18', desktop: 107, mobile: 170 },
{ date: '2024-06-19', desktop: 341, mobile: 290 },
{ date: '2024-06-20', desktop: 408, mobile: 450 },
{ date: '2024-06-21', desktop: 169, mobile: 210 },
{ date: '2024-06-22', desktop: 317, mobile: 270 },
{ date: '2024-06-23', desktop: 480, mobile: 530 },
{ date: '2024-06-24', desktop: 132, mobile: 180 },
{ date: '2024-06-25', desktop: 141, mobile: 190 },
{ date: '2024-06-26', desktop: 434, mobile: 380 },
{ date: '2024-06-27', desktop: 448, mobile: 490 },
{ date: '2024-06-28', desktop: 149, mobile: 200 },
{ date: '2024-06-29', desktop: 103, mobile: 160 },
{ date: '2024-06-30', desktop: 446, mobile: 400 },
]
const chartConfig = {
views: {
label: 'Page Views',
},
desktop: {
label: 'Desktop',
color: 'var(--color-primary)',
},
mobile: {
label: 'Mobile',
color: 'var(--color-secondary)',
},
} satisfies ChartConfig
export default function ChartBarInteractiveCode() {
const [activeChart, setActiveChart] =
React.useState<keyof typeof chartConfig>('desktop')
const total = React.useMemo(
() => ({
desktop: chartData.reduce((acc, curr) => acc + curr.desktop, 0),
mobile: chartData.reduce((acc, curr) => acc + curr.mobile, 0),
}),
[]
)
return (
<>
{/* */}
<div className='flex border border-ld rounded-md'>
{['desktop', 'mobile'].map((key) => {
const chart = key as keyof typeof chartConfig
return (
<button
key={chart}
data-active={activeChart === chart}
className='data-[active=true]:bg-lightprimary dark:data-[active=true]:bg-darkprimary relative z-30 flex flex-1 flex-col justify-center gap-1 px-6 py-4 text-left sm:px-8 sm:py-6'
onClick={() => setActiveChart(chart)}>
<span className='text-muted-foreground text-xs'>
{chartConfig[chart].label}
</span>
<span className='text-lg leading-none font-bold sm:text-3xl'>
{total[key as keyof typeof total].toLocaleString()}
</span>
</button>
)
})}
</div>
{/* */}
<ChartContainer config={chartConfig}>
<BarChart
accessibilityLayer
data={chartData}
margin={{
left: 12,
right: 12,
}}>
<CartesianGrid vertical={false} />
<XAxis
dataKey='date'
tickLine={false}
axisLine={false}
tickMargin={8}
minTickGap={32}
tickFormatter={(value) => {
const date = new Date(value)
return date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
})
}}
/>
<ChartTooltip
content={
<ChartTooltipContent
className='w-[150px]'
nameKey='views'
labelFormatter={(value) => {
return new Date(value).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
})
}}
/>
}
/>
<Bar dataKey={activeChart} fill={`var(--color-${activeChart})`} />
</BarChart>
</ChartContainer>
</>
)
}