{"spec_id":"network-hierarchical","library":"muix","language":"javascript","code":"// anyplot.ai\n// network-hierarchical: Hierarchical Network Graph with Tree Layout\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 87/100 | Updated: 2026-10-24\n\nimport * as React from \"react\";\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TITLE = \"network-hierarchical · javascript · muix · anyplot.ai\";\nconst TITLE_FONT_SIZE = 22;\nconst TITLE_H = 44;\nconst LEGEND_H = 36;\nconst LEVEL_RADII = [22, 16, 12, 9];\nconst X_LEFT_PAD_SHARE = 0.065; // room for the left-leaning rotated leaf labels\nconst X_RIGHT_PAD_SHARE = 0.015;\nconst Y_TOP_PAD_SHARE = 0.1;\nconst Y_BOTTOM_PAD_SHARE = 0.19;\nconst EDGE_OPACITY = 0.62;\n\n// Columns: set these to your data's column names\nconst NODE_ID = \"Node ID\"; // role: id\nconst LABEL = \"Role\"; // role: label\nconst PARENT_ID = \"Reports to\"; // role: parent\n\n// Data: replace this block with your own records\nconst data: Array<Record<string, string | null>> = [\n  { [NODE_ID]: \"ceo\", [LABEL]: \"CEO\", [PARENT_ID]: null },\n\n  // Engineering branch\n  { [NODE_ID]: \"vp-eng\", [LABEL]: \"VP Engineering\", [PARENT_ID]: \"ceo\" },\n  { [NODE_ID]: \"dir-plat\", [LABEL]: \"Platform Eng\", [PARENT_ID]: \"vp-eng\" },\n  { [NODE_ID]: \"dir-prod\", [LABEL]: \"Product Eng\", [PARENT_ID]: \"vp-eng\" },\n  { [NODE_ID]: \"rep-be1\", [LABEL]: \"Backend Eng 1\", [PARENT_ID]: \"dir-plat\" },\n  { [NODE_ID]: \"rep-be2\", [LABEL]: \"Backend Eng 2\", [PARENT_ID]: \"dir-plat\" },\n  { [NODE_ID]: \"rep-inf\", [LABEL]: \"Infra Eng\", [PARENT_ID]: \"dir-plat\" },\n  { [NODE_ID]: \"rep-fe1\", [LABEL]: \"Frontend Eng 1\", [PARENT_ID]: \"dir-prod\" },\n  { [NODE_ID]: \"rep-fe2\", [LABEL]: \"Frontend Eng 2\", [PARENT_ID]: \"dir-prod\" },\n\n  // Sales branch\n  { [NODE_ID]: \"vp-sales\", [LABEL]: \"VP Sales\", [PARENT_ID]: \"ceo\" },\n  { [NODE_ID]: \"dir-ent\", [LABEL]: \"Enterprise Sales\", [PARENT_ID]: \"vp-sales\" },\n  { [NODE_ID]: \"dir-smb\", [LABEL]: \"SMB Sales\", [PARENT_ID]: \"vp-sales\" },\n  { [NODE_ID]: \"rep-ae1\", [LABEL]: \"Account Exec 1\", [PARENT_ID]: \"dir-ent\" },\n  { [NODE_ID]: \"rep-ae2\", [LABEL]: \"Account Exec 2\", [PARENT_ID]: \"dir-ent\" },\n  { [NODE_ID]: \"rep-ae3\", [LABEL]: \"Account Exec 3\", [PARENT_ID]: \"dir-smb\" },\n  { [NODE_ID]: \"rep-ae4\", [LABEL]: \"Account Exec 4\", [PARENT_ID]: \"dir-smb\" },\n\n  // Marketing branch\n  { [NODE_ID]: \"vp-mkt\", [LABEL]: \"VP Marketing\", [PARENT_ID]: \"ceo\" },\n  { [NODE_ID]: \"dir-brand\", [LABEL]: \"Brand\", [PARENT_ID]: \"vp-mkt\" },\n  { [NODE_ID]: \"dir-growth\", [LABEL]: \"Growth\", [PARENT_ID]: \"vp-mkt\" },\n  { [NODE_ID]: \"rep-bm\", [LABEL]: \"Brand Manager\", [PARENT_ID]: \"dir-brand\" },\n  { [NODE_ID]: \"rep-cl\", [LABEL]: \"Content Lead\", [PARENT_ID]: \"dir-brand\" },\n  { [NODE_ID]: \"rep-ga\", [LABEL]: \"Growth Analyst\", [PARENT_ID]: \"dir-growth\" },\n  { [NODE_ID]: \"rep-seo\", [LABEL]: \"SEO Specialist\", [PARENT_ID]: \"dir-growth\" },\n\n  // Operations branch\n  { [NODE_ID]: \"vp-ops\", [LABEL]: \"VP Operations\", [PARENT_ID]: \"ceo\" },\n  { [NODE_ID]: \"dir-fin\", [LABEL]: \"Finance\", [PARENT_ID]: \"vp-ops\" },\n  { [NODE_ID]: \"dir-ppl\", [LABEL]: \"People\", [PARENT_ID]: \"vp-ops\" },\n  { [NODE_ID]: \"rep-ctrl\", [LABEL]: \"Controller\", [PARENT_ID]: \"dir-fin\" },\n  { [NODE_ID]: \"rep-fpa\", [LABEL]: \"FP&A Analyst\", [PARENT_ID]: \"dir-fin\" },\n  { [NODE_ID]: \"rep-rec\", [LABEL]: \"Recruiter\", [PARENT_ID]: \"dir-ppl\" },\n  { [NODE_ID]: \"rep-hr\", [LABEL]: \"HR Partner\", [PARENT_ID]: \"dir-ppl\" },\n];\n\n// Plot: reads only data and the constants above\ninterface TreeNode {\n  id: string;\n  label: string;\n  parentId: string | null;\n  level: number;\n  branchIndex: number;\n  children: string[];\n  radius: number;\n  color: string;\n  x: number;\n  y: number;\n}\n\nconst nodesById = new Map<string, TreeNode>();\nconst allNodes: TreeNode[] = [];\n\nfor (const row of data) {\n  const id = String(row[NODE_ID]);\n  const label = String(row[LABEL]);\n  const parentVal = row[PARENT_ID];\n  const parentId = parentVal !== null && parentVal !== undefined && String(parentVal) !== \"\" ? String(parentVal) : null;\n  const node: TreeNode = {\n    id,\n    label,\n    parentId,\n    level: 0,\n    branchIndex: -1,\n    children: [],\n    radius: LEVEL_RADII[0],\n    color: t.ink,\n    x: 0,\n    y: 0,\n  };\n  nodesById.set(id, node);\n  allNodes.push(node);\n}\n\nfor (const node of allNodes) {\n  if (node.parentId !== null) {\n    nodesById.get(node.parentId)!.children.push(node.id);\n  }\n}\n\nconst rootNode = allNodes.find((n) => n.parentId === null)!;\nconst branchRootIds = rootNode.children;\nconst branches = branchRootIds.map((id) => nodesById.get(id)!.label);\n\nconst dfsOrder: TreeNode[] = [];\nconst stack: TreeNode[] = [rootNode];\nwhile (stack.length > 0) {\n  const curr = stack.pop()!;\n  dfsOrder.push(curr);\n  for (let i = curr.children.length - 1; i >= 0; i -= 1) {\n    const child = nodesById.get(curr.children[i])!;\n    child.level = curr.level + 1;\n    child.branchIndex = curr.id === rootNode.id ? branchRootIds.indexOf(child.id) : curr.branchIndex;\n    child.radius = LEVEL_RADII[Math.min(child.level, LEVEL_RADII.length - 1)];\n    child.color = t.palette[child.branchIndex];\n    stack.push(child);\n  }\n}\n\nconst maxLevel = Math.max(...allNodes.map((n) => n.level));\n\nlet nextLeafX = 0;\nfor (let i = 0; i < dfsOrder.length; i += 1) {\n  const node = dfsOrder[i];\n  if (node.children.length === 0) {\n    node.x = nextLeafX;\n    nextLeafX += 1;\n  }\n}\nfor (let i = dfsOrder.length - 1; i >= 0; i -= 1) {\n  const node = dfsOrder[i];\n  if (node.children.length > 0) {\n    let sumX = 0;\n    for (const childId of node.children) {\n      sumX += nodesById.get(childId)!.x;\n    }\n    node.x = sumX / node.children.length;\n  }\n  node.y = maxLevel - node.level;\n}\n\nconst edges: Array<{ parent: TreeNode; child: TreeNode }> = [];\nfor (const node of allNodes) {\n  if (node.parentId !== null) {\n    edges.push({ parent: nodesById.get(node.parentId)!, child: node });\n  }\n}\n\nconst xSpan = nextLeafX - 1;\nconst domain = {\n  xMin: -xSpan * X_LEFT_PAD_SHARE,\n  xMax: xSpan * (1 + X_RIGHT_PAD_SHARE),\n  yMin: -maxLevel * Y_BOTTOM_PAD_SHARE,\n  yMax: maxLevel * (1 + Y_TOP_PAD_SHARE),\n};\n\nfunction HierarchicalTreeOverlay() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  return (\n    <g data-drawing-container>\n      {edges.map(({ parent, child }, i) => (\n        <line\n          key={`edge-${i}`}\n          x1={xScale(parent.x)}\n          y1={yScale(parent.y)}\n          x2={xScale(child.x)}\n          y2={yScale(child.y)}\n          stroke={child.color}\n          strokeOpacity={EDGE_OPACITY}\n          strokeWidth={1.6}\n        />\n      ))}\n      {allNodes.map((node) => {\n        const cx = Number(xScale(node.x));\n        const cy = Number(yScale(node.y));\n        const isLeaf = node.children.length === 0;\n        return (\n          <React.Fragment key={node.id}>\n            <circle\n              cx={cx}\n              cy={cy}\n              r={node.radius}\n              fill={node.color}\n              stroke={t.pageBg}\n              strokeWidth={1.5}\n            />\n            {!isLeaf ? (\n              <g>\n                <rect\n                  x={cx - (node.label.length * 4.1 + 6)}\n                  y={cy - node.radius - 24}\n                  width={node.label.length * 8.2 + 12}\n                  height={19}\n                  rx={4}\n                  fill={t.pageBg}\n                  fillOpacity={0.86}\n                />\n                <text\n                  x={cx}\n                  y={cy - node.radius - 10}\n                  textAnchor=\"middle\"\n                  fontSize={node.level === 0 ? 15 : 14}\n                  fontWeight={node.level === 0 ? 600 : 500}\n                  fill={t.ink}\n                >\n                  {node.label}\n                </text>\n              </g>\n            ) : (\n              <text\n                x={cx}\n                y={cy + node.radius + 12}\n                textAnchor=\"end\"\n                fontSize={14}\n                fontWeight={400}\n                fill={t.inkSoft}\n                transform={`rotate(-36, ${cx}, ${cy + node.radius + 12})`}\n              >\n                {node.label}\n              </text>\n            )}\n          </React.Fragment>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const chartHeight = height - TITLE_H - LEGEND_H;\n\n  return (\n    <div\n      style={{\n        width,\n        height,\n        backgroundColor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        fontFamily: \"Inter, system-ui, sans-serif\",\n      }}\n    >\n      <div\n        style={{\n          height: `${TITLE_H}px`,\n          lineHeight: `${TITLE_H}px`,\n          fontSize: `${TITLE_FONT_SIZE}px`,\n          fontWeight: 600,\n          color: t.ink,\n          paddingLeft: \"16px\",\n        }}\n      >\n        {TITLE}\n      </div>\n      <div\n        style={{\n          height: LEGEND_H,\n          display: \"flex\",\n          alignItems: \"center\",\n          gap: \"20px\",\n          flexWrap: \"wrap\",\n          paddingLeft: \"16px\",\n        }}\n      >\n        {branches.map((branchName, i) => (\n          <div key={branchName} style={{ display: \"flex\", alignItems: \"center\", gap: \"7px\" }}>\n            <span\n              style={{\n                width: \"12px\",\n                height: \"12px\",\n                borderRadius: \"50%\",\n                backgroundColor: t.palette[i],\n                display: \"inline-block\",\n              }}\n            />\n            <span style={{ fontSize: \"14px\", color: t.inkSoft }}>{branchName}</span>\n          </div>\n        ))}\n      </div>\n      <ChartsContainer\n        width={width}\n        height={chartHeight}\n        series={[]}\n        margin={{ top: 8, bottom: 8, left: 16, right: 16 }}\n        xAxis={[{ id: \"x\", scaleType: \"linear\", min: domain.xMin, max: domain.xMax, position: \"none\" }]}\n        yAxis={[{ id: \"y\", scaleType: \"linear\", min: domain.yMin, max: domain.yMax, position: \"none\" }]}\n        skipAnimation\n      >\n        <HierarchicalTreeOverlay />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}