TanStack
API Reference

Alpine Chart

ts
import { charts } from '@tanstack/alpine-charts'

Register charts with Alpine.plugin, then make x-chart evaluate to a complete ChartOptions value. Replace the definition identity when captured application values change. The definition also owns focus, tooltip, animation, keyboard policy, focus distance, and spatial indexing.

Directive options

OptionTypeDefaultMeaning
definitionChartDefinitionRequiredFramework-neutral chart definition
ariaLabelstringRequiredAccessible chart name
ariaDescriptionstringNoneOptional accessible description
heightnumberExisting height or 320 without a ratioFixed CSS and scene height
aspectRationumberNonePositive width-to-height ratio when height is absent
widthnumberExisting width or 100%Fixed CSS and scene width
initialWidthnumber640Initial width before responsive measurement
tabIndexnumber0Surface tab index; keyboard: false forces -1
idPrefixstringGeneratedPrefix for renderer-owned document IDs
classNamestringNoneExtra class on the rendered SVG surface
renderSvgChartSvgRenderer<TDatum, TXValue, TYValue>renderChartSvgScene-to-SVG renderer
measureTextChartTextMeasurerHost measurerGuide text measurement
onFocusChange(point: ChartPoint | null) => voidNonePrimary focus callback
onFocusGroupChange(points: readonly ChartPoint[]) => voidNoneGrouped focus callback
onSelect(point: ChartPoint | null) => voidNonePointer or keyboard activation callback
onRender(context: ChartRenderContext) => voidNoneLive SVG, container, and scene after rendering
renderTooltipBody(context: ChartTooltipBodyRenderContext) => AlpineChartTooltipBodyNoneReturns DOM content for the native tooltip body

The directive element becomes .ts-chart-host; use its normal HTML class and style attributes for outer presentation. Directive cleanup removes the chart surface and restores the element's prior inline layout and host class.

renderTooltipBody receives points, content, a DocumentFragment defaultBody, pinned, and dismiss. Return a DOM node, text, number, nested array, or null. Include defaultBody to retain the native title, rows, formatting, and swatches. The directive replaces returned content when focus changes and removes it when the tooltip closes.

Exported types

ChartOptions includes the definition and host options. ChartTooltipBodyRenderContext and AlpineChartTooltipBody describe custom body composition. The package also re-exports ChartDefinition and ChartPoint.

See the Alpine adapter for lifecycle and browser requirements, Focus and interaction for callback semantics, and Rendering and export for custom SVG renderers.