# Chart

> Line, area, bar and composed charts with interactive legends, reference lines, zoom, and loading, error and empty states, all colored by the theme.

Status: beta. Category: Data display.

```ts
import { LineChart, AreaChart, BarChart, ComposedChart, ChartLegend, ChartTooltipCard } from '@glyphui/ui/chart'
```

Exports: `LineChart`, `AreaChart`, `BarChart`, `ComposedChart`, `ChartLegend`, `ChartTooltipCard`, `chartStyles`.

## Notes

- Install the optional peer dependency first: npm install recharts.
- Series colors default to the theme chart palette (chart-1 to chart-5), in order; pass a theme color name or any CSS color to override. Keep the same color for the same thing across charts.
- Set the palette in your theme file: colors: { light: { "chart-1": "oklch(55% 0.2 30)" } }. A theme without chart colors falls back to primary, info, success, warning and danger.
- With more than one series the legend is interactive: click or press Enter on an entry to hide its series. hiddenSeries and onHiddenSeriesChange control it.
- Entry and update animations follow the theme motion level and turn off for visitors who prefer reduced motion.
- Every chart is a labelled figure with a visually hidden data table, so screen-reader users get the numbers. Wrap a chart in ChartCard to let everyone switch to the table.
- The brush is a pointer control. Keyboard and screen-reader users get every row from the data table; wrap the chart in ChartCard to make that table visible.
- ComposedChart can put a series on a right-hand axis (axis: "right"). Two scales on one plot are easy to misread; prefer two charts with the same syncId when you can.

## LineChart props

| Prop | Type | Default | Description |
|---|---|---|---|
| `data` (required) | `readonly T[]` |  |  |
| `index` (required) | `string` |  | Field used for the x axis, e.g. `"month"`. |
| `series` (required) | `readonly ChartSeries<T>[]` |  |  |
| `title` (required) | `string` |  | Accessible name; also shown as the caption unless `hideTitle` is set. |
| `brush` | `boolean \| ChartBrushOptions` |  | Adds a range slider under the plot to zoom into part of the data. |
| `className` | `string` |  |  |
| `curve` | `"step" \| "monotone" \| "linear"` |  |  |
| `defaultHiddenSeries` | `readonly Extract<keyof T, string>[]` |  |  |
| `description` | `ReactNode` |  |  |
| `dots` | `boolean` |  | Show a dot on every data point. Default false. |
| `empty` | `ReactNode` |  | Shown when there is no data. Defaults to a short "No data" message. |
| `error` | `unknown` |  | Shows an error message in place of the plot, with a retry button when `onRetry` is set. |
| `height` | `number` |  | Plot height in pixels. Default 240. |
| `hiddenSeries` | `readonly Extract<keyof T, string>[]` |  | Series hidden through the legend (controlled). |
| `hideTitle` | `boolean` |  |  |
| `labelFormatter` | `((label: ReactNode) => ReactNode)` |  | Formats the x value at the top of the tooltip. |
| `loading` | `boolean` |  | Shows a skeleton in place of the plot. |
| `onHiddenSeriesChange` | `((hidden: Extract<keyof T, string>[]) => void)` |  |  |
| `onPointClick` | `((row: T, seriesKey?: Extract<keyof T, string>) => void)` |  | Called with the row (and series, when known) the user clicked. |
| `onRetry` | `(() => unknown)` |  |  |
| `referenceAreas` | `readonly ChartReferenceArea[]` |  | Shaded ranges, e.g. a launch window or an incident. |
| `referenceLines` | `readonly ChartReferenceLine[]` |  | Targets, limits, averages... drawn across the plot. |
| `showGrid` | `boolean` |  |  |
| `showLegend` | `boolean` |  | Defaults to true when there is more than one series. |
| `showYAxis` | `boolean` |  | Shorthand for `yAxis={{ hide: !showYAxis }}`. |
| `syncId` | `string` |  | Charts with the same `syncId` share their tooltip and brush position. |
| `valueFormatter` | `((value: number) => string)` |  | Formats values on the y axis, in tooltips and in the accessible table. |
| `xAxis` | `ChartAxisOptions` |  |  |
| `yAxis` | `ChartAxisOptions` |  |  |

## Example: Line

Two series with a value formatter.

