
Spatial Delaunay Voronoi tessellation providing nearest-point hover targeting.
components/tanstack-charts/tooltip/voronoi-nearest-tooltip.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;