Line, area and bar charts drawn as inline SVG on the server. Data comes through props, colors from the --chart-1..5 theme variables, hovering or focusing a point or bar shows a small tooltip next to it, rendered on the server and revealed by CSS, and a hidden table repeats the data for screen readers. No JavaScript, no charting library.
Preview
- Desktop
- Mobile
| Month | Desktop | Mobile |
|---|---|---|
| Jan | 186 | 80 |
| Feb | 305 | 200 |
| Mar | 237 | 120 |
| Apr | 273 | 190 |
| May | 209 | 130 |
| Jun | 314 | 140 |
Installation
Usage
import { Chart } from "@/components/ui/chart"
export default function Example() {
return (
<Chart
type="line"
title="Visitors per month"
categories={["Jan", "Feb", "Mar", "Apr", "May", "Jun"]}
series={[
{ name: "Desktop", values: [186, 305, 237, 273, 209, 314] },
{ name: "Mobile", values: [80, 200, 120, 190, 130, 140] },
]}
/>
)
}Examples
Area
Same data as a filled area with a linear curve.
- Desktop
- Mobile
| Month | Desktop | Mobile |
|---|---|---|
| Jan | 186 | 80 |
| Feb | 305 | 200 |
| Mar | 237 | 120 |
| Apr | 273 | 190 |
| May | 209 | 130 |
| Jun | 314 | 140 |
<Chart type="area" curve="linear" categories={months} series={series} />Bar
Grouped bars; negative values go below the zero line.
- Desktop
- Mobile
| Month | Desktop | Mobile |
|---|---|---|
| Jan | 186 | 80 |
| Feb | 305 | -40 |
| Mar | 237 | 120 |
| Apr | 273 | 190 |
| May | 209 | -30 |
| Jun | 314 | 140 |
<Chart type="bar" categories={months} series={series} />Custom colors and formatting
color takes any CSS color and formatValue formats ticks, tooltips and the table.
| Month | Revenue |
|---|---|
| Jan | $12k |
| Feb | $18k |
| Mar | $15k |
| Apr | $24k |
| May | $31k |
| Jun | $28k |
<Chart
type="area"
categories={months}
series={[{ name: "Revenue", color: "var(--primary)", values: [12, 18, 15, 24, 31, 28] }]}
formatValue={(v) => `$${v}k`}
showLegend={false}
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| type | "line" | "area" | "bar" | "line" | Chart kind |
| categories | string[] | - | X axis labels |
| series | { name: string; values: (number | null)[]; color?: string }[] | - | Series: name, values (null leaves a gap) and optional color, default --chart-1..5 |
| curve | "linear" | "smooth" | "smooth" | Line and area curve |
| ticks | number | 5 | Approximate number of Y ticks |
| showGrid | boolean | true | Horizontal grid lines |
| showLegend | boolean | true | Legend under the chart |
| showDots | boolean | true | Dots on points of line and area |
| tooltip | boolean | true | Tooltip at each point or bar on hover and keyboard focus (Tab walks the points series by series) |
| formatValue | (value: number) => string | - | Formats ticks, tooltips and the table |
| title | string | "Chart" | Accessible name and caption of the hidden table |
| categoryHeader | string | "Category" | Header of the first column of the hidden table |
| width | number | 600 | SVG coordinate width; a smaller number gives larger text |
| height | number | 300 | SVG coordinate height; the box keeps width to height |
| duration | number | 900 | Draw-in animation in ms; 0 turns it off |