This walkthrough uses the framework-agnostic DOM host. The same chart definition passes unchanged to any supported framework adapter.
Install the core and compact scales:
pnpm add @tanstack/charts @tanstack/charts-scales<div id="monthly-revenue-chart"></div>The host follows the container width when width is omitted.
import { scaleLinear } from '@tanstack/charts-scales/linear'
import { scalePoint } from '@tanstack/charts-scales/point'
import { defineChart, lineY, mountChart } from '@tanstack/charts'
import { tooltip } from '@tanstack/charts/tooltip'
interface RevenueMonth {
month: string
revenue: number
}
const monthlyRevenue: readonly RevenueMonth[] = [
{ month: 'Jan', revenue: 42_000 },
{ month: 'Feb', revenue: 58_000 },
{ month: 'Mar', revenue: 76_000 },
{ month: 'Apr', revenue: 64_000 },
{ month: 'May', revenue: 81_000 },
]
const monthlyRevenueChart = defineChart({
marks: [
lineY(monthlyRevenue, {
id: 'monthly-revenue',
x: 'month',
y: 'revenue',
points: true,
stroke: '#2563eb',
}),
],
x: {
scale: () => scalePoint<string>().padding(0.2),
axis: { label: 'Month' },
},
y: {
scale: scaleLinear,
nice: true,
grid: true,
axis: { label: 'Revenue (USD)' },
},
tooltip,
})The compact point and linear scales cover this categorical and numeric chart without a D3 dependency. The original revenue row flows through the mark and into interaction callbacks; no cast or manual chart generic is needed.
const container = document.querySelector<HTMLElement>('#monthly-revenue-chart')
if (!container) {
throw new Error('Missing #monthly-revenue-chart container')
}
const options = {
definition: monthlyRevenueChart,
height: 360,
initialWidth: 640,
ariaLabel: 'Monthly revenue',
}
const host = mountChart(container, options)initialWidth is the deterministic fallback for server output, hidden containers, and the first frame before measurement. Once visible, the host uses ResizeObserver to follow the container.
Host options only cover mounting concerns such as size and accessibility:
host.update({
...options,
height: 420,
})When data, visual options, focus, tooltips, keyboard policy, or animation change, create a new definition and pass it to host.update. In a framework component, memoize the complete definition against the values it captures. See Chart definitions.
host.destroy()Destroying the host removes observers, event listeners, animations, tooltips, and chart markup. Framework adapters do this automatically during unmount.
Read Grammar of Graphics for the full model, then browse the Example Gallery for complete compositions.