{"spec_id":"network-directed","library":"muix","language":"javascript","code":"// anyplot.ai\n// network-directed: Directed Network Graph\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 84/100 | Updated: 2026-10-24\n\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { useDrawingArea, useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TITLE = \"network-directed · javascript · muix · anyplot.ai\";\nconst TITLE_HEIGHT = 64;\nconst MARGIN = { top: 28, right: 260, bottom: 32, left: 44 };\nconst NODE_HALF_HEIGHT = 21;\nconst NODE_PAD_X = 16;\nconst CHAR_WIDTH = 8.6;\nconst LABEL_SIZE = 15;\nconst END_GAP = 6;\nconst PAD_X_SHARE = 0.14;\nconst PAD_Y_SHARE = 0.25;\n\n// Columns: set these to your data's column names\nconst MODULE = \"Module\"; // role: node\nconst LAYER = \"Architecture layer\"; // role: group\nconst SOURCE = \"Importer module\"; // role: source\nconst TARGET = \"Imported module\"; // role: target\nconst WEIGHT = \"Import calls\"; // role: weight\n\n// Data: replace this block with your own records\nconst nodes: Array<Record<string, string>> = [\n  { [MODULE]: \"utils\", [LAYER]: \"Foundation\" },\n  { [MODULE]: \"config\", [LAYER]: \"Foundation\" },\n  { [MODULE]: \"logger\", [LAYER]: \"Core Services\" },\n  { [MODULE]: \"auth\", [LAYER]: \"Core Services\" },\n  { [MODULE]: \"cache\", [LAYER]: \"Core Services\" },\n  { [MODULE]: \"orders\", [LAYER]: \"Domain Logic\" },\n  { [MODULE]: \"billing\", [LAYER]: \"Domain Logic\" },\n  { [MODULE]: \"inventory\", [LAYER]: \"Domain Logic\" },\n  { [MODULE]: \"rest-api\", [LAYER]: \"API Layer\" },\n  { [MODULE]: \"graphql-api\", [LAYER]: \"API Layer\" },\n  { [MODULE]: \"web-app\", [LAYER]: \"UI & Apps\" },\n  { [MODULE]: \"mobile-app\", [LAYER]: \"UI & Apps\" },\n  { [MODULE]: \"admin-panel\", [LAYER]: \"UI & Apps\" },\n];\n\nconst data: Array<Record<string, string | number>> = [\n  { [SOURCE]: \"config\", [TARGET]: \"utils\", [WEIGHT]: 2 },\n  { [SOURCE]: \"logger\", [TARGET]: \"utils\", [WEIGHT]: 3 },\n  { [SOURCE]: \"auth\", [TARGET]: \"utils\", [WEIGHT]: 2 },\n  { [SOURCE]: \"auth\", [TARGET]: \"config\", [WEIGHT]: 1 },\n  { [SOURCE]: \"cache\", [TARGET]: \"utils\", [WEIGHT]: 2 },\n  { [SOURCE]: \"orders\", [TARGET]: \"auth\", [WEIGHT]: 2 },\n  { [SOURCE]: \"orders\", [TARGET]: \"logger\", [WEIGHT]: 3 },\n  { [SOURCE]: \"orders\", [TARGET]: \"cache\", [WEIGHT]: 2 },\n  { [SOURCE]: \"inventory\", [TARGET]: \"cache\", [WEIGHT]: 2 },\n  { [SOURCE]: \"inventory\", [TARGET]: \"logger\", [WEIGHT]: 2 },\n  { [SOURCE]: \"billing\", [TARGET]: \"auth\", [WEIGHT]: 2 },\n  { [SOURCE]: \"billing\", [TARGET]: \"orders\", [WEIGHT]: 1 },\n  { [SOURCE]: \"rest-api\", [TARGET]: \"orders\", [WEIGHT]: 3 },\n  { [SOURCE]: \"rest-api\", [TARGET]: \"inventory\", [WEIGHT]: 2 },\n  { [SOURCE]: \"rest-api\", [TARGET]: \"billing\", [WEIGHT]: 2 },\n  { [SOURCE]: \"graphql-api\", [TARGET]: \"orders\", [WEIGHT]: 3 },\n  { [SOURCE]: \"graphql-api\", [TARGET]: \"billing\", [WEIGHT]: 1 },\n  { [SOURCE]: \"web-app\", [TARGET]: \"rest-api\", [WEIGHT]: 3 },\n  { [SOURCE]: \"web-app\", [TARGET]: \"graphql-api\", [WEIGHT]: 2 },\n  { [SOURCE]: \"mobile-app\", [TARGET]: \"graphql-api\", [WEIGHT]: 2 },\n  { [SOURCE]: \"admin-panel\", [TARGET]: \"rest-api\", [WEIGHT]: 2 },\n  { [SOURCE]: \"admin-panel\", [TARGET]: \"inventory\", [WEIGHT]: 1 },\n  { [SOURCE]: \"config\", [TARGET]: \"auth\", [WEIGHT]: 1 },\n];\n\n// Plot: reads only data and the constants above\ninterface ModuleNode {\n  id: string;\n  layer: string;\n  depth: number;\n  x: number;\n  y: number;\n  halfWidth: number;\n}\n\n// Distance from a node's centre to its border along the direction (dx, dy)\nfunction borderDistance(n: ModuleNode, dx: number, dy: number) {\n  const dist = Math.hypot(dx, dy);\n  const ux = Math.abs(dx / dist);\n  const uy = Math.abs(dy / dist);\n  return Math.min(ux > 0 ? n.halfWidth / ux : Infinity, uy > 0 ? NODE_HALF_HEIGHT / uy : Infinity);\n}\n\nconst layerNames: string[] = [];\nfor (let i = 0; i < nodes.length; i++) {\n  const layer = String(nodes[i][LAYER]);\n  if (!layerNames.includes(layer)) layerNames.push(layer);\n}\n\nconst rawModules = nodes.map((row) => {\n  const id = String(row[MODULE]);\n  const layer = String(row[LAYER]);\n  const depth = layerNames.indexOf(layer);\n  return { id, layer, depth };\n});\n\nconst flagColor = t.palette[4];\nconst unusedPaletteSlots = [t.palette[0], t.palette[1], t.palette[2], t.palette[3], t.palette[5]];\nconst layerColors = new Map<string, string>();\nfor (let i = 0; i < layerNames.length; i++) {\n  layerColors.set(layerNames[i], unusedPaletteSlots[i]);\n}\n\nconst depthCounts = new Map<number, number>();\nfor (let i = 0; i < rawModules.length; i++) {\n  const d = rawModules[i].depth;\n  depthCounts.set(d, (depthCounts.get(d) ?? 0) + 1);\n}\n\nconst depthSeen = new Map<number, number>();\nconst layoutNodes: ModuleNode[] = [];\nconst nodeById = new Map<string, ModuleNode>();\n\nfor (let i = 0; i < rawModules.length; i++) {\n  const m = rawModules[i];\n  const seen = depthSeen.get(m.depth) ?? 0;\n  depthSeen.set(m.depth, seen + 1);\n  const count = Number(depthCounts.get(m.depth));\n  const node: ModuleNode = {\n    id: m.id,\n    layer: m.layer,\n    depth: m.depth,\n    x: m.depth,\n    y: seen - (count - 1) / 2,\n    halfWidth: (m.id.length * CHAR_WIDTH) / 2 + NODE_PAD_X,\n  };\n  layoutNodes.push(node);\n  nodeById.set(node.id, node);\n}\n\nconst edgeKeys = new Set<string>();\nfor (let i = 0; i < data.length; i++) {\n  edgeKeys.add(`${data[i][SOURCE]}>${data[i][TARGET]}`);\n}\n\nconst edges = data.map((row) => {\n  const source = String(row[SOURCE]);\n  const target = String(row[TARGET]);\n  const weight = Number(row[WEIGHT]);\n  const srcNode = nodeById.get(source)!;\n  const tgtNode = nodeById.get(target)!;\n  const bidirectional = edgeKeys.has(`${target}>${source}`);\n  const flagged = srcNode.depth < tgtNode.depth;\n  return { source, target, weight, bidirectional, flagged };\n});\n\nconst xs = layoutNodes.map((n) => n.x);\nconst ys = layoutNodes.map((n) => n.y);\nconst minX = Math.min(...xs);\nconst maxX = Math.max(...xs);\nconst minY = Math.min(...ys);\nconst maxY = Math.max(...ys);\nconst xPad = (maxX - minX) * PAD_X_SHARE;\nconst yPad = (maxY - minY) * PAD_Y_SHARE;\nconst xAxisMin = minX - xPad;\nconst xAxisMax = maxX + xPad;\nconst yAxisMin = minY - yPad;\nconst yAxisMax = maxY + yPad;\n\nfunction DirectedNetworkOverlay() {\n  const { left, top, width } = useDrawingArea();\n  const xScale = useXScale();\n  const yScale = useYScale();\n\n  const legendX = left + width + 36;\n  const legendY = top + 12;\n\n  return (\n    <g>\n      <defs>\n        <marker\n          id=\"np-arrow\"\n          viewBox=\"0 0 12 10\"\n          refX=\"11\"\n          refY=\"5\"\n          markerWidth=\"13\"\n          markerHeight=\"11\"\n          markerUnits=\"userSpaceOnUse\"\n          orient=\"auto-start-reverse\"\n        >\n          <path d=\"M0,0 L12,5 L0,10 Z\" fill={t.inkSoft} />\n        </marker>\n        <marker\n          id=\"np-arrow-flag\"\n          viewBox=\"0 0 12 10\"\n          refX=\"11\"\n          refY=\"5\"\n          markerWidth=\"15\"\n          markerHeight=\"13\"\n          markerUnits=\"userSpaceOnUse\"\n          orient=\"auto-start-reverse\"\n        >\n          <path d=\"M0,0 L12,5 L0,10 Z\" fill={flagColor} />\n        </marker>\n      </defs>\n\n      {edges.map((e, i) => {\n        const s = nodeById.get(e.source)!;\n        const d = nodeById.get(e.target)!;\n        const x1 = Number(xScale(s.x));\n        const y1 = Number(yScale(s.y));\n        const x2 = Number(xScale(d.x));\n        const y2 = Number(yScale(d.y));\n\n        let pathD = \"\";\n        if (!e.bidirectional) {\n          const dx = x2 - x1;\n          const dy = y2 - y1;\n          const dist = Math.hypot(dx, dy);\n          const stop = borderDistance(d, dx, dy) + END_GAP;\n          const ex = x2 - (dx / dist) * stop;\n          const ey = y2 - (dy / dist) * stop;\n          pathD = `M ${x1} ${y1} L ${ex} ${ey}`;\n        } else {\n          const mx = (x1 + x2) / 2;\n          const my = (y1 + y2) / 2;\n          const canonFrom = e.source < e.target ? s : d;\n          const canonTo = e.source < e.target ? d : s;\n          const cdx = Number(xScale(canonTo.x)) - Number(xScale(canonFrom.x));\n          const cdy = Number(yScale(canonTo.y)) - Number(yScale(canonFrom.y));\n          const cdist = Math.hypot(cdx, cdy);\n          const nx = -cdy / cdist;\n          const ny = cdx / cdist;\n          const sign = e.source < e.target ? 1 : -1;\n          const bow = Math.max(55, cdist * 0.4) * sign;\n          const cx = mx + nx * bow;\n          const cy = my + ny * bow;\n          const tdx = x2 - cx;\n          const tdy = y2 - cy;\n          const tdist = Math.hypot(tdx, tdy);\n          const stop = borderDistance(d, tdx, tdy) + END_GAP;\n          const ex = x2 - (tdx / tdist) * stop;\n          const ey = y2 - (tdy / tdist) * stop;\n          pathD = `M ${x1} ${y1} Q ${cx} ${cy} ${ex} ${ey}`;\n        }\n\n        return (\n          <path\n            key={`${e.source}-${e.target}-${i}`}\n            d={pathD}\n            fill=\"none\"\n            stroke={e.flagged ? flagColor : t.inkSoft}\n            strokeWidth={1.3 + e.weight * 0.5}\n            strokeOpacity={e.flagged ? 0.95 : 0.68}\n            markerEnd={e.flagged ? \"url(#np-arrow-flag)\" : \"url(#np-arrow)\"}\n          />\n        );\n      })}\n\n      {layoutNodes.map((n) => {\n        const cx = Number(xScale(n.x));\n        const cy = Number(yScale(n.y));\n        const color = String(layerColors.get(n.layer));\n        const box = {\n          x: cx - n.halfWidth,\n          y: cy - NODE_HALF_HEIGHT,\n          width: n.halfWidth * 2,\n          height: NODE_HALF_HEIGHT * 2,\n          rx: 10,\n        };\n        return (\n          <g key={`node-${n.id}`}>\n            <rect {...box} fill={t.pageBg} />\n            <rect {...box} fill={color} fillOpacity={0.24} stroke={color} strokeWidth={3} />\n            <text\n              x={cx}\n              y={cy + 5}\n              textAnchor=\"middle\"\n              fontSize={LABEL_SIZE}\n              fontWeight={600}\n              fill={t.ink}\n            >\n              {n.id}\n            </text>\n          </g>\n        );\n      })}\n\n      <g transform={`translate(${legendX}, ${legendY})`}>\n        <text x={0} y={14} fontSize={15} fontWeight={700} fill={t.ink}>\n          {LAYER}\n        </text>\n        {layerNames.map((lName, idx) => (\n          <g key={lName} transform={`translate(0, ${38 + idx * 32})`}>\n            <rect\n              x={0}\n              y={-9}\n              width={20}\n              height={18}\n              rx={5}\n              fill={String(layerColors.get(lName))}\n              fillOpacity={0.24}\n              stroke={String(layerColors.get(lName))}\n              strokeWidth={2.5}\n            />\n            <text x={28} y={5} fontSize={15} fill={t.ink}>\n              {lName}\n            </text>\n          </g>\n        ))}\n\n        <g transform={`translate(0, ${52 + layerNames.length * 32})`}>\n          <line\n            x1={0}\n            y1={0}\n            x2={32}\n            y2={0}\n            stroke={flagColor}\n            strokeWidth={2.3}\n            markerEnd=\"url(#np-arrow-flag)\"\n          />\n          <text x={44} y={5} fontSize={14} fontWeight={600} fill={t.ink}>\n            Upward dependency\n          </text>\n        </g>\n\n        <g transform={`translate(0, ${92 + layerNames.length * 32})`}>\n          <text x={0} y={0} fontSize={14} fill={t.inkSoft}>\n            {`Edge width = ${WEIGHT}`}\n          </text>\n        </g>\n      </g>\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\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_HEIGHT,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          fontSize: 22,\n          fontWeight: 600,\n          color: t.ink,\n        }}\n      >\n        {TITLE}\n      </div>\n      <ChartsContainer\n        width={width}\n        height={height - TITLE_HEIGHT}\n        margin={MARGIN}\n        skipAnimation\n        series={[]}\n        xAxis={[{ position: \"none\", scaleType: \"linear\", min: xAxisMin, max: xAxisMax }]}\n        yAxis={[{ position: \"none\", scaleType: \"linear\", min: yAxisMin, max: yAxisMax }]}\n      >\n        <DirectedNetworkOverlay />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}