```tsx
import { LineChart } from '@glyphui/ui/chart'

const data = [
  { month: 'Jan', revenue: 18200, costs: 12100 },
  { month: 'Feb', revenue: 21400, costs: 12800 },
  { month: 'Mar', revenue: 20100, costs: 13400 },
  { month: 'Apr', revenue: 26800, costs: 14100 },
  { month: 'May', revenue: 31200, costs: 15000 },
  { month: 'Jun', revenue: 34900, costs: 15600 },
]

const usd = (v: number) => `$${(v / 1000).toFixed(0)}k`

export default function Example() {
  return (
    <LineChart
      className="w-full"
      title="Revenue and costs, first half"
      data={data}
      index="month"
      series={[
        { key: 'revenue', label: 'Revenue' },
        { key: 'costs', label: 'Costs', color: 'muted-foreground' },
      ]}
      valueFormatter={usd}
    />
  )
}
```

## Example: Stacked area

Stacked charts add a total row to the tooltip.

```tsx
import { AreaChart } from '@glyphui/ui/chart'

const data = [
  { week: 'W1', organic: 1200, paid: 640, referral: 310 },
  { week: 'W2', organic: 1380, paid: 720, referral: 290 },
  { week: 'W3', organic: 1510, paid: 690, referral: 380 },
  { week: 'W4', organic: 1720, paid: 810, referral: 420 },
  { week: 'W5', organic: 1890, paid: 760, referral: 470 },
]

export default function Example() {
  return (
    <AreaChart
      className="w-full"
      title="Signups by source"
      description="Weekly, last 5 weeks"
      data={data}
      index="week"
      stacked
      series={[
        { key: 'organic', label: 'Organic' },
        { key: 'paid', label: 'Paid' },
        { key: 'referral', label: 'Referral' },
      ]}
    />
  )
}
```

## Example: Bar

```tsx
import { BarChart } from '@glyphui/ui/chart'

const data = [
  { day: 'Mon', opened: 42, closed: 38 },
  { day: 'Tue', opened: 51, closed: 47 },
  { day: 'Wed', opened: 39, closed: 44 },
  { day: 'Thu', opened: 58, closed: 50 },
  { day: 'Fri', opened: 33, closed: 41 },
]

export default function Example() {
  return (
    <BarChart
      className="w-full"
      title="Issues opened and closed"
      data={data}
      index="day"
      height={220}
      series={[
        { key: 'opened', label: 'Opened' },
        { key: 'closed', label: 'Closed', color: 'success' },
      ]}
    />
  )
}
```

## Example: Horizontal bars

layout="vertical" for long category names.

```tsx
import { BarChart } from '@glyphui/ui/chart'

const data = [
  { page: '/pricing', views: 18420 },
  { page: '/docs/getting-started', views: 12950 },
  { page: '/blog/usage-billing', views: 8730 },
  { page: '/changelog', views: 5210 },
  { page: '/integrations/stripe', views: 3980 },
]

export default function Example() {
  return (
    <BarChart
      className="w-full"
      title="Top pages"
      description="Views, last 30 days"
      data={data}
      index="page"
      layout="vertical"
      height={230}
      xAxis={{ width: 168 }}
      series={[{ key: 'views', label: 'Views' }]}
    />
  )
}
```

## Example: Composed

Bars and a line on one plot. Click a legend entry to hide its series.

```tsx
import { ComposedChart } from '@glyphui/ui/chart'

const data = [
  { month: 'May', revenue: 18200, target: 20000 },
  { month: 'Jun', revenue: 21400, target: 22000 },
  { month: 'Jul', revenue: 23100, target: 24000 },
  { month: 'Aug', revenue: 26800, target: 26000 },
  { month: 'Sep', revenue: 31200, target: 28000 },
  { month: 'Oct', revenue: 34900, target: 30000 },
]

const usd = (v: number) => `$${(v / 1000).toFixed(0)}k`

export default function Example() {
  return (
    <ComposedChart
      className="w-full"
      title="Revenue against plan"
      data={data}
      index="month"
      valueFormatter={usd}
      series={[
        { key: 'revenue', label: 'Revenue', type: 'bar' },
        { key: 'target', label: 'Plan', type: 'line', color: 'muted-foreground' },
      ]}
    />
  )
}
```

## Example: Targets and annotations

referenceLines for goals and limits, referenceAreas for periods.

