shadcn.io is not affiliated with official shadcn/ui
Chart examples · Bar
An interactive bar chart for React
A bar chart of three months of daily page views with dates on the axis and in the tooltip.
Use it for daily data over weeks or months. Three months of daily page views are bars at a fixed height, minTickGap keeps the date ticks apart, and the tooltip shows the full date. The series on show is set by one constant.
Preview
Installation
FAQ
How do I switch the chart to mobile?
activeChart is "desktop". Set it to "mobile". Bar uses dataKey={activeChart} and a fill of var(--color- plus that key, so the series and the color follow the constant. config maps desktop to var(--chart-2) and mobile to var(--chart-1).
Why does the tooltip say Page Views?
ChartTooltipContent sets nameKey="views". That looks up the views entry in config, whose label is Page Views, and uses it as the row name for whichever series is drawn.
Why does the tooltip include the year when the axis doesn't?
XAxis uses tickFormatter={formatDateTick}, a short month and day, and minTickGap={32} so daily labels stay 32 pixels apart. labelFormatter is formatDateLabel, which adds year: "numeric", so the tooltip title reads like Apr 1, 2024. The tooltip also sets className to a fixed w-[150px].
How is the height fixed?
ChartContainer sets aspect-auto h-[250px] w-full. aspect-auto clears the default 16:9 box so h-[250px] holds, and the width follows the max-w-xl frame. CartesianGrid sets vertical={false}.
Related
- A bar chartA bar chart with rounded corners
- An interactive area chartA stacked area chart of three months of
- A multiple bar chartA bar chart grouping desktop and mobile
- An interactive line chartA line chart of three months of daily
- Expandable Sub-Rows Data TableA table of projects where each row
- A bar chart with a labelA bar chart with each bar's value printed