# Chart

Bar, line, area and pie charts drawn with the pen. One ink, so series are told apart by how they are coloured in: shaded, hatched, cross-hatched or dotted. No charting library.

Docs: https://ballpoint.st9wd.com/docs/chart

```bash
npx shadcn@latest add @ballpoint/chart
```

```tsx
import { BarChart, ChartContainer, type ChartConfig } from "@/components/ui/chart"

const config = {
  desktop: { label: "Desktop" },
  mobile: { label: "Mobile" },
} satisfies ChartConfig

<ChartContainer config={config} className="h-72 w-full">
  <BarChart data={data} xKey="month" label="Visitors by month" />
</ChartContainer>
```

Props:

- `ChartContainer config`: `Record<string, { label?, fill?, tone? }>`. Names the series. `fill` is shade, hatch, cross, dots or plain; `tone` is the pressure: ink, ink-2 or ink-3. By default each series takes the next style in turn.
- `data`: `Record<string, string | number>[]`. One row for each point along the bottom.
- `xKey`: `string`. The key in each row that names it.
- `series`: `string[]`. Which keys to plot. By default every key in the config.
- `BarChart stacked`: `boolean` (default `false`). Pile a row's series up instead of setting them side by side.
- `LineChart curve / dots`: `"natural" | "linear" / boolean` (default `"natural" / true`). Smoothed or straight lines (areas default to straight), and a ring at each point.
- `PieChart nameKey / valueKey / innerRadius`: `string / string / number` (default `"name" / "value" / 0.55`). Which keys name and size each slice; 0 is a whole pie, up to 0.9 a thin ring. Children are set in the middle.
- `format`: `(value: number) => string`. How values are written on the axis and in the tooltip.
- `tooltip / legend`: `boolean` (default `true`). Point at a chart to read it; a key to the series under it.
- `label`: `string`. A name for the chart. It also adds a table of the data, hidden, for screen readers.
- `seed`: `string | number`. Pins the drawing.

Pen settings: `draw`, `roughness`, `weight`, `speed` (see Pen settings).

## Example

```tsx
import { BarChart, ChartContainer, type ChartConfig } from "@/components/ui/chart";

const data = [
  { month: "Jan", desktop: 186, mobile: 80 },
  { month: "Feb", desktop: 305, mobile: 200 },
  { month: "Mar", desktop: 237, mobile: 120 },
  { month: "Apr", desktop: 173, mobile: 190 },
  { month: "May", desktop: 209, mobile: 130 },
  { month: "Jun", desktop: 214, mobile: 140 },
];

const config = {
  desktop: { label: "Desktop" },
  mobile: { label: "Mobile" },
} satisfies ChartConfig;

export default function ChartDemo() {
  return (
    <ChartContainer config={config} className="h-72 w-full max-w-xl">
      <BarChart data={data} xKey="month" label="Visitors by month, desktop and mobile" seed="chart-demo" />
    </ChartContainer>
  );
}
```

## Stacked bars

A row's series piled on top of one another.

```tsx
import { BarChart, ChartContainer, type ChartConfig } from "@/components/ui/chart";

const data = [
  { day: "Mon", sketches: 12, letters: 6, lists: 3 },
  { day: "Tue", sketches: 9, letters: 8, lists: 5 },
  { day: "Wed", sketches: 15, letters: 4, lists: 2 },
  { day: "Thu", sketches: 7, letters: 10, lists: 6 },
  { day: "Fri", sketches: 11, letters: 5, lists: 4 },
];

const config = {
  sketches: { label: "Sketches" },
  letters: { label: "Letters" },
  lists: { label: "Lists" },
} satisfies ChartConfig;

export default function ChartStacked() {
  return (
    <ChartContainer config={config} className="h-72 w-full max-w-xl">
      <BarChart data={data} xKey="day" stacked label="Pages written each day, by kind" seed="chart-stacked" />
    </ChartContainer>
  );
}
```

## Lines

A hand-smoothed line for each series, with a ring at every point.

```tsx
import { ChartContainer, LineChart, type ChartConfig } from "@/components/ui/chart";

const data = [
  { month: "Jan", ink: 120, pencil: 40 },
  { month: "Feb", ink: 180, pencil: 55 },
  { month: "Mar", ink: 160, pencil: 90 },
  { month: "Apr", ink: 240, pencil: 70 },
  { month: "May", ink: 210, pencil: 130 },
  { month: "Jun", ink: 290, pencil: 110 },
];

const config = {
  ink: { label: "Ink" },
  pencil: { label: "Pencil", tone: "ink-3" },
} satisfies ChartConfig;

export default function ChartLine() {
  return (
    <ChartContainer config={config} className="h-72 w-full max-w-xl">
      <LineChart data={data} xKey="month" label="Pages by month, in ink and in pencil" seed="chart-line" />
    </ChartContainer>
  );
}
```

## Area

A line with the ground under it hatched in.

```tsx
import { AreaChart, ChartContainer, type ChartConfig } from "@/components/ui/chart";

const data = [
  { week: "W1", pages: 14 },
  { week: "W2", pages: 22 },
  { week: "W3", pages: 19 },
  { week: "W4", pages: 31 },
  { week: "W5", pages: 27 },
  { week: "W6", pages: 38 },
  { week: "W7", pages: 34 },
];

const config = { pages: { label: "Pages", fill: "hatch" } } satisfies ChartConfig;

export default function ChartArea() {
  return (
    <ChartContainer config={config} className="h-72 w-full max-w-xl">
      <AreaChart data={data} xKey="week" label="Pages written each week" seed="chart-area" />
    </ChartContainer>
  );
}
```

## Pie and ring

A ring with the total written in its middle.

```tsx
import { ChartContainer, PieChart, type ChartConfig } from "@/components/ui/chart";

const data = [
  { name: "Sketches", value: 275 },
  { name: "Letters", value: 200 },
  { name: "Lists", value: 187 },
  { name: "Recipes", value: 123 },
];

const config = {
  Sketches: { label: "Sketches" },
  Letters: { label: "Letters" },
  Lists: { label: "Lists" },
  Recipes: { label: "Recipes" },
} satisfies ChartConfig;

export default function ChartPie() {
  const total = data.reduce((sum, row) => sum + row.value, 0);
  return (
    <ChartContainer config={config} className="h-80 w-full max-w-sm">
      <PieChart data={data} label="Pages by kind" seed="chart-pie">
        <div className="text-4xl font-bold tabular-nums">{total}</div>
        <div className="text-sm text-ink-3">pages</div>
      </PieChart>
    </ChartContainer>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
