HextaUI

Chart: Bar chart

A grid, an axis, <ChartTooltip /> and <ChartLegend />. The tooltip and legend read their labels and colors from the config.

"use client"

import { Bar, BarChart, CartesianGrid, XAxis } from "recharts"

import {
  ChartContainer,
  ChartLegend,
  ChartLegendContent,
  ChartTooltip,
  ChartTooltipContent,
  type ChartConfig,
} from "@/components/ui/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 },
]

const chartConfig = {
  desktop: {
    label: "Desktop",
    color: "var(--chart-1)",
  },
  mobile: {
    label: "Mobile",
    color: "var(--chart-2)",
  },
} satisfies ChartConfig

export function ChartBar() {
  return (
    <ChartContainer
      aria-label="Bar chart of desktop and mobile visitors by month"
      config={chartConfig}
      className="min-h-52 w-full"
    >
      <BarChart data={chartData}>
        <CartesianGrid vertical={false} />
        <XAxis
          dataKey="month"
          tickLine={false}
          tickMargin={10}
          axisLine={false}
          tickFormatter={(value) => value.slice(0, 3)}
        />
        <ChartTooltip content={<ChartTooltipContent />} />
        <ChartLegend content={<ChartLegendContent />} />
        <Bar dataKey="desktop" fill="var(--color-desktop)" radius={4} />
        <Bar dataKey="mobile" fill="var(--color-mobile)" radius={4} />
      </BarChart>
    </ChartContainer>
  )
}

This adds the example as components/examples/chart/bar.tsx, together with the Chart component and the HextaUI theme tokens it needs.

pnpm dlx shadcn@latest add https://hextaui.com/r/chart-bar.json

To add only the component, follow the Chart installation.

Props in this example

The Chart props this example sets, from the API reference.

PropTypeDefault
configLabels, icons and colors for each data key.
ChartConfig–
PropTypeDefault
contentUsually <ChartTooltipContent />.
ReactElement | (props) => ReactNode–
PropTypeDefault
contentUsually <ChartLegendContent />.
ReactElement | (props) => ReactNode–

Every prop is in the Chart API reference.

See them all, with keyboard support and accessibility notes, on the Chart page.