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