# Chart tooltip

Tooltip and legend content shared by every chart.

Import from `@a31labs/rax/chart`.

## Usage

```tsx
<ChartTooltip formatter={(value) => `${value} deploys`} />
// custom content: <ChartTooltip content={<MyTooltip />} /> and read colours with useChartConfig()
```

## Rules

A thin layer over plain Recharts (`recharts` ^3, an optional peer dependency: install it only in apps that chart). Apps keep writing `AreaChart`, `Bar`, `XAxis` and so on; Rax sizes, colours and styles them. The root `@a31labs/rax` entry never imports Recharts.

- `ChartContainer`: props `config`, `label` (required; the root is `role="figure"` with `aria-label`), `height` (px, default 240), one Recharts chart as the child, plus native div props. Wraps the chart in a `ResponsiveContainer` at full width. Grid lines are `line`, axis lines and tick lines are hidden, tick labels are ink-3 at 14px with tabular figures, the hover cursor is `line-strong` (a surface-3 band on bars), the active dot has a surface ring, and the focused chart (Recharts' keyboard layer) shows the focus ring. Empty `data` keeps the height. On the server it renders the empty container at its height (the width is unknown there), and the chart draws after mount, so the layout does not jump. Give every chart a text alternative nearby too: a summary sentence or a table.
- `ChartConfig`: `Record<seriesKey, { label, color? }>`. `color` is `"accent" | "accent-2" | "ok" | "warn" | "danger" | "neutral"`, a slot number `1`–`6`, or any CSS colour. If exactly one series has no colour, it takes the accent, so single-series charts carry the app's brand. Otherwise series without a colour take slots 1, 2, 3, … in config order; a seventh uncoloured series gets `neutral` (`edge`), never a repeated slot. Each key becomes `--rax-series-<key>` on the container (characters outside `[a-zA-Z0-9_-]` turn into `-`), a separate namespace from the slot tokens. Use `chartVar(key)` for marks: `<Area dataKey="requests" fill={chartVar("requests")} />`. With a pale accent (under 3:1 on the surface, such as a light yellow), colour a single series with a slot: `color: 1`.
- `ChartTooltip` (Recharts `Tooltip` with `ChartTooltipContent`) and `ChartTooltipContent` (`formatter(value, key)`, `hideLabel`): a surface panel with a `line-strong` border, radius 6px and the float shadow; the label, then one row per series with a 10px colour key, the config label in ink-2 and the value right-aligned in tabular figures. Text is always ink, never the series colour. A series missing from the config shows its raw key with a neutral key. Missing values show "–"; range values (`[low, high]`) show "low–high".
- `ChartLegend` and `ChartLegendContent`: a centred, wrapping row of 10px keys and config labels in ink-2.
- `useChartConfig()` returns the config, for custom tooltip or legend content. Passing `content` to `ChartTooltip` or `ChartLegend` replaces the config-aware content entirely; that is intended for custom content, which can read the config through this hook.

Mark props Recharts only takes as props: 2px lines `strokeWidth={2}` (Recharts defaults to 1; Rax does not force it in CSS, so an explicit width always wins), rounded bar ends `radius={[4, 4, 0, 0]}` (horizontal bars `[0, 4, 4, 0]`), and a 2px surface gap between adjacent fills (stacked bars, pie and radial segments) `stroke="var(--rax-surface)" strokeWidth={2}`. No dual-axis charts: two measures on different scales are two charts.

What Rax's CSS sets on Recharts parts (all under `.rax-chart`): tick label `fill` and `font-size` (ink-3, small, tabular figures), grid line `stroke` (`line`), axis lines and tick lines hidden, tooltip cursor `stroke` (`line-strong`) and `fill` (surface-3 on bars and sectors), active-dot `stroke` (surface), and the focus outline on the chart surface. These are CSS rules, so a rule with higher specificity (for example `.my-chart .rax-chart .recharts-cartesian-axis-line { display: block }`) wins, and a chart that must look different can sit in a wrapper without `.rax-chart`.

Every built file starts with `"use client"`. In apps with server components, write charts in a client component: functions such as `chartVar` cannot be called from a server component, and Recharts itself needs the browser to measure and draw.

Config keys that differ only in characters outside `[a-zA-Z0-9_-]` map to the same variable (`a.b` and `a b` are both `a-b`); keep keys distinct.

Categorical palette, `--rax-chart-1` … `--rax-chart-6`: fixed hues, independent of the accent, in fixed order, never cycled. Lightness alternates so neighbours stay apart without colour vision.

| Slot | Light | Dark |
|---|---|---|
| 1 | oklch(0.50 0.21 260) | oklch(0.56 0.20 260) |
| 2 | oklch(0.60 0.22 0) | oklch(0.55 0.22 0) |
| 3 | oklch(0.50 0.14 150) | oklch(0.67 0.18 150) |
| 4 | oklch(0.62 0.20 300) | oklch(0.66 0.18 300) |
| 5 | oklch(0.66 0.18 55) | oklch(0.58 0.16 60) |
| 6 | oklch(0.60 0.13 215) | oklch(0.66 0.13 215) |

In both modes every slot clears 3:1 on the surface, adjacent slots are at least 21 apart (OKLab ΔE ×100) and at least 11 apart under simulated protanopia and deuteranopia. Apps can override `--rax-chart-1` … `--rax-chart-6` in their own mode scopes; keep the order rules and re-check contrast when they do. Tailwind gets them as `chart-1` … `chart-6`. Status colours are not categorical slots: use `ok`, `warn` and `danger` only when the series means that status. With more than six series, fold the rest into "Other".

Page: https://rax.a31.at/components/chart-tooltip