```tsx
import { LineChart } from '@glyphui/ui/chart'

const data = [
  { day: 'Oct 1', p95: 182 },
  { day: 'Oct 2', p95: 176 },
  { day: 'Oct 3', p95: 210 },
  { day: 'Oct 4', p95: 334 },
  { day: 'Oct 5', p95: 298 },
  { day: 'Oct 6', p95: 191 },
  { day: 'Oct 7', p95: 168 },
  { day: 'Oct 8', p95: 172 },
]

export default function Example() {
  return (
    <LineChart
      className="w-full"
      title="API latency"
      description="p95 response time, milliseconds"
      data={data}
      index="day"
      dots
      valueFormatter={(v) => `${v} ms`}
      series={[{ key: 'p95', label: 'p95' }]}
      referenceLines={[{ y: 250, label: 'SLO 250 ms', color: 'danger' }]}
      referenceAreas={[{ x1: 'Oct 4', x2: 'Oct 5', label: 'Incident #4182' }]}
    />
  )
}
```

## Example: Zoom with a brush

Drag the handles under the plot to focus on a range.

```tsx
import { AreaChart } from '@glyphui/ui/chart'

// Ninety days of signups with a weekly rhythm and steady growth.
const data = Array.from({ length: 90 }, (_, i) => {
  const date = new Date(2026, 6, 18 + i)
  const weekday = date.getDay()
  const weekend = weekday === 0 || weekday === 6 ? 0.55 : 1
  return {
    date: date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }),
    signups: Math.round((120 + i * 1.6 + 18 * Math.sin(i / 3)) * weekend),
  }
})

export default function Example() {
  return (
    <AreaChart
      className="w-full"
      title="Daily signups"
      description="Last 90 days"
      data={data}
      index="date"
      height={260}
      series={[{ key: 'signups', label: 'Signups' }]}
      brush={{ startIndex: 60 }}
    />
  )
}
```

## Example: Loading, error and empty

The same props as every Glyph data component.

```tsx
import { SegmentedControl } from '@glyphui/ui'
import { LineChart } from '@glyphui/ui/chart'
import { useState } from 'react'

const data = [
  { month: 'Jul', mrr: 23100 },
  { month: 'Aug', mrr: 26800 },
  { month: 'Sep', mrr: 31200 },
  { month: 'Oct', mrr: 34900 },
]

type State = 'loading' | 'error' | 'empty' | 'ready'

export default function Example() {
  const [state, setState] = useState<State>('loading')
  return (
    <div className="grid w-full gap-4">
      <SegmentedControl<State>
        aria-label="State"
        size="sm"
        className="justify-self-start"
        value={state}
        onValueChange={setState}
        options={[
          { value: 'loading', label: 'Loading' },
          { value: 'error', label: 'Error' },
          { value: 'empty', label: 'Empty' },
          { value: 'ready', label: 'Ready' },
        ]}
      />
      <LineChart
        title="Monthly recurring revenue"
        data={state === 'empty' ? [] : data}
        index="month"
        series={[{ key: 'mrr', label: 'MRR' }]}
        valueFormatter={(v) => `$${(v / 1000).toFixed(1)}k`}
        height={200}
        loading={state === 'loading'}
        error={state === 'error' ? new Error('The metrics service timed out.') : undefined}
        onRetry={() => setState('ready')}
      />
    </div>
  )
}
```

## Example: Live data

Pass new data and the series animate to it, at the theme’s motion level.

```tsx
import { Button } from '@glyphui/ui'
import { LineChart } from '@glyphui/ui/chart'
import { useEffect, useState } from 'react'

const start = [42, 45, 44, 48, 51, 49, 53, 56, 55, 58, 61, 59]

function next(points: { t: number; rps: number }[]) {
  const last = points[points.length - 1]!
  const drift = Math.sin(last.t / 2) * 3
  return [...points.slice(1), { t: last.t + 1, rps: Math.max(20, Math.round(last.rps + drift)) }]
}

export default function Example() {
  const [running, setRunning] = useState(false)
  const [points, setPoints] = useState(() => start.map((rps, t) => ({ t, rps })))

  useEffect(() => {
    if (!running) return
    const timer = setInterval(() => setPoints(next), 1200)
    return () => clearInterval(timer)
  }, [running])

  return (
    <div className="grid w-full gap-3">
      <Button
        variant="outline"
        size="sm"
        className="justify-self-start"
        onClick={() => setRunning((r) => !r)}
      >
        {running ? 'Pause stream' : 'Start stream'}
      </Button>
      <LineChart
        title="Requests per second"
        data={points.map((p) => ({ ...p, label: `${p.t}s` }))}
        index="label"
        height={200}
        series={[{ key: 'rps', label: 'Requests/s' }]}
        yAxis={{ domain: [0, 'auto'] }}
      />
    </div>
  )
}
```

Live page: https://glyphui.xyz/components/chart
