{
  "title": "Connected Scatter",
  "description": "2D path trajectory tracking driving distance vs gas price over time.",
  "name": "tanstack-charts-connected-scatter",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale",
    "d3-shape"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/connected-scatter.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, arrow, d3Curve, defineChart, dot, lineY, rollingWindow, text } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo } from \"react\";\n\nimport { scaleLinear } from \"d3-scale\";\nimport { curveCatmullRom } from \"d3-shape\";\nimport { Route } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface DrivingDatum {\n    year: number;\n    miles: number;\n    gas: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): DrivingDatum[] {\n    const results: DrivingDatum[] = [];\n    let miles = 4000;\n    let gas = 1.2;\n\n    for (let yr = 1980; yr <= 2024; yr += 2) {\n        miles += (Math.sin(yr * 0.2) + 1.5) * 200;\n        gas += (Math.cos(yr * 0.3) + 0.2) * 0.15;\n        results.push({\n            year: yr,\n            miles: Math.round(miles),\n            gas: Number(gas.toFixed(2)),\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 DrivingDatum;\n    if (!d) return { title: \"\", rows: [] };\n    return {\n        title: `Year ${d.year}`,\n        rows: [\n            { label: \"Miles Driven / Person\", value: `${d.miles.toLocaleString()} mi`, color: \"var(--chart-1)\" },\n            { label: \"Gasoline Price\", value: `$${d.gas.toFixed(2)}/gal`, color: \"var(--foreground)\" },\n        ],\n    };\n}\n\nexport function ConnectedScatter() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const directionPairs = useMemo(() => {\n        return rollingWindow(data, {\n            orderBy: \"year\",\n            size: 2,\n            partial: false,\n            outputs: {},\n        }).filter(({ sourceIndexes }) => (sourceIndexes[1] ?? -1) % 4 === 0);\n    }, [data]);\n\n    const labels = useMemo(() => data.filter((d) => d.year % 10 === 0), [data]);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        lineY(data, {\n                            id: \"driving-path\",\n                            x: \"miles\",\n                            y: \"gas\",\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 2.25,\n                            curve: d3Curve(curveCatmullRom.alpha(0.5)),\n                        }),\n                        dot(data, {\n                            id: \"driving-points\",\n                            x: \"miles\",\n                            y: \"gas\",\n                            fill: \"var(--chart-2)\",\n                            r: 3.5,\n                        }),\n                        arrow(directionPairs, {\n                            id: \"direction-arrows\",\n                            x1: ({ source }) => source[0]?.miles,\n                            y1: ({ source }) => source[0]?.gas,\n                            x2: \"miles\",\n                            y2: \"gas\",\n                            stroke: \"var(--chart-2)\",\n                            strokeWidth: 1.5,\n                            headLength: 6,\n                        }),\n                        text(labels, {\n                            id: \"year-labels\",\n                            x: \"miles\",\n                            y: \"gas\",\n                            text: (row: DrivingDatum) => `${row.year}`,\n                            fill: \"var(--foreground)\",\n                            anchor: \"middle\",\n                            dy: -10,\n                            fontSize: 10,\n                            fontWeight: 600,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: { label: \"Miles Driven per Person\" },\n                        },\n                        y: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: { label: \"Cost of Gasoline ($/gal)\" },\n                        },\n                    },\n                    margin: { top: 24, right: 24, bottom: 32, left: 45 },\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"var(--border)\",\n                        background: \"transparent\",\n                    },\n                },\n                {\n                    keyboard: true,\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        [data, directionPairs, labels],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Directed Connected Scatterplot</CardTitle>\n                <CardDescription>\n                    2D path trajectory tracking driving distance vs gas prices across time\n                </CardDescription>\n            </CardHeader>\n            <CardContent>\n                <div className=\"h-[250px] w-full\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={250}\n                        ariaLabel=\"Directed Connected Scatterplot 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                    Trajectory arrows illustrate chronological progression over 40 years{\" \"}\n                    <Route className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Connected scatterplot connects 2D points sequentially to visualize non-monotonic relationship\n                    dynamics\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default ConnectedScatter;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. 2D path trajectory tracking driving distance vs gas price over time. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "line"
    ]
  }
}