# ChartCard

> A card for one chart with a period selector, a “view as table” toggle and PNG or SVG download. exportChart downloads any chart.

Status: beta. Category: Data display.

```ts
import { ChartCard } from '@glyphui/ui/chart'
```

Exports: `ChartCard`, `chartCardStyles`, `exportChart`, `serializeChart`.

## Notes

- The chart inside hides its caption (the card shows the title) but keeps it as its accessible name.
- The table view is a real, visible table of the same rows the chart plots, for anyone who wants exact numbers.
- Exports resolve every theme variable, so the PNG or SVG looks the same outside your app. Web fonts fall back to system fonts in PNGs.

## ChartCard props

| Prop | Type | Default | Description |
|---|---|---|---|
| `children` (required) | `ReactNode` |  | One chart from `@glyphui/ui/chart`. Its own title becomes the accessible name only. |
| `title` (required) | `ReactNode` |  |  |
| `actions` | `ReactNode` |  | Extra controls in the header, before the built-in buttons. |
| `defaultView` | `"table" \| "chart"` |  |  |
| `description` | `ReactNode` |  |  |
| `exportable` | `boolean \| { fileName?: string; }` |  | Adds a download menu (PNG and SVG). Default true. |
| `onExportError` | `((error: unknown) => void)` |  | Called when a download fails, e.g. to show a toast. Logs to the console by default. |
| `onViewChange` | `((view: "table" \| "chart") => void)` |  |  |
| `period` | `ChartPeriod<P>` |  | A period selector in the header, e.g. 7d / 30d / 90d. |
| `tableToggle` | `boolean` |  | Adds a button that shows the chart's data as a table. Default true. |
| `view` | `"table" \| "chart"` |  |  |

## Example: Dashboard card

Switch the period, view the numbers as a table, or download the chart.

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

type Period = '7d' | '30d' | '90d'

const lengths: Record<Period, number> = { '7d': 7, '30d': 30, '90d': 90 }

function series(days: number) {
  return Array.from({ length: days }, (_, i) => {
    const date = new Date(2026, 9, 16 - days + 1 + i)
    return {
      date: date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }),
      active: Math.round(2400 + i * (900 / days) + 160 * Math.sin(i / 2)),
      new: Math.round(310 + 60 * Math.cos(i / 3)),
    }
  })
}

export default function Example() {
  const [period, setPeriod] = useState<Period>('30d')
  return (
    <ChartCard
      className="w-full"
      title="Active users"
      description="Daily, compared with new signups"
      period={{
        value: period,
        onValueChange: setPeriod,
        options: [
          { value: '7d', label: '7d' },
          { value: '30d', label: '30d' },
          { value: '90d', label: '90d' },
        ],
      }}
    >
      <LineChart
        title="Active users"
        data={series(lengths[period])}
        index="date"
        height={220}
        series={[
          { key: 'active', label: 'Active' },
          { key: 'new', label: 'New' },
        ]}
      />
    </ChartCard>
  )
}
```

## Example: Export without a card

exportChart(element, { format }) bakes the theme’s colors into the file.

```tsx
import { Button } from '@glyphui/ui'
import { BarChart, exportChart } from '@glyphui/ui/chart'
import { Download } from 'lucide-react'
import { useRef } from 'react'

const data = [
  { quarter: 'Q1', bookings: 182000 },
  { quarter: 'Q2', bookings: 214000 },
  { quarter: 'Q3', bookings: 268000 },
  { quarter: 'Q4', bookings: 311000 },
]

export default function Example() {
  const chart = useRef<HTMLDivElement>(null)
  return (
    <div className="grid w-full gap-3">
      <div ref={chart}>
        <BarChart
          title="Bookings by quarter"
          data={data}
          index="quarter"
          height={200}
          valueFormatter={(v) => `$${Math.round(v / 1000)}k`}
          series={[{ key: 'bookings', label: 'Bookings' }]}
        />
      </div>
      <div className="flex gap-2">
        <Button
          variant="outline"
          size="sm"
          leftIcon={<Download />}
          onClick={() => exportChart(chart, { format: 'svg', fileName: 'bookings' })}
        >
          SVG
        </Button>
        <Button
          variant="outline"
          size="sm"
          leftIcon={<Download />}
          onClick={() => exportChart(chart, { format: 'png', fileName: 'bookings' })}
        >
          PNG
        </Button>
      </div>
    </div>
  )
}
```

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