{
  "title": "Voronoi Nearest Tooltip",
  "description": "Spatial Delaunay Voronoi tessellation providing nearest-point hover targeting.",
  "name": "tanstack-charts-voronoi-nearest-tooltip",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/tooltip/voronoi-nearest-tooltip.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, dot } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { voronoi } from \"@tanstack/charts/spatial/voronoi\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo } from \"react\";\n\nimport { scaleLinear } from \"d3-scale\";\nimport { MousePointerClick } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface VoronoiCar {\n    name: string;\n    weight: number;\n    mpg: number;\n    cylinders: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): VoronoiCar[] {\n    const models = [\n        \"Tesla Model 3\",\n        \"BMW i4\",\n        \"Porsche Taycan\",\n        \"Audi e-tron\",\n        \"Mercedes EQE\",\n        \"Lucid Air\",\n        \"Rivian R1T\",\n        \"Ford Lightning\",\n    ];\n    const results: VoronoiCar[] = [];\n\n    for (let i = 0; i < 20; i++) {\n        const weight = 2400 + Math.random() * 2600;\n        const mpg = 48 - (weight - 2400) * 0.007 + (Math.random() - 0.5) * 5;\n        results.push({\n            name: `${models[i % models.length]} #${i + 1}`,\n            weight: Math.round(weight),\n            mpg: Number(mpg.toFixed(1)),\n            cylinders: 4 + (i % 3) * 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 VoronoiCar;\n    if (!d) return { title: \"\", rows: [] };\n    return {\n        title: d.name,\n        rows: [\n            { label: \"Weight\", value: `${d.weight} lb`, color: \"var(--chart-1)\" },\n            { label: \"Fuel Economy\", value: `${d.mpg} mpg`, color: \"var(--chart-2)\" },\n            { label: \"Engine Spec\", value: `${d.cylinders} Cylinders`, color: \"var(--foreground)\" },\n        ],\n    };\n}\n\nexport function VoronoiNearestTooltip() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        voronoi(data, {\n                            id: \"nearest-cells\",\n                            x: \"weight\",\n                            y: \"mpg\",\n                            key: \"name\",\n                            color: (r: VoronoiCar) => `${r.cylinders}`,\n                            fillOpacity: 0.15,\n                            stroke: \"var(--background)\",\n                            strokeWidth: 1,\n                        }),\n                        dot(data, {\n                            id: \"voronoi-points\",\n                            x: \"weight\",\n                            y: \"mpg\",\n                            key: \"name\",\n                            color: (r: VoronoiCar) => `${r.cylinders}`,\n                            stroke: \"var(--background)\",\n                            strokeWidth: 1.5,\n                            r: 4.5,\n                            states: [\n                                {\n                                    when: { focus: \"primary\" },\n                                    style: { r: 8, stroke: \"var(--foreground)\", strokeWidth: 2 },\n                                },\n                            ],\n                        }),\n                    ],\n                    scales: {\n                        x: { scale: scaleLinear, grid: true, axis: { label: \"Vehicle Weight (lb)\" } },\n                        y: { scale: scaleLinear, grid: true, axis: { label: \"Fuel Economy (mpg)\" } },\n                    },\n                    color: {\n                        range: [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\"],\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],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Voronoi Nearest-Point Hover Target</CardTitle>\n                <CardDescription>\n                    Spatial Delaunay Voronoi tessellation expanding hover capture hit-areas for points\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=\"Voronoi Nearest-Point Hover Target 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                    Voronoi cells guarantee closest point selection anywhere inside cell boundaries{\" \"}\n                    <MousePointerClick className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Polygon cell borders partition 2D plane into nearest-neighbor pointer regions\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default VoronoiNearestTooltip;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Spatial Delaunay Voronoi tessellation providing nearest-point hover targeting. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "tooltip"
    ]
  }
}