components/tanstack-charts/bar/hierarchy-tree.tsx
import { type ChartPoint, defineChart, dot, link, text } from "@tanstack/charts";
import { treeLayout } from "@tanstack/charts/hierarchy/tree";
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 } from "d3-scale";
import { Network } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface TreeRawDatum {
name: string;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): TreeRawDatum[] {
return [
{ name: "Root" },
{ name: "Root.Analytics" },
{ name: "Root.Analytics.Cluster" },
{ name: "Root.Analytics.Graph" },
{ name: "Root.Visualization" },
{ name: "Root.Visualization.Axis" },
{ name: "Root.Visualization.Scale" },
];
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const p = points[0];
const d = p.datum as any;
if (!d) return { title: "", rows: [] };
if ("internal" in d) {
return {
title: d.data?.name ?? d.name,
rows: [{ label: "Node Type", value: d.internal ? "Group Node" : "Leaf Node", color: "var(--chart-1)" }],
};
}
return {
title: "Hierarchy Edge",
rows: [
{
label: "Link Path",
value: `${d.sourceNode?.data?.name ?? "Node"} → ${d.targetNode?.data?.name ?? "Node"}`,
color: "var(--foreground)",
},
],
};
}
export function HierarchyTree() {
const rawData = useMemo(() => generateDemoData(), []);
const layout = useMemo(() => {
return treeLayout(rawData, {
path: "name",
delimiter: ".",
});
}, [rawData]);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
link(layout.links, {
id: "hierarchy-links",
x1: "x1",
y1: "y1",
x2: "x2",
y2: "y2",
key: "id",
stroke: "var(--border)",
strokeOpacity: 0.8,
strokeWidth: 1.5,
}),
dot(layout.nodes, {
id: "hierarchy-nodes",
x: "x",
y: "y",
key: "id",
fill: "var(--chart-1)",
r: 4,
}),
text(layout.nodes, {
id: "hierarchy-labels",
x: "x",
y: "y",
text: (n: any) => {
const parts = (n.data?.name || n.name || "").split(".");
return parts[parts.length - 1];
},
key: "id",
fill: "var(--foreground)",
fontSize: 10,
anchor: (n: any) => (n.internal ? "end" : "start"),
dx: (n: any) => (n.internal ? -6 : 6),
}),
],
scales: {
x: { scale: scaleLinear },
y: { scale: scaleLinear },
},
guides: false,
margin: { top: 22, right: 90, bottom: 22, left: 60 },
theme: {
foreground: "var(--muted-foreground)",
grid: "transparent",
background: "transparent",
},
},
{
focus: "group-x",
tooltip: {
use: tooltip,
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "group-center",
placement: "auto",
content: (points) => shadcnTooltipContent(points),
},
},
),
[layout],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Tidy Hierarchy Tree</CardTitle>
<CardDescription>
Tree layout algorithm mapping hierarchical package namespace dependencies
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Tidy Hierarchy Tree Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Tidy node distribution prevents label overlap across branches{" "}
<Network className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Nodes positioned proportionally according to tree hierarchy level and parent relationships
</div>
</CardFooter>
</Card>
);
}
export default HierarchyTree;