# Voronoi Nearest Tooltip

> Spatial Delaunay Voronoi tessellation providing nearest-point hover targeting.

A shadcn/ui block from the [@pacekit](https://ui.pacekit.dev/?utm_source=shadcn.exchange) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/pacekit/tanstack-charts-voronoi-nearest-tooltip).

- Page, with a live preview: https://shadcn.exchange/r/pacekit/tanstack-charts-voronoi-nearest-tooltip
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/pacekit/tanstack-charts-voronoi-nearest-tooltip-light-4c844782bc62fdb4.png
- Registry item: https://ui.pacekit.dev/r/tanstack-charts-voronoi-nearest-tooltip.json
- Listed: 2026-10-21, source last changed 2026-10-21

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/pacekit/tanstack-charts-voronoi-nearest-tooltip.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `@tanstack/charts`, `@tanstack/react-charts`, `d3-scale`
- Registry items: `card`

## Files

### `components/tanstack-charts/tooltip/voronoi-nearest-tooltip.tsx`

```tsx
import { type ChartPoint, defineChart, dot } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { voronoi } from "@tanstack/charts/spatial/voronoi";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";

import { scaleLinear } from "d3-scale";
import { MousePointerClick } from "lucide-react";

import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";

interface VoronoiCar {
    name: string;
    weight: number;
    mpg: number;
    cylinders: number;
}

const motionRenderer = motion({
    initial: "always",
    transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});

function generateDemoData(): VoronoiCar[] {
    const models = [
        "Tesla Model 3",
        "BMW i4",
        "Porsche Taycan",
        "Audi e-tron",
        "Mercedes EQE",
        "Lucid Air",
        "Rivian R1T",
        "Ford Lightning",
    ];
    const results: VoronoiCar[] = [];

    for (let i = 0; i < 20; i++) {
        const weight = 2400 + Math.random() * 2600;
        const mpg = 48 - (weight - 2400) * 0.007 + (Math.random() - 0.5) * 5;
        results.push({
            name: `${models[i % models.length]} #${i + 1}`,
            weight: Math.round(weight),
            mpg: Number(mpg.toFixed(1)),
            cylinders: 4 + (i % 3) * 2,
        });
    }
    return results;
}

function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
    if (points.length === 0) return { title: "", rows: [] };
    const d = points[0].datum as VoronoiCar;
    if (!d) return { title: "", rows: [] };
    return {
        title: d.name,
        rows: [
            { label: "Weight", value: `${d.weight} lb`, color: "var(--chart-1)" },
            { label: "Fuel Economy", value: `${d.mpg} mpg`, color: "var(--chart-2)" },
            { label: "Engine Spec", value: `${d.cylinders} Cylinders`, color: "var(--foreground)" },
        ],
    };
}

export function VoronoiNearestTooltip() {
    const data = useMemo(() => generateDemoData(), []);

    const definition = useMemo(
        () =>
            defineChart(
                {
                    renderer: motionRenderer,
                    marks: [
                        voronoi(data, {
                            id: "nearest-cells",
                            x: "weight",
                            y: "mpg",
                            key: "name",
                            color: (r: VoronoiCar) => `${r.cylinders}`,
                            fillOpacity: 0.15,
                            stroke: "var(--background)",
                            strokeWidth: 1,
                        }),
                        dot(data, {
                            id: "voronoi-points",
                            x: "weight",
                            y: "mpg",
                            key: "name",
                            color: (r: VoronoiCar) => `${r.cylinders}`,
                            stroke: "var(--background)",
                            strokeWidth: 1.5,
                            r: 4.5,
                            states: [
                                {
                                    when: { focus: "primary" },
                                    style: { r: 8, stroke: "var(--foreground)", strokeWidth: 2 },
                                },
                            ],
                        }),
                    ],
                    scales: {
                        x: { scale: scaleLinear, grid: true, axis: { label: "Vehicle Weight (lb)" } },
                        y: { scale: scaleLinear, grid: true, axis: { label: "Fuel Economy (mpg)" } },
                    },
                    color: {
                        range: ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)"],
                    },
                    margin: { top: 24, right: 24, bottom: 32, left: 45 },
                    theme: {
                        foreground: "var(--muted-foreground)",
                        grid: "var(--border)",
                        background: "transparent",
                    },
                },
                {
                    keyboard: true,
                    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),
                    },
                },
            ),
        [data],
    );

    return (
        <Card className="w-full max-w-xl">
            <CardHeader>
                <CardTitle>Voronoi Nearest-Point Hover Target</CardTitle>
                <CardDescription>
                    Spatial Delaunay Voronoi tessellation expanding hover capture hit-areas for points
                </CardDescription>
            </CardHeader>
            <CardContent>
                <div className="h-[250px] w-full">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={250}
                        ariaLabel="Voronoi Nearest-Point Hover Target Chart"
                    />
                </div>
            </CardContent>
            <CardFooter className="flex-col items-start gap-2 text-sm">
                <div className="flex items-center gap-2 leading-none font-medium">
                    Voronoi cells guarantee closest point selection anywhere inside cell boundaries{" "}
                    <MousePointerClick className="h-4 w-4 text-emerald-500" />
                </div>
                <div className="text-muted-foreground leading-none">
                    Polygon cell borders partition 2D plane into nearest-neighbor pointer regions
                </div>
            </CardFooter>
        </Card>
    );
}

export default VoronoiNearestTooltip;
```
