<!-- generated by scripts/gen-docs.mjs from docs/system/chart-card.md. Edit the source and rerun. -->
# ChartCard

## Description
Frames one chart with what it is about: the title, the headline figure with its change, a note and one control, so a person knows what the picture shows before they read it.

`import { ChartCard } from "@/components/cobalt/ui/chart-card"`, source `registry/cobalt/ui/chart-card.tsx`, status `ready-with-gaps`.
Foundation: `Card (registry/cobalt/ui/card.tsx)`. Traps checked: `trap/loading-layout-shift`, `trap/color-only-status`, `trap/numbers-shift`, `trap/live-region-double` (n/a: the card announces nothing; the chart inside owns its live line), `trap/reduced-motion-ignored` (n/a: the card has no motion), `trap/motion-transition-all` (n/a: the card has no transition), `trap/motion-layout-property` (n/a: nothing animates), `trap/tooltip-essential` (n/a: the card has no tooltip), `trap/tooltip-interactive` (n/a: the card has no tooltip), `trap/touch-hover-flash` (n/a: nothing on the card reacts to hover), `trap/hover-unguarded` (n/a: no CSS hover reveals anything), `trap/demo-on-timers` (n/a: every example holds fixed data), `trap/illustration-announced` (n/a: the card holds text and the chart's own group), `trap/loop-offscreen` (n/a: nothing loops).

### Foundation
| Given by the foundation | Added by the team |
|---|---|
| `Card`: the surface (`bg-card`, `rounded-2xl`), the vertical padding and gap from `--card-spacing`, and `className` and the rest of the props passed through | The card rendered as a `section` with `min-w-0`; a header row a control high (`--control`) with or without an action, with the title as an `h2` in muted small text on one line, cut with an ellipsis and whole in the system tooltip only when the action leaves too little room, so the figures of cards side by side line up whatever their titles and controls, the headline figure in `text-2xl font-semibold tabular-nums` so its digits do not shift as the hovered point changes, the change as `ChartChange` (a sign and an arrow, never colour alone) beside it, a note line, and one `action` at the title's end, in a grid whose first row is the title and the action and whose next rows give the figure and the note the card's whole width, so a range picker never squeezes the headline; the figure, its chip and the note dim with the chart's old marks while a new range loads; a content area that holds the chart at the card's padding |

## Examples
A titled card with the latest month's figure, its change on a year before and a line chart under them, all read from the same rows.

Real uses, 0 call sites (`rg -n "<ChartCard\b" app docs/system/examples --glob '!docs/system/examples/chart-card/*'`): no showcase page draws one outside its examples yet, and the examples folder does not count.

Uses inside the registry: none beyond its tests. The examples of LineChart, BarChart, Histogram and WaterfallChart each put their chart in one.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/chart-card/default.tsx` | default | A titled card with the latest month's figure, its change on a year before and a line chart under them, all read from the same rows |
| Not applicable: up is the default, the rise in good colour in default.tsx | positive=up | A rise is good news |
| `docs/system/examples/chart-card/positive-down.tsx` | positive=down | A cost, where a fall is good news: the chip is in the good colour though it points down |
| `docs/system/examples/chart-card/in-grid.tsx` | composition:SegmentedControl | Two chart cards side by side on a dashboard; the second switches between weeks and months at once, from rows already loaded |
| `docs/system/examples/chart-card/state-pending.tsx` | state:pending | The range picker answers at once; the chart keeps its marks, dimmed after a short wait, until the new range arrives, then fades them in |

Example: A titled card with the latest month's figure, its change on a year before and a line chart under them, all read from the same rows (`docs/system/examples/chart-card/default.tsx`)

```tsx
import { ChartCard } from "@/components/cobalt/ui/chart-card";
import { LineChart } from "@/components/cobalt/ui/line-chart";
import { compactMoney } from "@/lib/cobalt/format";

// Gross assets at the end of each month, in billions of dollars.
const assets = [
  3.12, 3.18, 3.05, 3.21, 3.34, 3.29, 3.41, 3.38, 3.52, 3.47, 3.6, 3.71, 3.66, 3.58, 3.49, 3.62, 3.75, 3.81,
  3.79, 3.94, 4.02, 3.97, 4.11, 4.2, 4.16, 4.09, 4.25, 4.31, 4.28, 4.42, 4.51, 4.47, 4.39, 4.55, 4.63, 4.71,
];
const months = assets.map((value, i) => ({
  month: `${2023 + Math.floor(i / 12)}-${String((i % 12) + 1).padStart(2, "0")}-01`,
  assets: value * 1e9,
}));
const series = [{ key: "assets", label: "Gross assets", value: "assets" as const }];

const latest = months[months.length - 1]!.assets;
const yearBefore = months[months.length - 13]!.assets;

export default function Example() {
  return (
    <ChartCard
      className="w-full max-w-xl"
      title="Gross assets"
      value={compactMoney(latest)}
      change={((latest - yearBefore) / yearBefore) * 100}
      note={`Dec 2025, against ${compactMoney(yearBefore)} a year before`}
    >
      <LineChart label="Gross assets" height="sm" data={months} x="month" series={series} format="money" />
    </ChartCard>
  );
}
```

Example: Two chart cards side by side on a dashboard; the second switches between weeks and months at once, from rows already loaded (`docs/system/examples/chart-card/in-grid.tsx`)

```tsx
import { useState } from "react";

import { BarChart } from "@/components/cobalt/ui/bar-chart";
import { ChartCard } from "@/components/cobalt/ui/chart-card";
import { LineChart } from "@/components/cobalt/ui/line-chart";
import { SegmentedControl } from "@/components/cobalt/ui/segmented-control";
import { compactMoney, integer } from "@/lib/cobalt/format";

// Gross assets at the end of each month of the year to September 2026, in billions of dollars.
const assets = [4.12, 4.18, 4.05, 4.21, 4.34, 4.29, 4.41, 4.38, 4.52, 4.47, 4.6, 4.71].map((value, i) => ({
  month: new Date(Date.UTC(2025, 9 + i, 1)).toISOString().slice(0, 10),
  assets: value * 1e9,
}));
const assetSeries = [{ key: "assets", label: "Gross assets", value: "assets" as const }];

// Filings received in each of the last ten weeks and the last six months.
type Period = "week" | "month";
const filings: Record<Period, { at: string; filings: number }[]> = {
  week: [212, 236, 198, 251, 274, 229, 262, 288, 301, 277].map((count, i) => ({
    at: new Date(Date.UTC(2026, 7, 3) + i * 7 * 86_400_000).toISOString().slice(0, 10),
    filings: count,
  })),
  month: [1034, 968, 1121, 1087, 1176, 1112].map((count, i) => ({
    at: new Date(Date.UTC(2026, 3 + i, 1)).toISOString().slice(0, 10),
    filings: count,
  })),
};
const filingSeries = [{ key: "filings", label: "Filings", value: "filings" as const }];

const first = assets[0]!.assets;
const last = assets[assets.length - 1]!.assets;

export default function Example() {
  const [period, setPeriod] = useState<Period>("week");
  const rows = filings[period];
  const now = rows[rows.length - 1]!.filings;
  const before = rows[rows.length - 2]!.filings;
  return (
    <div className="grid w-full gap-4 md:grid-cols-2">
      <ChartCard
        title="Gross assets"
        value={compactMoney(last)}
        change={((last - first) / first) * 100}
        note="Over the year to September"
      >
        <LineChart label="Gross assets" height="sm" data={assets} x="month" series={assetSeries} format="money" />
      </ChartCard>
      <ChartCard
        title="Filings"
        value={integer(now)}
        change={((now - before) / before) * 100}
        note={period === "week" ? "Last week, against the week before" : "September, against August"}
        action={
          <SegmentedControl
            label="Period"
            value={period}
            onChange={setPeriod}
            options={[
              { value: "week", label: "Weeks" },
              { value: "month", label: "Months" },
            ]}
          />
        }
      >
        <BarChart label={`Filings per ${period}`} height="sm" data={rows} x="at" series={filingSeries} />
      </ChartCard>
    </div>
  );
}
```

## Variants

### positive
- `up`: a rise in the change is good news, the default, for assets, revenue and flows in. Used at 0 call sites.
- `down`: a fall in the change is good news, for costs, fees and outflows to be cut. Used at 0 call sites.

Example: A cost, where a fall is good news: the chip is in the good colour though it points down (`docs/system/examples/chart-card/positive-down.tsx`)

```tsx
import { BarChart } from "@/components/cobalt/ui/bar-chart";
import { ChartCard } from "@/components/cobalt/ui/chart-card";
import { compactMoney } from "@/lib/cobalt/format";

// Fees paid each quarter of 2025, in dollars.
const quarters = [
  { quarter: "Q1", fees: 4.6e5 },
  { quarter: "Q2", fees: 4.4e5 },
  { quarter: "Q3", fees: 4.3e5 },
  { quarter: "Q4", fees: 4.1e5 },
];
const series = [{ key: "fees", label: "Fees paid", value: "fees" as const }];

const latest = quarters[3]!.fees;
const before = quarters[2]!.fees;

export default function Example() {
  return (
    <ChartCard
      className="w-full max-w-sm"
      title="Fees paid"
      value={compactMoney(latest)}
      change={((latest - before) / before) * 100}
      positive="down"
      note={`Q4, against ${compactMoney(before)} in Q3`}
    >
      <BarChart label="Fees paid each quarter" height="sm" data={quarters} x="quarter" series={series} format="money" />
    </ChartCard>
  );
}
```

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| title only | no `value` | read the title, use the chart | the `h2` and the content; no figure and no chip | test |
| headline | `value` passed | read the figure | the figure in tabular numbers under the title | test |
| changed | `value` passed and `change` a finite number | read the chip | the sign and an arrow glyph with the percentage, such as "+6.1%" | test |
| no chip | `change` missing or not a number | read the figure alone | nothing where the chip would be | test |
| noted | `note` passed and not empty | read the note | a line of muted tabular text under the figure | test |
| action | `action` passed | use the control | the control at the title's end, kept at its own width | by hand |
| pending | `status="pending"` | wait | `aria-busy`; with no `value`, Skeleton lines hold the figure's and the note's lines; with a `value`, as when a range is switched, the figure, its chip and the note dim to 55% after 250ms, as the chart's old marks do, since all are the old range's | test |

Example: The range picker answers at once; the chart keeps its marks, dimmed after a short wait, until the new range arrives, then fades them in (`docs/system/examples/chart-card/state-pending.tsx`)

```tsx
import { useRef, useState } from "react";

import { ChartCard } from "@/components/cobalt/ui/chart-card";
import { LineChart } from "@/components/cobalt/ui/line-chart";
import { SegmentedControl } from "@/components/cobalt/ui/segmented-control";
import { pointDate } from "@/lib/cobalt/chart-math";
import { money } from "@/lib/cobalt/format";

type Range = "1y" | "3y" | "5y";
const months: Record<Range, number> = { "1y": 12, "3y": 36, "5y": 60 };

// Net asset value per unit at the end of each month, for the five years to September 2026.
const history = Array.from({ length: 60 }, (_, i) => ({
  month: new Date(Date.UTC(2021, 9 + i, 1)).toISOString().slice(0, 10),
  nav: Math.round((100 + i * 0.55 + 3.2 * Math.sin(i / 2.6) + 1.4 * Math.sin(i / 0.9)) * 100) / 100,
}));
type Row = (typeof history)[number];
const series = [{ key: "nav", label: "Value per unit", value: "nav" as const }];
const monthOf = (row: Row) => pointDate(Date.parse(row.month), 0, "en-US", "month");

// Stands in for your API call: it answers with the range's rows after 900ms.
const request = (range: Range) =>
  new Promise<Row[]>((resolve) =>
    setTimeout(() => {
      resolve(history.slice(-months[range]));
    }, 900),
  );

export default function Example() {
  const [range, setRange] = useState<Range>("1y");
  const [rows, setRows] = useState(() => history.slice(-months["1y"]));
  const [pending, setPending] = useState(false);
  const latest = useRef(0);
  const choose = (next: Range) => {
    setRange(next);
    setPending(true);
    const asked = ++latest.current;
    void request(next).then((answer) => {
      // Only the last range asked for lands.
      if (asked !== latest.current) return;
      setRows(answer);
      setPending(false);
    });
  };
  const start = rows[0]!;
  const end = rows[rows.length - 1]!;
  return (
    <ChartCard
      className="w-full max-w-xl"
      title="Value per unit"
      value={money(end.nav)}
      change={((end.nav - start.nav) / start.nav) * 100}
      note={`${monthOf(start)} to ${monthOf(end)}`}
      status={pending ? "pending" : "idle"}
      action={
        <SegmentedControl
          label="Range"
          value={range}
          onChange={choose}
          options={[
            { value: "1y", label: "1Y" },
            { value: "3y", label: "3Y" },
            { value: "5y", label: "5Y" },
          ]}
        />
      }
    >
      <LineChart
        label="Value per unit"
        height="sm"
        data={rows}
        x="month"
        series={series}
        format="money"
        status={pending ? "pending" : "idle"}
      />
    </ChartCard>
  );
}
```

### State precedence
- Change without a value: the missing value wins. The chip sits in the value row, so no chip shows.
- Title and action: both show. The title keeps to one line in the room the action leaves, cut with an ellipsis and whole in a tooltip when it does not fit; the figure and the note below keep the card's whole width, and sit at the same height in a card with no action.
- Changed and no chip: Not applicable: `change` is a finite number or it is not.

### Motion
| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| a pending figure passes the slow wait, and the new one arrives | announce | `duration-fade` on `--curve` | opacity, to 55% and back | the same fade |

## Props

<!-- Props generated from registry/cobalt/ui/chart-card.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `title` | `ReactNode` |  | What the chart shows: "Gross assets". |
| `action?` | `ReactNode` |  | One control for the chart, such as a range picker, at the title's end. |
| `change?` | `number` |  | The change in percent, signed. |
| `locale?` | `string` |  |  |
| `note?` | `ReactNode` |  | One line under the figure: the period, or what the hovered point is. |
| `positive?` | `"up" \| "down"` | `"up"` | Which direction is good news. |
| `status?` | `Status` | `"idle"` | The chart's status: while pending, the figure and the note hold their lines as placeholders. |
| `value?` | `ReactNode` |  | The headline figure, already formatted: "$5.2B". |

Also accepts every native `<div>` attribute.

`title` is what the chart shows, set in the `h2`. `value` is the headline figure, already formatted, such as "$4.7B". `change` is the change in percent, signed, and `positive` is `up` or `down`. `note` is one line under the figure. `action` is one control at the title's end. `status` is the chart's status: while it is `pending` the figure and the note keep their lines with a Skeleton each and the card carries `aria-busy`, so the card does not grow when the data lands. `locale` sets the number format of the chip. The rest of a `div`'s props pass to the section, and `children` is the chart. Pass `value`, `change` and `note` from the chart's `onActiveChange` and the headline follows the pointer without the card or the chart moving.

## Usage

### When to use
- A chart needs a name, a headline figure and its change beside it, such as gross assets with the year's change.
- Several charts sit in a dashboard grid, each framed the same way and holding its own size.

### When not to use
- One figure and its change, with no chart. Use Stat instead.
- A group of settings or a form needs a surface. Use Card instead.
- A page's rail needs a small labelled panel. Use RailCard instead.

### Rules
- `rule/chart-card-follow-the-hover`: When the headline should show the hovered point, pass `value`, `change` and `note` from the chart's `onActiveChange` instead of printing the point in a second readout beside the chart, because the figure is in tabular numbers and the note keeps its line, so the card and the chart do not move. Evidence: person D37; single use docs/system/examples/line-chart/in-chart-card.tsx:51. Check: review of every `ChartCard` that shows a hovered figure, for `onActiveChange` on its chart.
  - Don't: `<ChartCard title="Gross assets"><LineChart label="Gross assets" data={months} x="month" series={series} /><p>{hovered}</p></ChartCard>`
  - Do: `<ChartCard title="Gross assets" value={figure} note={when}><LineChart label="Gross assets" onActiveChange={setActive} data={months} x="month" series={series} /></ChartCard>`
- `rule/chart-card-note-never-empty`: When the note follows the hovered point, keep the `ChartCard` note a text at rest, such as the period, instead of clearing it to an empty string, because the card draws no note line for an empty `note`, and the card would lose a line when the pointer leaves. Evidence: person D37; single use registry/cobalt/ui/chart-card.tsx:88. Check: review of every `ChartCard` with a `note` that is set from state.
  - Don't: `<ChartCard title="Gross assets" note={active === null ? "" : date} />`
  - Do: `<ChartCard title="Gross assets" note={active === null ? "Latest month" : date} />`
- `rule/chart-card-positive-down-for-costs`: When the figure is a cost or a fee, set `positive="down"` instead of leaving the default, because the chip shows a fall in the good colour and a rise in the bad one only then, and a rise in costs is not good news. Evidence: person D13; principle wcag: 1.4.1 Use of color. Check: test `chart.test.tsx` expects a fall to be good when `positive="down"`, and the chip to keep its sign and arrow.
  - Don't: `<ChartCard title="Fees paid" value="$412K" change={-3.2} />`
  - Do: `<ChartCard title="Fees paid" value="$412K" change={-3.2} positive="down" />`
- `rule/chart-card-title-is-the-heading`: When a card is placed, pass the name of the measure as the `title` text instead of your own heading element, because the card renders the title as the `h2` of a `section`, and a heading inside it nests a second one. Evidence: principle wcag: 2.4.6 Headings and Labels; principle wcag: 1.3.1 Info and Relationships. Check: test `chart-card.test.tsx` expects the card to start with a `section` holding an `h2`.
  - Don't: `<ChartCard title={<h3>Gross assets</h3>} value="$4.7B" />`
  - Do: `<ChartCard title="Gross assets" value="$4.7B" />`

### Content
- Follows `rule/writing-no-invented-numbers`.
- `rule/chart-card-value-is-formatted`: When a headline figure is shown, pass `value` already formatted, such as "$4.7B" from `compactMoney`, instead of a raw number, because the card prints the node as given and the chart's axis and tooltip use the same `format`, so one figure reads one way. Evidence: principle heuristic: consistency and standards, one column of figures shares one unit; single use docs/system/examples/line-chart/in-chart-card.tsx:34. Check: test `chart-card.test.tsx` expects "$4.7B" in the value cell in tabular numbers.
  - Don't: `<ChartCard title="Gross assets" value={4700000000} />`
  - Do: `<ChartCard title="Gross assets" value={compactMoney(4.7e9)} />`

### Anti-slop
- `rule/chart-card-one-action`: When the chart needs a control, pass one `action` such as a range picker instead of a row of buttons, because the action sits at the title's end at its own width and the title keeps to one line in the room left, so a row of buttons cuts the title. Evidence: principle heuristic: aesthetic and minimalist design; single use registry/cobalt/ui/chart-card.tsx:77. Check: review of every `ChartCard` whose `action` holds more than one control.
  - Don't: `<ChartCard title="Gross assets" action={<><Button>1M</Button><Button>3M</Button><Button>1Y</Button></>} />`
  - Do: `<ChartCard title="Gross assets" action={<SegmentedControl label="Range" value={range} onChange={setRange} options={ranges} />} />`

### Limits
Not applicable: no measured limit. The title keeps to one line and is cut with an ellipsis past the room it has; the figure wraps inside the card (`min-w-0`); no run measured a length or a count past which the header breaks.

## Accessibility
Rests on a native `section` with an `h2`. The card has no keyboard of its own, and the chart and the action inside it keep their own tab stops. The arrow in the chip is `aria-hidden`, and the chip's text carries the sign. Screen reader pass with VoiceOver: NEEDS REVIEW.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the card | skips the card; the action, then the chart, take the stops they have | the action or the chart | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| card | section, generic: it carries no name, so it is not a landmark | nothing | none | nothing on its own | test |
| title | heading, level 2 | its text | none | the title as a heading in the outline | test |
| figure | none, text | its text | none | the figure read in place | test |
| change chip | none, text | its text, the sign and the percentage | the arrow glyph is `aria-hidden` | "+6.1%" read as text | test |
| note | none, text | its text | none | the note read in place | test |

## Tokens
| Part | State | Token |
|---|---|---|
| card | all | `--card`, `--card-foreground`, `--r-2xl`, `--card-spacing` |
| title, note | all | `--muted-foreground` |
| change chip, good | all | `--good` at 10% for the wash, `--good` for the text |
| change chip, bad | all | `--bad` at 10% for the wash, `--bad` for the text |
| change chip, no change | all | `--muted`, `--muted-foreground` |
| change chip radius | all | `--r-sm` |

## Related
- Chart: the frame every chart type draws in.
- Stat: one figure and its change, with no chart.
- Card: any other content that needs a surface.
- RailCard: a small labelled panel for a page's rail.
- Sparkline: a trend with no axes, beside a figure in a card of text.
