# Hierarchy Tree

> Tree layout algorithm mapping hierarchical package dependencies.

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-hierarchy-tree).

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

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/pacekit/tanstack-charts-hierarchy-tree.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/bar/hierarchy-tree.tsx`

```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;
```
