← Components

Communication

Chart

Bar, line and area charts in SVG with chart tokens. Bars grow from the baseline, lines draw, areas rise; new data morphs from the old; the tooltip follows on a spring and snaps to the nearest point.

Examples and variants

Bars

Two series. New data morphs the bars.

Revenue grows every month; new customers bring most of it.
Revenue by month, in thousands
monthNewReturning
Jan$16k$9k
Feb$24k$13k
Mar$32k$17k
Apr$35k$21k
May$43k$21k
Jun$46k$25k
Example
function ChartBars() {
  const [seed, setSeed] = useState(0)
  return (
    <div className="flex w-full flex-col gap-stack-sm">
      <Chart
        label="Revenue by month, in thousands"
        summary="Revenue grows every month; new customers bring most of it."
        data={revenue(seed)}
        xKey="month"
        series={[
          { key: "new", label: "New" },
          { key: "returning", label: "Returning" },
        ]}
        format={(n) => `$${n}k`}
      />
      <Button size="sm" variant="secondary" className="self-start" onClick={() => setSeed((s) => s + 1)}>
        New data
      </Button>
    </div>
  )
}

Line and area

One series; switch type or data.

Weekly active users, in thousands
monthActive users
Jan31k
Feb34k
Mar46k
Apr58k
May65k
Jun73k
Example
function ChartTrend() {
  const [type, setType] = useState<"line" | "area">("area")
  const [seed, setSeed] = useState(1)
  return (
    <div className="flex w-full flex-col gap-stack-sm">
      <Chart
        type={type}
        label="Weekly active users, in thousands"
        data={revenue(seed).map((row) => ({ month: row.month, users: row.new + row.returning }))}
        xKey="month"
        series={[{ key: "users", label: "Active users" }]}
        format={(n) => `${n}k`}
      />
      <div className="flex gap-2">
        <Button size="sm" variant="secondary" onClick={() => setType((t) => (t === "line" ? "area" : "line"))}>
          Show {type === "line" ? "area" : "line"}
        </Button>
        <Button size="sm" variant="ghost" onClick={() => setSeed((s) => s + 1)}>
          New data
        </Button>
      </div>
    </div>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/chart.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/chart.tsxchart
      components/ui/chart.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      
      /*
        Chart: bar, line and area charts drawn in SVG with Isotopi tokens
        (--chart-series-1..5 map to --color-chart-1..5). On first view bars grow
        from the baseline with a stagger, lines draw left to right and areas rise.
        New data morphs from the old values. The tooltip follows on a spring and
        snaps to the nearest point; arrow keys move it too. Calm: shorter, no
        spring. Still: static. A hidden table carries the data for screen readers.
      */
      export type ChartSeries = { key: string; label: string }
      type Row = Record<string, string | number>
      
      const colours = ["var(--chart-series-1)", "var(--chart-series-2)", "var(--chart-series-3)", "var(--chart-series-4)", "var(--chart-series-5)"]
      
      const PAD = { top: 12, right: 12, bottom: 28, left: 44 }
      
      function niceStep(raw: number) {
        const power = 10 ** Math.floor(Math.log10(raw || 1))
        const unit = raw / power
        return (unit <= 1 ? 1 : unit <= 2 ? 2 : unit <= 5 ? 5 : 10) * power
      }
      
      function Chart({
        data,
        xKey,
        series,
        type = "bar",
        format = (n) => n.toLocaleString(),
        label,
        summary,
        className,
      }: {
        data: Row[]
        /** Field for the x-axis labels. */
        xKey: string
        /** Up to 5 numeric fields, drawn in --chart-series-1..5. */
        series: ChartSeries[]
        type?: "bar" | "line" | "area"
        format?: (value: number) => string
        /** Accessible name. */
        label: string
        /** One-sentence takeaway, read before the data. */
        summary?: string
        className?: string
      }) {
        const ref = React.useRef<HTMLDivElement>(null)
        const [box, setBox] = React.useState({ w: 0, h: 0 })
        const [seen, setSeen] = React.useState(false)
        const [active, setActive] = React.useState<number | null>(null)
        // Announce values only when moving with the keyboard, not on every hover.
        const [keyed, setKeyed] = React.useState(false)
        const summaryId = React.useId()
      
        React.useEffect(() => {
          const node = ref.current
          if (!node) return
          const resize = new ResizeObserver(([entry]) => setBox({ w: entry.contentRect.width, h: entry.contentRect.height }))
          resize.observe(node)
          const io = new IntersectionObserver(([entry]) => {
            if (entry.isIntersecting) {
              setSeen(true)
              io.disconnect()
            }
          }, { threshold: 0.3 })
          io.observe(node)
          return () => {
            resize.disconnect()
            io.disconnect()
          }
        }, [])
      
        const shown = series.slice(0, 5)
        const n = data.length
        const max = Math.max(0, ...data.flatMap((row) => shown.map((s) => Number(row[s.key]) || 0)))
        const step = niceStep(max / 4)
        const top = Math.max(step, Math.ceil(max / step) * step)
        const ticks = Array.from({ length: Math.round(top / step) + 1 }, (_, i) => i * step)
        const innerW = Math.max(0, box.w - PAD.left - PAD.right)
        const innerH = Math.max(0, box.h - PAD.top - PAD.bottom)
        const yOf = (v: number) => PAD.top + innerH - (v / top) * innerH
        const band = n ? innerW / n : 0
        const xOf = (i: number) => (type === "bar" ? PAD.left + band * i + band / 2 : PAD.left + (n > 1 ? (innerW * i) / (n - 1) : innerW / 2))
        const base = PAD.top + innerH
      
        function nearest(clientX: number) {
          const rect = ref.current?.getBoundingClientRect()
          if (!rect || !n) return null
          const x = clientX - rect.left - PAD.left
          const i = type === "bar" ? Math.floor(x / band) : Math.round((x / innerW) * (n - 1))
          return Math.max(0, Math.min(n - 1, i))
        }
      
        const tipX = active === null ? 0 : Math.max(64, Math.min(box.w - 64, xOf(active)))
        const tipY = active === null ? 0 : Math.min(...shown.map((s) => yOf(Number(data[active][s.key]) || 0)))
      
        return (
          <figure data-slot="chart" className={cn("flex w-full flex-col gap-stack-sm", className)}>
            {summary ? (
              <figcaption id={summaryId} className="text-sm text-muted-foreground">
                {summary}
              </figcaption>
            ) : null}
            <div
              ref={ref}
              role="group"
              aria-label={`${label}. Use the arrow keys to read values.`}
              aria-describedby={summary ? summaryId : undefined}
              tabIndex={0}
              data-seen={seen || undefined}
              data-type={type}
              className="chart relative h-(--chart-height) w-full rounded-(--shape-sm) outline-none focus-visible:shadow-(--focus-ring)"
              onPointerMove={(event) => {
                setKeyed(false)
                setActive(nearest(event.clientX))
              }}
              onPointerLeave={() => setActive(null)}
              onFocus={() => setActive((a) => a ?? 0)}
              onBlur={() => setActive(null)}
              onKeyDown={(event) => {
                if (event.key === "ArrowRight" || event.key === "ArrowLeft") {
                  event.preventDefault()
                  setKeyed(true)
                  setActive((a) => Math.max(0, Math.min(n - 1, (a ?? 0) + (event.key === "ArrowRight" ? 1 : -1))))
                } else if (event.key === "Escape") setActive(null)
              }}
            >
              {box.w > 0 ? (
                <svg aria-hidden width={box.w} height={box.h} className="absolute inset-0 overflow-visible">
                  {ticks.map((tick) => (
                    <g key={tick}>
                      <line x1={PAD.left} x2={box.w - PAD.right} y1={yOf(tick)} y2={yOf(tick)} className="chart-grid" />
                      <text x={PAD.left - 8} y={yOf(tick)} textAnchor="end" dominantBaseline="middle" className="chart-axis">
                        {format(tick)}
                      </text>
                    </g>
                  ))}
                  {data.map((row, i) => (
                    <text key={i} x={xOf(i)} y={box.h - 8} textAnchor="middle" className="chart-axis">
                      {String(row[xKey])}
                    </text>
                  ))}
                  {active !== null ? (
                    type === "bar" ? (
                      <rect x={PAD.left + band * active} y={PAD.top} width={band} height={innerH} className="chart-band" />
                    ) : (
                      <line x1={xOf(active)} x2={xOf(active)} y1={PAD.top} y2={base} className="chart-crosshair" />
                    )
                  ) : null}
                  {type === "bar"
                    ? data.map((row, i) => {
                        const group = band * 0.64
                        const w = group / shown.length
                        return shown.map((s, k) => {
                          const v = Number(row[s.key]) || 0
                          const y = yOf(v)
                          return (
                            <rect
                              key={`${i}-${s.key}`}
                              className="chart-bar"
                              x={PAD.left + band * i + (band - group) / 2 + w * k + 1}
                              width={Math.max(1, w - 2)}
                              y={y}
                              height={base - y}
                              style={{ y, height: base - y, fill: colours[k], "--i": i } as React.CSSProperties}
                            />
                          )
                        })
                      })
                    : shown.map((s, k) => {
                        const points = data.map((row, i) => `${xOf(i)},${yOf(Number(row[s.key]) || 0)}`)
                        const line = `M${points.join(" L")}`
                        const area = `${line} L${xOf(n - 1)},${base} L${xOf(0)},${base} Z`
                        const colour = colours[k]
                        return (
                          <g key={s.key} style={{ "--i": k } as React.CSSProperties}>
                            {type === "area" ? <path d={area} className="chart-area" style={{ d: `path("${area}")`, fill: colour } as React.CSSProperties} /> : null}
                            <path d={line} pathLength={1} className="chart-line" style={{ d: `path("${line}")`, stroke: colour } as React.CSSProperties} />
                            {active !== null ? (
                              <circle cx={xOf(active)} cy={yOf(Number(data[active][s.key]) || 0)} r={4} className="chart-dot" style={{ fill: colour }} />
                            ) : null}
                          </g>
                        )
                      })}
                </svg>
              ) : null}
              {active !== null ? (
                <div
                  aria-hidden
                  className="chart-tooltip pointer-events-none absolute start-0 top-0 z-[1] min-w-28 rounded-(--chart-tooltip-corner) bg-(--chart-tooltip-container) px-3 py-2 text-xs text-(--chart-tooltip-label) shadow-(--chart-tooltip-elevation)"
                  style={{ transform: `translate(calc(${tipX}px - 50%), calc(${tipY}px - 100% - var(--space-3)))` }}
                >
                  <p className="mb-1 font-medium">{String(data[active][xKey])}</p>
                  {shown.map((s, k) => (
                    <p key={s.key} className="flex items-center gap-2">
                      <span className="size-2 rounded-full" style={{ background: colours[k] }} />
                      <span className="flex-1 opacity-80">{s.label}</span>
                      <span className="tabular-nums">{format(Number(data[active][s.key]) || 0)}</span>
                    </p>
                  ))}
                </div>
              ) : null}
              <span className="sr-only" aria-live="polite">
                {keyed && active !== null ? `${data[active][xKey]}: ${shown.map((s) => `${s.label} ${format(Number(data[active][s.key]) || 0)}`).join(", ")}` : ""}
              </span>
            </div>
            {shown.length > 1 ? (
              <ul className="flex flex-wrap gap-inline-md text-xs text-muted-foreground" aria-hidden>
                {shown.map((s, k) => (
                  <li key={s.key} className="flex items-center gap-1.5">
                    <span className="size-2 rounded-full" style={{ background: colours[k] }} />
                    {s.label}
                  </li>
                ))}
              </ul>
            ) : null}
            <table className="sr-only">
              <caption>{label}</caption>
              <thead>
                <tr>
                  <th scope="col">{xKey}</th>
                  {shown.map((s) => (
                    <th key={s.key} scope="col">
                      {s.label}
                    </th>
                  ))}
                </tr>
              </thead>
              <tbody>
                {data.map((row, i) => (
                  <tr key={i}>
                    <th scope="row">{String(row[xKey])}</th>
                    {shown.map((s) => (
                      <td key={s.key}>{format(Number(row[s.key]) || 0)}</td>
                    ))}
                  </tr>
                ))}
              </tbody>
            </table>
          </figure>
        )
      }
      
      export { Chart }
      

Imports

What the examples on this page import:

Imports
import { ArchiveIcon } from "lucide-react"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Chart } from "@/components/ui/chart"
import { DataTable, type DataTableColumn } from "@/components/ui/data-table"
import { InputOTP } from "@/components/ui/input-otp"
import { tokenMs } from "@/lib/motion/token-ms"

Anatomy

  1. 1Series · --chart-series-1 to --chart-series-5 (from --color-chart-1..5).
  2. 2Grid and axes · --chart-grid, --chart-grid-width, --chart-axis.
  3. 3Bars, lines, areas · --chart-bar-corner, --chart-line-width, --chart-area-opacity.
  4. 4Tooltip · --chart-tooltip-container, --chart-tooltip-label, --chart-tooltip-corner, --chart-tooltip-elevation; band --chart-band; crosshair --chart-crosshair.

Props

PropTypeDefaultDescription
data, xKeyRecord<string, string | number>[], string—Rows and the label field.
series{ key, label }[]—Up to 5 numeric fields.
type"bar" | "line" | "area""bar"Chart kind.
format(n) => string—Axis and tooltip values.
label, summarystring—Accessible name and a one-line takeaway.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--chart-heightcalc(var(--space-32) * 1.75)calc(var(--space-32) * 1.75)same
--chart-series-1var(--color-chart-1)oklch(0.55 0.04 250)oklch(0.7 0.04 250)
--chart-series-2var(--color-chart-2)oklch(0.62 0.02 95)oklch(0.75 0.02 95)
--chart-series-3var(--color-chart-3)oklch(0.48 0.03 150)oklch(0.68 0.04 150)
--chart-series-4var(--color-chart-4)oklch(0.7 0.02 95)oklch(0.8 0.02 95)
--chart-series-5var(--color-chart-5)oklch(0.4 0.02 260)oklch(0.62 0.02 260)
--chart-gridvar(--stroke-color-subtle)oklch(0.22 0.01 260 / 16%)oklch(1 0 0 / 8%)
--chart-grid-widthvar(--stroke-width-thin)1pxsame
--chart-axisvar(--on-surface-variant)oklch(0.45 0.01 95)oklch(0.68 0 0)
--chart-bar-cornervar(--shape-xs)6pxsame
--chart-line-widthvar(--stroke-width-thick)3pxsame
--chart-area-opacity0.180.18same
--chart-bandvar(--color-bg-hover)oklch(0.965 0.005 95)oklch(0.22 0 0)
--chart-crosshairvar(--stroke-color-default)oklch(0.22 0.01 260 / 18%)oklch(1 0 0 / 12%)
--chart-tooltip-containervar(--surface-inverse)oklch(0.22 0.01 260)oklch(0.95 0 0)
--chart-tooltip-labelvar(--on-surface-inverse)oklch(0.98 0.004 95)oklch(0.14 0 0)
--chart-tooltip-cornervar(--shape-sm)8pxsame
--chart-tooltip-elevationvar(--elevation-3)0 1px 1px -0.5px oklch(0.22 0.01 260 / 8%), 0 3px 3px -1.5px oklch(0.22 0.01 260 / 8%), 0 6px 6px -3px oklch(0.22 0.01 260 / 8%), 0 12px 12px -6px oklch(0.22 0.01 260 / 8%), 0 24px 24px -12px oklch(0.22 0.01 260 / 8%), 0 48px 48px -24px oklch(0.22 0.01 260 / 6%)0 8px 20px oklch(0 0 0 / 45%), 0 20px 44px oklch(0 0 0 / 40%)

Motion

TriggerWhat happensTokens
First viewBars grow with a stagger; lines draw left to right; areas rise.--duration-settle, --duration-signature
New dataValues morph to their new positions.--spring-snappy
Pointer / arrowsThe tooltip follows on a spring and snaps to the nearest point.--spring-snappy

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • A hidden table carries all the data; the summary is read first.
  • Focus the chart and use Left and Right to read values aloud.
  • Legend and tooltip name every series, so colour is never the only cue.

Usage

Do

  • Use for trends and comparisons with a clear takeaway.

Don’t

  • Don't use more than 5 series or a pie for many parts.

Code

components/ui/chart.tsx
"use client"

import * as React from "react"
import { cn } from "cn"

/*
  Chart: bar, line and area charts drawn in SVG with Isotopi tokens
  (--chart-series-1..5 map to --color-chart-1..5). On first view bars grow
  from the baseline with a stagger, lines draw left to right and areas rise.
  New data morphs from the old values. The tooltip follows on a spring and
  snaps to the nearest point; arrow keys move it too. Calm: shorter, no
  spring. Still: static. A hidden table carries the data for screen readers.
*/
export type ChartSeries = { key: string; label: string }
type Row = Record<string, string | number>

const colours = ["var(--chart-series-1)", "var(--chart-series-2)", "var(--chart-series-3)", "var(--chart-series-4)", "var(--chart-series-5)"]

const PAD = { top: 12, right: 12, bottom: 28, left: 44 }

function niceStep(raw: number) {
  const power = 10 ** Math.floor(Math.log10(raw || 1))
  const unit = raw / power
  return (unit <= 1 ? 1 : unit <= 2 ? 2 : unit <= 5 ? 5 : 10) * power
}

function Chart({
  data,
  xKey,
  series,
  type = "bar",
  format = (n) => n.toLocaleString(),
  label,
  summary,
  className,
}: {
  data: Row[]
  /** Field for the x-axis labels. */
  xKey: string
  /** Up to 5 numeric fields, drawn in --chart-series-1..5. */
  series: ChartSeries[]
  type?: "bar" | "line" | "area"
  format?: (value: number) => string
  /** Accessible name. */
  label: string
  /** One-sentence takeaway, read before the data. */
  summary?: string
  className?: string
}) {
  const ref = React.useRef<HTMLDivElement>(null)
  const [box, setBox] = React.useState({ w: 0, h: 0 })
  const [seen, setSeen] = React.useState(false)
  const [active, setActive] = React.useState<number | null>(null)
  // Announce values only when moving with the keyboard, not on every hover.
  const [keyed, setKeyed] = React.useState(false)
  const summaryId = React.useId()

  React.useEffect(() => {
    const node = ref.current
    if (!node) return
    const resize = new ResizeObserver(([entry]) => setBox({ w: entry.contentRect.width, h: entry.contentRect.height }))
    resize.observe(node)
    const io = new IntersectionObserver(([entry]) => {
      if (entry.isIntersecting) {
        setSeen(true)
        io.disconnect()
      }
    }, { threshold: 0.3 })
    io.observe(node)
    return () => {
      resize.disconnect()
      io.disconnect()
    }
  }, [])

  const shown = series.slice(0, 5)
  const n = data.length
  const max = Math.max(0, ...data.flatMap((row) => shown.map((s) => Number(row[s.key]) || 0)))
  const step = niceStep(max / 4)
  const top = Math.max(step, Math.ceil(max / step) * step)
  const ticks = Array.from({ length: Math.round(top / step) + 1 }, (_, i) => i * step)
  const innerW = Math.max(0, box.w - PAD.left - PAD.right)
  const innerH = Math.max(0, box.h - PAD.top - PAD.bottom)
  const yOf = (v: number) => PAD.top + innerH - (v / top) * innerH
  const band = n ? innerW / n : 0
  const xOf = (i: number) => (type === "bar" ? PAD.left + band * i + band / 2 : PAD.left + (n > 1 ? (innerW * i) / (n - 1) : innerW / 2))
  const base = PAD.top + innerH

  function nearest(clientX: number) {
    const rect = ref.current?.getBoundingClientRect()
    if (!rect || !n) return null
    const x = clientX - rect.left - PAD.left
    const i = type === "bar" ? Math.floor(x / band) : Math.round((x / innerW) * (n - 1))
    return Math.max(0, Math.min(n - 1, i))
  }

  const tipX = active === null ? 0 : Math.max(64, Math.min(box.w - 64, xOf(active)))
  const tipY = active === null ? 0 : Math.min(...shown.map((s) => yOf(Number(data[active][s.key]) || 0)))

  return (
    <figure data-slot="chart" className={cn("flex w-full flex-col gap-stack-sm", className)}>
      {summary ? (
        <figcaption id={summaryId} className="text-sm text-muted-foreground">
          {summary}
        </figcaption>
      ) : null}
      <div
        ref={ref}
        role="group"
        aria-label={`${label}. Use the arrow keys to read values.`}
        aria-describedby={summary ? summaryId : undefined}
        tabIndex={0}
        data-seen={seen || undefined}
        data-type={type}
        className="chart relative h-(--chart-height) w-full rounded-(--shape-sm) outline-none focus-visible:shadow-(--focus-ring)"
        onPointerMove={(event) => {
          setKeyed(false)
          setActive(nearest(event.clientX))
        }}
        onPointerLeave={() => setActive(null)}
        onFocus={() => setActive((a) => a ?? 0)}
        onBlur={() => setActive(null)}
        onKeyDown={(event) => {
          if (event.key === "ArrowRight" || event.key === "ArrowLeft") {
            event.preventDefault()
            setKeyed(true)
            setActive((a) => Math.max(0, Math.min(n - 1, (a ?? 0) + (event.key === "ArrowRight" ? 1 : -1))))
          } else if (event.key === "Escape") setActive(null)
        }}
      >
        {box.w > 0 ? (
          <svg aria-hidden width={box.w} height={box.h} className="absolute inset-0 overflow-visible">
            {ticks.map((tick) => (
              <g key={tick}>
                <line x1={PAD.left} x2={box.w - PAD.right} y1={yOf(tick)} y2={yOf(tick)} className="chart-grid" />
                <text x={PAD.left - 8} y={yOf(tick)} textAnchor="end" dominantBaseline="middle" className="chart-axis">
                  {format(tick)}
                </text>
              </g>
            ))}
            {data.map((row, i) => (
              <text key={i} x={xOf(i)} y={box.h - 8} textAnchor="middle" className="chart-axis">
                {String(row[xKey])}
              </text>
            ))}
            {active !== null ? (
              type === "bar" ? (
                <rect x={PAD.left + band * active} y={PAD.top} width={band} height={innerH} className="chart-band" />
              ) : (
                <line x1={xOf(active)} x2={xOf(active)} y1={PAD.top} y2={base} className="chart-crosshair" />
              )
            ) : null}
            {type === "bar"
              ? data.map((row, i) => {
                  const group = band * 0.64
                  const w = group / shown.length
                  return shown.map((s, k) => {
                    const v = Number(row[s.key]) || 0
                    const y = yOf(v)
                    return (
                      <rect
                        key={`${i}-${s.key}`}
                        className="chart-bar"
                        x={PAD.left + band * i + (band - group) / 2 + w * k + 1}
                        width={Math.max(1, w - 2)}
                        y={y}
                        height={base - y}
                        style={{ y, height: base - y, fill: colours[k], "--i": i } as React.CSSProperties}
                      />
                    )
                  })
                })
              : shown.map((s, k) => {
                  const points = data.map((row, i) => `${xOf(i)},${yOf(Number(row[s.key]) || 0)}`)
                  const line = `M${points.join(" L")}`
                  const area = `${line} L${xOf(n - 1)},${base} L${xOf(0)},${base} Z`
                  const colour = colours[k]
                  return (
                    <g key={s.key} style={{ "--i": k } as React.CSSProperties}>
                      {type === "area" ? <path d={area} className="chart-area" style={{ d: `path("${area}")`, fill: colour } as React.CSSProperties} /> : null}
                      <path d={line} pathLength={1} className="chart-line" style={{ d: `path("${line}")`, stroke: colour } as React.CSSProperties} />
                      {active !== null ? (
                        <circle cx={xOf(active)} cy={yOf(Number(data[active][s.key]) || 0)} r={4} className="chart-dot" style={{ fill: colour }} />
                      ) : null}
                    </g>
                  )
                })}
          </svg>
        ) : null}
        {active !== null ? (
          <div
            aria-hidden
            className="chart-tooltip pointer-events-none absolute start-0 top-0 z-[1] min-w-28 rounded-(--chart-tooltip-corner) bg-(--chart-tooltip-container) px-3 py-2 text-xs text-(--chart-tooltip-label) shadow-(--chart-tooltip-elevation)"
            style={{ transform: `translate(calc(${tipX}px - 50%), calc(${tipY}px - 100% - var(--space-3)))` }}
          >
            <p className="mb-1 font-medium">{String(data[active][xKey])}</p>
            {shown.map((s, k) => (
              <p key={s.key} className="flex items-center gap-2">
                <span className="size-2 rounded-full" style={{ background: colours[k] }} />
                <span className="flex-1 opacity-80">{s.label}</span>
                <span className="tabular-nums">{format(Number(data[active][s.key]) || 0)}</span>
              </p>
            ))}
          </div>
        ) : null}
        <span className="sr-only" aria-live="polite">
          {keyed && active !== null ? `${data[active][xKey]}: ${shown.map((s) => `${s.label} ${format(Number(data[active][s.key]) || 0)}`).join(", ")}` : ""}
        </span>
      </div>
      {shown.length > 1 ? (
        <ul className="flex flex-wrap gap-inline-md text-xs text-muted-foreground" aria-hidden>
          {shown.map((s, k) => (
            <li key={s.key} className="flex items-center gap-1.5">
              <span className="size-2 rounded-full" style={{ background: colours[k] }} />
              {s.label}
            </li>
          ))}
        </ul>
      ) : null}
      <table className="sr-only">
        <caption>{label}</caption>
        <thead>
          <tr>
            <th scope="col">{xKey}</th>
            {shown.map((s) => (
              <th key={s.key} scope="col">
                {s.label}
              </th>
            ))}
          </tr>
        </thead>
        <tbody>
          {data.map((row, i) => (
            <tr key={i}>
              <th scope="row">{String(row[xKey])}</th>
              {shown.map((s) => (
                <td key={s.key}>{format(Number(row[s.key]) || 0)}</td>
              ))}
            </tr>
          ))}
        </tbody>
      </table>
    </figure>
  )
}

export { Chart }