# Bar Chart

> Colourful bars that grow in, with a tooltip on hover.

- Category: Data display
- Tier: Free (MIT)
- Page: https://bowls.dev/components/bar-chart
- Requires: React 18+, Tailwind CSS 4, motion

## Install

Copy the source below into `components/bowls/bar-chart.tsx`, then install its dependencies:

```bash
npm i motion
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `data` | `{ label; value; color }[]` | — | One entry per bar. |
| `title` | `string` | — | Caption, also read to screen readers. |
| `height` | `number` | `150` | Chart height, in px. |
| `format` | `(value: number) => string` | — | Tooltip text. |

## Usage

```tsx
"use client";

import { BarChart } from "@/components/bowls/bar-chart";

// Sample data for the demo.
const data = [
  { label: "Mon", value: 4, color: "#ff5c39" },
  { label: "Tue", value: 7, color: "#ffb800" },
  { label: "Wed", value: 5, color: "#22c55e" },
  { label: "Thu", value: 9, color: "#3b82f6" },
  { label: "Fri", value: 6, color: "#a855f7" },
  { label: "Sat", value: 3, color: "#ec4899" },
];

export default function Demo() {
  return (
    <div className="flex h-full w-full items-center justify-center px-8 pt-4">
      <BarChart title="Coffees this week (sample)" data={data} format={(v) => `${v} cups`} />
    </div>
  );
}
```

## Source

`components/bowls/bar-chart.tsx`

```tsx
"use client";

import { motion } from "motion/react";
import { useState } from "react";

export type Bar = { label: string; value: number; color: string };

export type BarChartProps = {
  data: Bar[];
  /** Describes the chart for screen readers. */
  title: string;
  height?: number;
  /** Formats the value in the tooltip. */
  format?: (value: number) => string;
};

/** Bars that grow in on mount, each in its own colour, with a hover tooltip. */
export function BarChart({ data, title, height = 150, format = String }: BarChartProps) {
  const [active, setActive] = useState<number | null>(null);
  const max = Math.max(...data.map((d) => d.value), 1);

  return (
    <figure className="w-full max-w-[300px]">
      <figcaption className="mb-3 text-[13px] font-semibold">{title}</figcaption>
      <div className="flex items-end gap-2" style={{ height }} role="img" aria-label={`${title}: ${data.map((d) => `${d.label} ${format(d.value)}`).join(", ")}`}>
        {data.map((d, i) => (
          <div key={d.label} className="relative flex h-full flex-1 flex-col justify-end" onPointerEnter={() => setActive(i)} onPointerLeave={() => setActive(null)}>
            {active === i && (
              <span className="absolute -top-7 left-1/2 -translate-x-1/2 rounded-md bg-[#0a0a0a] px-1.5 py-0.5 font-mono text-[11px] whitespace-nowrap text-white dark:bg-[#f5f5f5] dark:text-black">
                {format(d.value)}
              </span>
            )}
            <motion.span
              className="block w-full rounded-t-lg rounded-b-sm"
              style={{ background: d.color, opacity: active === null || active === i ? 1 : 0.35, transition: "opacity .2s" }}
              initial={{ height: 0 }}
              animate={{ height: `${(d.value / max) * 100}%` }}
              transition={{ type: "spring", stiffness: 120, damping: 16, delay: i * 0.06 }}
            />
          </div>
        ))}
      </div>
      <div className="mt-2 flex gap-2">
        {data.map((d) => (
          <span key={d.label} className="flex-1 text-center font-mono text-[11px] text-[#666] dark:text-[#999]">{d.label}</span>
        ))}
      </div>
    </figure>
  );
}
```
