{
  "title": "Force Directed Network",
  "description": "Force-directed graph layout visualizing nodes and physics spring link connections.",
  "name": "tanstack-charts-force-directed-network",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/force-directed-network.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, dot, link, text } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { forceLayout } from \"@tanstack/charts/network/force\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo } from \"react\";\n\nimport { scaleLinear } from \"d3-scale\";\nimport { Share2 } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface NodeDatum {\n    id: string;\n    group: number;\n}\n\ninterface LinkDatum {\n    source: string;\n    target: string;\n    value: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): { nodes: NodeDatum[]; links: LinkDatum[] } {\n    const nodes: NodeDatum[] = [\n        { id: \"Valjean\", group: 1 },\n        { id: \"Javert\", group: 1 },\n        { id: \"Fantine\", group: 1 },\n        { id: \"Cosette\", group: 2 },\n        { id: \"Marius\", group: 2 },\n        { id: \"Eponine\", group: 2 },\n        { id: \"Thenardier\", group: 2 },\n        { id: \"Mme.Thenardier\", group: 2 },\n        { id: \"Enjolras\", group: 3 },\n        { id: \"Gavroche\", group: 3 },\n        { id: \"Courfeyrac\", group: 3 },\n        { id: \"Combeferre\", group: 3 },\n    ];\n\n    const links: LinkDatum[] = [\n        { source: \"Valjean\", target: \"Javert\", value: 8 },\n        { source: \"Valjean\", target: \"Fantine\", value: 6 },\n        { source: \"Valjean\", target: \"Cosette\", value: 9 },\n        { source: \"Valjean\", target: \"Marius\", value: 5 },\n        { source: \"Javert\", target: \"Fantine\", value: 3 },\n        { source: \"Cosette\", target: \"Marius\", value: 10 },\n        { source: \"Marius\", target: \"Eponine\", value: 7 },\n        { source: \"Thenardier\", target: \"Mme.Thenardier\", value: 9 },\n        { source: \"Thenardier\", target: \"Valjean\", value: 4 },\n        { source: \"Enjolras\", target: \"Gavroche\", value: 8 },\n        { source: \"Enjolras\", target: \"Courfeyrac\", value: 7 },\n        { source: \"Enjolras\", target: \"Combeferre\", value: 7 },\n        { source: \"Courfeyrac\", target: \"Combeferre\", value: 6 },\n        { source: \"Marius\", target: \"Enjolras\", value: 5 },\n    ];\n\n    return { nodes, links };\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const p = points[0];\n    const d = p.datum as any;\n    if (!d) return { title: \"\", rows: [] };\n    if (\"group\" in d) {\n        return {\n            title: d.id,\n            rows: [{ label: \"Group\", value: `Cluster ${d.group}`, color: \"var(--chart-1)\" }],\n        };\n    }\n    return {\n        title: \"Connection Link\",\n        rows: [\n            { label: \"Link Path\", value: `${d.source} → ${d.target}`, color: \"var(--foreground)\" },\n            { label: \"Interaction Weight\", value: `${d.value}`, color: \"var(--chart-2)\" },\n        ],\n    };\n}\n\nexport function ForceDirectedNetwork() {\n    const { nodes, links } = useMemo(() => generateDemoData(), []);\n\n    const graph = useMemo(() => {\n        return forceLayout(nodes, links, {\n            nodeKey: \"id\",\n            source: \"source\",\n            target: \"target\",\n            iterations: 300,\n            domainPadding: 0.2,\n            forces: [\n                {\n                    type: \"link\",\n                    distance: (datum: LinkDatum) => 54 - Math.min(datum.value, 10) * 1.8,\n                    strength: (datum: LinkDatum) => 0.2 + Math.min(datum.value, 10) * 0.045,\n                },\n                { type: \"manyBody\", strength: -165 },\n                { type: \"center\", x: 0, y: 0 },\n                { type: \"collide\", radius: 15, strength: 0.9 },\n                { type: \"x\", x: 0, strength: 0.035 },\n                { type: \"y\", y: 0, strength: 0.035 },\n            ],\n        });\n    }, [nodes, links]);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        link(graph.links, {\n                            id: \"network-links\",\n                            x1: \"x1\",\n                            y1: \"y1\",\n                            x2: \"x2\",\n                            y2: \"y2\",\n                            key: ({ source, target }: any) => `${source}->${target}`,\n                            stroke: \"var(--border)\",\n                            strokeOpacity: 0.7,\n                            strokeWidth: 1.5,\n                        }),\n                        dot(graph.nodes, {\n                            id: \"network-nodes\",\n                            x: \"x\",\n                            y: \"y\",\n                            color: (d: any) =>\n                                d.group === 1 ? \"var(--chart-1)\" : d.group === 2 ? \"var(--chart-2)\" : \"var(--chart-3)\",\n                            key: \"id\",\n                            r: 7,\n                            stroke: \"var(--background)\",\n                            strokeWidth: 1.5,\n                        }),\n                        text(graph.nodes, {\n                            id: \"network-labels\",\n                            x: \"x\",\n                            y: \"y\",\n                            text: \"id\",\n                            key: \"id\",\n                            dy: -12,\n                            fontSize: 10,\n                            fontWeight: 600,\n                            fill: \"var(--foreground)\",\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleLinear().domain(graph.xDomain),\n                        },\n                        y: {\n                            scale: scaleLinear().domain(graph.yDomain),\n                        },\n                    },\n                    guides: false,\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"transparent\",\n                        background: \"transparent\",\n                    },\n                },\n                {\n                    keyboard: true,\n                    tooltip: {\n                        use: tooltip,\n                        className:\n                            \"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs\",\n                        anchor: \"group-center\",\n                        placement: \"auto\",\n                        content: (points) => shadcnTooltipContent(points),\n                    },\n                },\n            ),\n        [graph],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Force-Directed Network</CardTitle>\n                <CardDescription>\n                    Physics-based force layout displaying node relationships and entity interaction clusters\n                </CardDescription>\n            </CardHeader>\n            <CardContent>\n                <div className=\"h-[280px] w-full\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={280}\n                        ariaLabel=\"Force-Directed Network Chart\"\n                    />\n                </div>\n            </CardContent>\n            <CardFooter className=\"flex-col items-start gap-2 text-sm\">\n                <div className=\"flex items-center gap-2 leading-none font-medium\">\n                    Nodes positioned via spring-embedder force simulation <Share2 className=\"text-primary h-4 w-4\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Repulsive many-body forces separate clusters while link tension pulls connected entities\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default ForceDirectedNetwork;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Force-directed graph layout visualizing nodes and physics spring link connections. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}