
High-density multivariate bubble scatterplot encoding weight, economy, and power.
components/tanstack-charts/line/many-point-scatter.tsx
import { type ChartPoint, defineChart, dot } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { scaleLinear, scaleSqrt } from "d3-scale";
import { ScatterChart } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface HighDensityDatum {
name: string;
weight: number;
acceleration: number;
cylinders: number;
displacement: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): HighDensityDatum[] {
const results: HighDensityDatum[] = [];
const models = ["Sedan", "Coupe", "SUV", "Truck", "Hatchback"];
for (let i = 0; i < 180; i++) {
const weight = 1800 + Math.random() * 3200;
const acceleration = 22 - (weight - 1800) * 0.003 + (Math.random() - 0.5) * 5;
const cylinders = [4, 6, 8][i % 3];
const displacement = 1200 + cylinders * 400 + Math.random() * 800;
results.push({
name: `${models[i % models.length]} ${2015 + (i % 10)} #${i + 1}`,
weight: Math.round(weight),
acceleration: Number(Math.max(8, acceleration).toFixed(1)),
cylinders,
displacement: Math.round(displacement),
});
}
return results;
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as HighDensityDatum;
if (!d) return { title: "", rows: [] };
return {
title: d.name,
rows: [
{ label: "Weight", value: `${d.weight.toLocaleString()} lb`, color: "var(--chart-1)" },
{ label: "0-60mph Time", value: `${d.acceleration}s`, color: "var(--chart-2)" },
{ label: "Engine Displacement", value: `${d.displacement} cc`, color: "var(--foreground)" },
],
};
}
export function ManyPointScatter() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
dot(data, {
id: "cars",
x: "weight",
y: "acceleration",
color: (r: HighDensityDatum) => `${r.cylinders}`,
key: "name",
r: "displacement",
rScale: {
scale: () => scaleSqrt().range([2.5, 6]),
},
fillOpacity: 0.7,
stroke: "var(--background)",
strokeWidth: 0.5,
}),
],
scales: {
x: { scale: scaleLinear, grid: true, axis: { label: "Vehicle Weight (lb)" } },
y: { scale: scaleLinear, grid: true, axis: { label: "0-60 mph Acceleration (sec)" } },
},
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>High-Density Multivariate Bubble Scatterplot</CardTitle>
<CardDescription>
Large point-count scatterplot encoding weight, acceleration, cylinder count, and engine displacement
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="High-Density Multivariate Bubble Scatterplot Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Bubble area scales with square-root engine displacement{" "}
<ScatterChart className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Supports high point count rendering with custom radius and color encoding
</div>
</CardFooter>
</Card>
);
}
export default ManyPointScatter;