Keyframery

Chart

Bar, line, area and donut charts in plain SVG and CSS, no charting library: bars grow from the baseline one after another, lines draw themselves, rings sweep round, and every change of data tweens on a spring. The tooltip glides with rolling numbers, the legend folds series away, and the arrow keys walk the points.

See it as an AI agent does.

Resize preview. Focus the handle and use the left or right arrow keys. Hold Shift for larger steps.— px
Customize1 controls
Animation speed1×

Installation

npx shadcn@latest add @keyframery/chart

This adds components/keyframery/chart.tsx and its primitive to your project, and installs UI SDK. The motion tokens are added to your global CSS the first time you install a component.

Usage

import { Chart, type ChartConfig } from "@/components/keyframery/chart"

const data = [
  { month: "Jan", visitors: 1840 },
  { month: "Feb", visitors: 2120 },
  { month: "Mar", visitors: 1980 },
]

const config = { visitors: { label: "Visitors" } } satisfies ChartConfig

export default function Usage() {
  return <Chart aria-label="Visitors" data={data} config={config} categoryKey="month" />
}

Examples

Bar chart

Rows of data and a config naming each series, as in shadcn's charts. Colours come from --chart-1 to --chart-5 in config order; bars grow from the baseline and the tooltip follows the pointer or the arrow keys.

Line chart

type="line" draws each series from the left. curve="linear" joins the points straight, and dots marks every one.

Stacked areas

type="area" with stacked piles each series on the one below. The fill fades down from the line in the series' colour.

Donut

For a ring, each row is a slice: categoryKey names it and the one numeric key is its value. Label the slices in the config; the hole shows the total, or the slice under the pointer.

Horizontal bars

orientation="horizontal" lays the bars down, categories down the side. Good for long names; set your own colour per series in the config.

Interactive legend and readout

Compose the parts yourself to place them. onActiveIndexChange reports the point under the pointer or the arrow keys, and hiddenSeries with onHiddenSeriesChange lets the legend switch series off: they fold into the baseline while the others take their room.

Stacked bars

stacked puts each series on the one before it, in config order. On arrival every stack builds up from the baseline, segment by segment; the tooltip adds the total, and switching a series off in the legend re-stacks the rest.

Motion

MoveWhat you see
CascadeBars grow from the baseline one after another, left to right; a ring's slices sweep round from the top, and the others fade back from the slice under the pointer.
DrawLines draw themselves from the left, with their area unrolling under them, and their dots pop in after.
SpringNew data, stacking or hidden series tween every bar, line, slice, tick and grid line to its new place, with a small overshoot.
GlideThe cursor band or hairline, the tooltip and each line's dot travel to the active point, and the tooltip swings to the other side near the edge.
RollThe tooltip's values, the axis ticks and a donut's total roll to each new number.
StackStacked bars build up from the baseline, each segment growing once the one under it has; the tooltip ends with the stack's total.
HeightA series switched off in the legend folds into its baseline while the others widen into its room.
PressLegend items sink a little and pop back.

Timing and springiness follow the motion style you set. To change how fast this chart moves, pass speed:

<Chart speed="fast">
  …
</Chart>

"fast" is 0.6×, "slow" 1.4×, and a number sets the multiplier. With reduced motion turned on, nothing moves: changes happen at once or with a short fade.

Theming

Set these CSS variables on the component or a parent. Leave them unset to follow the light and dark theme. The open Customize panel below the preview lets you try these settings.

VariableWhat it changesDefault
--chart-1The series palette, --chart-1 to --chart-5 in config order (theme tokens a preset sets)cobalt, sky, slate, amber, mist
--chart-heightThe chart's height, legend and axes included16rem
--chart-gridGrid rules (75% on all but the zero line)--border
--chart-axisAxis labels--muted-foreground
--chart-cursorThe band or hairline on the active point--foreground at 5% (band) or 22% (hairline)
--chart-tooltipTooltip surface--popover
--chart-radiusBar corners--radius less 6px
--chart-strokeLine width2px
--chart-dotDot size8px
--chart-bar-gapSpace between bars in a group (never more than a third of a bar)4px

API reference

Every part takes className and children, along with the props of the element it renders. The parts are rendered by UI SDK, so they also take render, to draw your own element, and a function of their state for className and style.

Chart

Renders a <element>, and takes its props too.

Prop

Type

ChartLegend

Renders a <div>, and takes its props too.

Prop

Type

ChartTooltip

Renders a <div>, and takes its props too.

Prop

Type

ChartContainer

Renders a <element>, and takes its props too.

Prop

Type

ChartTick

Renders a <div>, and takes its props too.

Prop

Type

Roll

Prop

Type

Other parts

These take the props of the element they render, and nothing else:

  • ChartCenter: a <div>
  • ChartCursor: a <div>
  • ChartGrid: a <div>
  • ChartMarks: a <div>
  • ChartPlot: a <div>
  • ChartXAxis: a <div>
  • ChartYAxis: a <div>
  • ChartArea
  • ChartBar: a <div>
  • ChartDot: a <div>
  • ChartGridLine: a <div>
  • ChartLegendItem: a <button>
  • ChartLine
  • ChartMarker: a <div>
  • ChartPoint: a <div>
  • ChartPoints: a <div>
  • ChartSlice
  • ChartTable: a <table>
  • ChartTooltipItem: a <div>
  • ChartTooltipLabel: a <div>
  • ChartTooltipTotal: a <div>

Keyboard

KeyWhat it does
TabMoves focus to the legend's toggles, then to the chart's points as one tab stop.
ArrowRightMoves to the next point and shows its values (Down for horizontal bars).
ArrowLeftMoves to the previous point (Up for horizontal bars).
HomeMoves to the first point.
EndMoves to the last point.
EscapeHides the tooltip; any arrow brings it back.
SpaceSwitches the focused legend series off or on.

Accessibility

Roles, keyboard and focus are declared on the component's UI SDK primitive, and state is announced through the matching ARIA attributes. More in Accessibility.

Built on UI SDK's core

UI SDK doesn't ship a chart, so this one is defined with its core: one definePrimitive in components/keyframery/core/chart-core.ts, with no React in it. UI SDK's adapters can render that same definition in Vue, Svelte, Solid and web components. To keep your own styling and take only the motion, import from @/components/keyframery/primitives/chart.

Questions

How does the animated Chart for UI SDK move?

Cascade: Bars grow from the baseline one after another, left to right; a ring's slices sweep round from the top, and the others fade back from the slice under the pointer. Draw: Lines draw themselves from the left, with their area unrolling under them, and their dots pop in after. Spring: New data, stacking or hidden series tween every bar, line, slice, tick and grid line to its new place, with a small overshoot. Glide: The cursor band or hairline, the tooltip and each line's dot travel to the active point, and the tooltip swings to the other side near the edge. Roll: The tooltip's values, the axis ticks and a donut's total roll to each new number. Stack: Stacked bars build up from the baseline, each segment growing once the one under it has; the tooltip ends with the stack's total. Height: A series switched off in the legend folds into its baseline while the others widen into its room. Press: Legend items sink a little and pop back.

Do I need Motion or Framer Motion for the animated Chart?

No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.

UI SDK has no chart. How is this one built on UI SDK?

Its behaviour is one definition written with UI SDK's core (definePrimitive), in components/keyframery/core/chart-core.ts. Its parts are rendered by UI SDK's adapter, so they take render, state-based class names and data-slot, like every UI SDK component.

What happens to the Chart when someone turns on reduced motion?

Nothing moves. State changes happen at once, or with a short fade.

All components

On this page