
Multi-panel vertically synchronized chart grid with linked inspection cursor.
components/tanstack-charts/line/echarts-synchronized-cursors.tsx
import { type ChartPoint, defineChart, dot, lineY } from "@tanstack/charts";
import { focusGuideX } from "@tanstack/charts/focus/guide";
import { motion } from "@tanstack/charts/motion";
import { tooltip } from "@tanstack/charts/tooltip";
import { viewGrid } from "@tanstack/charts/view";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { scaleLinear, scaleUtc } from "d3-scale";
import { Link2 } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface TravelerDatum {
date: Date;
current: number;
previous: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): TravelerDatum[] {
const results: TravelerDatum[] = [];
for (let m = 0; m < 12; m++) {
const date = new Date(Date.UTC(2024, m, 1));
results.push({
date,
current: Math.round(180 + Math.sin(m * 0.5) * 60 + Math.random() * 20),
previous: Math.round(240 + Math.cos(m * 0.5) * 50 + Math.random() * 20),
});
}
return results;
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as TravelerDatum;
if (!d) return { title: "", rows: [] };
return {
title: d.date.toLocaleDateString("en-US", { month: "short", year: "numeric", timeZone: "UTC" }),
rows: [
{ label: "2024 Travelers", value: `${d.current}k`, color: "var(--chart-1)" },
{ label: "2023 Travelers", value: `${d.previous}k`, color: "var(--chart-2)" },
],
};
}
export function EchartsSynchronizedCursors() {
const data = useMemo(() => generateDemoData(), []);
const subViewCurrent = useMemo(
() =>
defineChart({
renderer: motionRenderer,
marks: [
lineY(data, {
id: "current-line",
x: "date",
y: "current",
stroke: "var(--chart-1)",
strokeWidth: 2,
}),
dot(data, {
id: "current-points",
x: "date",
y: "current",
fill: "var(--chart-1)",
r: 3,
}),
focusGuideX(data, {
id: "current-guide",
x: "date",
y: "current",
xRule: { stroke: "var(--muted-foreground)", strokeWidth: 1, strokeDasharray: "4 4" },
}),
],
scales: {
x: {
scale: scaleUtc,
axis: false,
},
y: {
scale: scaleLinear,
grid: true,
axis: { label: "2024 (k)" },
},
},
margin: { top: 12, right: 24, bottom: 12, left: 45 },
}),
[data],
);
const subViewPrevious = useMemo(
() =>
defineChart({
renderer: motionRenderer,
marks: [
lineY(data, {
id: "previous-line",
x: "date",
y: "previous",
stroke: "var(--chart-2)",
strokeWidth: 2,
}),
dot(data, {
id: "previous-points",
x: "date",
y: "previous",
fill: "var(--chart-2)",
r: 3,
}),
focusGuideX(data, {
id: "previous-guide",
x: "date",
y: "previous",
xRule: { stroke: "var(--muted-foreground)", strokeWidth: 1, strokeDasharray: "4 4" },
}),
],
scales: {
x: {
scale: scaleUtc,
axis: {
ticks: {
format: (v: Date) => v.toLocaleDateString("en-US", { month: "short", timeZone: "UTC" }),
},
},
},
y: {
scale: scaleLinear,
grid: true,
axis: { label: "2023 (k)" },
},
},
margin: { top: 12, right: 24, bottom: 28, left: 45 },
}),
[data],
);
const definition = useMemo(
() =>
defineChart(
viewGrid({
id: "synchronized-cursors",
rows: [
{ id: "current", grow: 1 },
{ id: "previous", grow: 1 },
],
columns: [{ id: "main", grow: 1 }],
rowGap: 8,
views: [
{ id: "current", row: "current", column: "main", chart: subViewCurrent },
{
id: "previous",
row: "previous",
column: "main",
share: { x: "current" },
chart: subViewPrevious,
},
],
}),
{
scales: {
x: { scale: scaleUtc },
y: { scale: scaleLinear },
},
keyboard: true,
focus: "group-x",
focusRing: false,
maxFocusDistance: Number.POSITIVE_INFINITY,
tooltip: {
use: tooltip,
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "pointer",
placement: "auto",
content: (points) => shadcnTooltipContent(points),
},
},
),
[subViewCurrent, subViewPrevious],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Synchronized Dual-Grid X-Axis Crosshair Cursors</CardTitle>
<CardDescription>
Multi-panel vertically synchronized charts sharing pointer cursor alignment across separate Y scales
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[270px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={270}
ariaLabel="Synchronized Dual-Grid X-Axis Crosshair Cursors Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Moving cursor over top grid synchronizes crosshair on bottom grid{" "}
<Link2 className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
`viewGrid` shares identical `x` scales to align dates perfectly across stacked panels
</div>
</CardFooter>
</Card>
);
}
export default EchartsSynchronizedCursors;