{"spec_id":"network-force-directed","library":"muix","language":"javascript","code":"// anyplot.ai\n// network-force-directed: Force-Directed Graph\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-24\n//# anyplot-orientation: square\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-force-directed · javascript · muix · anyplot.ai\";\nconst TITLE_FONT_SIZE = 22;\nconst TITLE_H = 46;\nconst LEGEND_H = 36;\nconst CAPTION_H = 38;\nconst CHART_MARGIN = 8;\nconst SIM_AREA_SPAN = 1000;\nconst RING_RADIUS_SHARE = 0.26;\nconst SUBRING_RADIUS_SHARE = 0.07;\nconst JITTER_SHARE = 0.024;\nconst ITERATIONS = 300;\nconst INITIAL_TEMP_SHARE = 0.08;\nconst GRAVITY_FACTOR = 0.01;\nconst ROTATION_STEPS = 180;\nconst PAD_X_PX = 80;\nconst PAD_Y_PX = 56;\nconst MIN_NODE_R = 7;\nconst MAX_NODE_R_SPAN = 16;\nconst HUB_HALO_OFFSET = 7;\n\n// Columns: set these to your data's column names\nconst NODE = \"Researcher\"; // role: node\nconst GROUP = \"Research field\"; // role: group\nconst SOURCE = \"Source researcher\"; // role: source\nconst TARGET = \"Target researcher\"; // role: target\nconst WEIGHT = \"Co-authored papers\"; // role: weight\n\n// Data: replace this block with your own records\nconst nodes: Array<Record<string, string>> = [\n  // Machine Learning (8 researchers)\n  { [NODE]: \"Okafor\", [GROUP]: \"Machine Learning\" },\n  { [NODE]: \"Lindqvist\", [GROUP]: \"Machine Learning\" },\n  { [NODE]: \"Tanaka\", [GROUP]: \"Machine Learning\" },\n  { [NODE]: \"Moreau\", [GROUP]: \"Machine Learning\" },\n  { [NODE]: \"Haddad\", [GROUP]: \"Machine Learning\" },\n  { [NODE]: \"Silva\", [GROUP]: \"Machine Learning\" },\n  { [NODE]: \"Kowalski\", [GROUP]: \"Machine Learning\" },\n  { [NODE]: \"Brennan\", [GROUP]: \"Machine Learning\" },\n\n  // Robotics (6 researchers)\n  { [NODE]: \"Nakamura\", [GROUP]: \"Robotics\" },\n  { [NODE]: \"Fischer\", [GROUP]: \"Robotics\" },\n  { [NODE]: \"Adeyemi\", [GROUP]: \"Robotics\" },\n  { [NODE]: \"Rossi\", [GROUP]: \"Robotics\" },\n  { [NODE]: \"Petrov\", [GROUP]: \"Robotics\" },\n  { [NODE]: \"Chen\", [GROUP]: \"Robotics\" },\n\n  // Bioinformatics (7 researchers)\n  { [NODE]: \"Iyer\", [GROUP]: \"Bioinformatics\" },\n  { [NODE]: \"Novak\", [GROUP]: \"Bioinformatics\" },\n  { [NODE]: \"Garcia\", [GROUP]: \"Bioinformatics\" },\n  { [NODE]: \"Holm\", [GROUP]: \"Bioinformatics\" },\n  { [NODE]: \"Abiodun\", [GROUP]: \"Bioinformatics\" },\n  { [NODE]: \"Larsen\", [GROUP]: \"Bioinformatics\" },\n  { [NODE]: \"Wojcik\", [GROUP]: \"Bioinformatics\" },\n\n  // Network Science (5 researchers)\n  { [NODE]: \"Varga\", [GROUP]: \"Network Science\" },\n  { [NODE]: \"Duarte\", [GROUP]: \"Network Science\" },\n  { [NODE]: \"Sato\", [GROUP]: \"Network Science\" },\n  { [NODE]: \"Keller\", [GROUP]: \"Network Science\" },\n  { [NODE]: \"Amari\", [GROUP]: \"Network Science\" },\n\n  // Cryptography (6 researchers)\n  { [NODE]: \"Ivanova\", [GROUP]: \"Cryptography\" },\n  { [NODE]: \"Osei\", [GROUP]: \"Cryptography\" },\n  { [NODE]: \"Bauer\", [GROUP]: \"Cryptography\" },\n  { [NODE]: \"Mendez\", [GROUP]: \"Cryptography\" },\n  { [NODE]: \"Park\", [GROUP]: \"Cryptography\" },\n  { [NODE]: \"Quinn\", [GROUP]: \"Cryptography\" },\n];\n\nconst data: Array<Record<string, string | number>> = [\n  // Machine Learning: a lab lead with a dense core and two sub-teams\n  { [SOURCE]: \"Okafor\", [TARGET]: \"Lindqvist\", [WEIGHT]: 6 },\n  { [SOURCE]: \"Okafor\", [TARGET]: \"Tanaka\", [WEIGHT]: 4 },\n  { [SOURCE]: \"Okafor\", [TARGET]: \"Moreau\", [WEIGHT]: 3 },\n  { [SOURCE]: \"Okafor\", [TARGET]: \"Haddad\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Lindqvist\", [TARGET]: \"Tanaka\", [WEIGHT]: 3 },\n  { [SOURCE]: \"Lindqvist\", [TARGET]: \"Silva\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Tanaka\", [TARGET]: \"Kowalski\", [WEIGHT]: 1 },\n  { [SOURCE]: \"Silva\", [TARGET]: \"Kowalski\", [WEIGHT]: 4 },\n  { [SOURCE]: \"Silva\", [TARGET]: \"Haddad\", [WEIGHT]: 1 },\n  { [SOURCE]: \"Haddad\", [TARGET]: \"Brennan\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Moreau\", [TARGET]: \"Brennan\", [WEIGHT]: 1 },\n\n  // Robotics: two close collaborators carry the group\n  { [SOURCE]: \"Nakamura\", [TARGET]: \"Fischer\", [WEIGHT]: 5 },\n  { [SOURCE]: \"Nakamura\", [TARGET]: \"Adeyemi\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Fischer\", [TARGET]: \"Rossi\", [WEIGHT]: 3 },\n  { [SOURCE]: \"Fischer\", [TARGET]: \"Petrov\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Rossi\", [TARGET]: \"Petrov\", [WEIGHT]: 4 },\n  { [SOURCE]: \"Adeyemi\", [TARGET]: \"Chen\", [WEIGHT]: 1 },\n  { [SOURCE]: \"Petrov\", [TARGET]: \"Chen\", [WEIGHT]: 2 },\n\n  // Bioinformatics: a star around one core-facility head\n  { [SOURCE]: \"Iyer\", [TARGET]: \"Novak\", [WEIGHT]: 4 },\n  { [SOURCE]: \"Iyer\", [TARGET]: \"Garcia\", [WEIGHT]: 5 },\n  { [SOURCE]: \"Iyer\", [TARGET]: \"Holm\", [WEIGHT]: 3 },\n  { [SOURCE]: \"Iyer\", [TARGET]: \"Abiodun\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Iyer\", [TARGET]: \"Larsen\", [WEIGHT]: 3 },\n  { [SOURCE]: \"Iyer\", [TARGET]: \"Wojcik\", [WEIGHT]: 1 },\n  { [SOURCE]: \"Novak\", [TARGET]: \"Garcia\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Holm\", [TARGET]: \"Larsen\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Abiodun\", [TARGET]: \"Wojcik\", [WEIGHT]: 3 },\n\n  // Network Science: a small loop with one outlier\n  { [SOURCE]: \"Varga\", [TARGET]: \"Duarte\", [WEIGHT]: 3 },\n  { [SOURCE]: \"Duarte\", [TARGET]: \"Sato\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Sato\", [TARGET]: \"Keller\", [WEIGHT]: 4 },\n  { [SOURCE]: \"Keller\", [TARGET]: \"Varga\", [WEIGHT]: 1 },\n  { [SOURCE]: \"Duarte\", [TARGET]: \"Amari\", [WEIGHT]: 2 },\n\n  // Cryptography: a tight trio trailing into a chain\n  { [SOURCE]: \"Ivanova\", [TARGET]: \"Osei\", [WEIGHT]: 5 },\n  { [SOURCE]: \"Ivanova\", [TARGET]: \"Bauer\", [WEIGHT]: 4 },\n  { [SOURCE]: \"Osei\", [TARGET]: \"Bauer\", [WEIGHT]: 3 },\n  { [SOURCE]: \"Bauer\", [TARGET]: \"Mendez\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Mendez\", [TARGET]: \"Park\", [WEIGHT]: 3 },\n  { [SOURCE]: \"Park\", [TARGET]: \"Quinn\", [WEIGHT]: 1 },\n\n  // Cross-field collaborations\n  { [SOURCE]: \"Okafor\", [TARGET]: \"Nakamura\", [WEIGHT]: 3 },\n  { [SOURCE]: \"Tanaka\", [TARGET]: \"Iyer\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Kowalski\", [TARGET]: \"Fischer\", [WEIGHT]: 1 },\n  { [SOURCE]: \"Garcia\", [TARGET]: \"Duarte\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Sato\", [TARGET]: \"Silva\", [WEIGHT]: 1 },\n  { [SOURCE]: \"Keller\", [TARGET]: \"Mendez\", [WEIGHT]: 1 },\n  { [SOURCE]: \"Moreau\", [TARGET]: \"Osei\", [WEIGHT]: 2 },\n  { [SOURCE]: \"Chen\", [TARGET]: \"Holm\", [WEIGHT]: 1 },\n];\n\n// Plot: reads only data and the constants above\nconst groups: string[] = [];\nfor (const row of nodes) {\n  const grp = String(row[GROUP]);\n  if (!groups.includes(grp)) groups.push(grp);\n}\n\ninterface ForceNode {\n  id: string;\n  groupIndex: number;\n  groupName: string;\n  isHub: boolean;\n  withinIndex: number;\n  degree: number;\n  radius: number;\n  x: number;\n  y: number;\n}\n\nconst nodeIndexById = new Map<string, number>();\nconst layoutNodes: ForceNode[] = [];\nconst groupCounts = new Array<number>(groups.length).fill(0);\n\nfor (const row of nodes) {\n  const id = String(row[NODE]);\n  const grp = String(row[GROUP]);\n  const groupIndex = groups.indexOf(grp);\n  const withinIndex = groupCounts[groupIndex];\n  groupCounts[groupIndex] += 1;\n  nodeIndexById.set(id, layoutNodes.length);\n  layoutNodes.push({\n    id,\n    groupIndex,\n    groupName: grp,\n    isHub: false,\n    withinIndex,\n    degree: 0,\n    radius: MIN_NODE_R,\n    x: 0,\n    y: 0,\n  });\n}\n\ninterface ForceEdge {\n  sourceIdx: number;\n  targetIdx: number;\n  weight: number;\n  wRatio: number;\n  isBridge: boolean;\n}\n\nconst edges: ForceEdge[] = [];\nfor (const row of data) {\n  const a = nodeIndexById.get(String(row[SOURCE]))!;\n  const b = nodeIndexById.get(String(row[TARGET]))!;\n  const weight = Number(row[WEIGHT]);\n  const isBridge = layoutNodes[a].groupIndex !== layoutNodes[b].groupIndex;\n  edges.push({ sourceIdx: a, targetIdx: b, weight, wRatio: 0, isBridge });\n  layoutNodes[a].degree += 1;\n  layoutNodes[b].degree += 1;\n}\n\nconst minW = Math.min(...edges.map((e) => e.weight));\nconst maxW = Math.max(...edges.map((e) => e.weight));\nconst wSpan = maxW - minW || 1;\nfor (const edge of edges) {\n  edge.wRatio = (edge.weight - minW) / wSpan;\n}\n\n// Identify the primary hub in each group by highest degree\nfor (let g = 0; g < groups.length; g += 1) {\n  let bestIdx = -1;\n  let bestDeg = -1;\n  for (let i = 0; i < layoutNodes.length; i += 1) {\n    if (layoutNodes[i].groupIndex === g && layoutNodes[i].degree > bestDeg) {\n      bestDeg = layoutNodes[i].degree;\n      bestIdx = i;\n    }\n  }\n  if (bestIdx >= 0) {\n    layoutNodes[bestIdx].isHub = true;\n  }\n}\n\nconst maxDegree = Math.max(...layoutNodes.map((n) => n.degree));\nfor (const node of layoutNodes) {\n  node.radius = MIN_NODE_R + (node.degree / maxDegree) * MAX_NODE_R_SPAN;\n}\n\nlet lcgState = 104729;\nconst ringRadius = SIM_AREA_SPAN * RING_RADIUS_SHARE;\nconst subringRadius = SIM_AREA_SPAN * SUBRING_RADIUS_SHARE;\nconst jitterSpan = SIM_AREA_SPAN * JITTER_SHARE;\n\nfor (const node of layoutNodes) {\n  const groupAngle = (node.groupIndex / groups.length) * Math.PI * 2;\n  const centerX = Math.cos(groupAngle) * ringRadius;\n  const centerY = Math.sin(groupAngle) * ringRadius;\n  const groupCount = groupCounts[node.groupIndex];\n  const withinAngle = (node.withinIndex / groupCount) * Math.PI * 2;\n  lcgState = (lcgState * 1664525 + 1013904223) % 4294967296;\n  const jx = (lcgState / 4294967296 - 0.5) * jitterSpan;\n  lcgState = (lcgState * 1664525 + 1013904223) % 4294967296;\n  const jy = (lcgState / 4294967296 - 0.5) * jitterSpan;\n  node.x = centerX + Math.cos(withinAngle) * subringRadius + jx;\n  node.y = centerY + Math.sin(withinAngle) * subringRadius + jy;\n}\n\nconst idealDistance = Math.sqrt((SIM_AREA_SPAN * SIM_AREA_SPAN) / layoutNodes.length);\nlet temperature = SIM_AREA_SPAN * INITIAL_TEMP_SHARE;\nconst cooling = temperature / ITERATIONS;\n\nfor (let iter = 0; iter < ITERATIONS; iter += 1) {\n  const dispX = new Array<number>(layoutNodes.length).fill(0);\n  const dispY = new Array<number>(layoutNodes.length).fill(0);\n\n  for (let i = 0; i < layoutNodes.length; i += 1) {\n    for (let j = i + 1; j < layoutNodes.length; j += 1) {\n      const dx = layoutNodes[i].x - layoutNodes[j].x;\n      const dy = layoutNodes[i].y - layoutNodes[j].y;\n      const dist = Math.sqrt(dx * dx + dy * dy) || 0.01;\n      const force = (idealDistance * idealDistance) / dist;\n      const fx = (dx / dist) * force;\n      const fy = (dy / dist) * force;\n      dispX[i] += fx;\n      dispY[i] += fy;\n      dispX[j] -= fx;\n      dispY[j] -= fy;\n    }\n  }\n\n  for (const edge of edges) {\n    const a = edge.sourceIdx;\n    const b = edge.targetIdx;\n    const dx = layoutNodes[a].x - layoutNodes[b].x;\n    const dy = layoutNodes[a].y - layoutNodes[b].y;\n    const dist = Math.sqrt(dx * dx + dy * dy) || 0.01;\n    const force = ((dist * dist) / idealDistance) * (0.6 + edge.wRatio * 0.6);\n    const fx = (dx / dist) * force;\n    const fy = (dy / dist) * force;\n    dispX[a] -= fx;\n    dispY[a] -= fy;\n    dispX[b] += fx;\n    dispY[b] += fy;\n  }\n\n  for (let i = 0; i < layoutNodes.length; i += 1) {\n    dispX[i] -= layoutNodes[i].x * GRAVITY_FACTOR;\n    dispY[i] -= layoutNodes[i].y * GRAVITY_FACTOR;\n    const dist = Math.sqrt(dispX[i] * dispX[i] + dispY[i] * dispY[i]) || 0.01;\n    const capped = Math.min(dist, temperature);\n    layoutNodes[i].x += (dispX[i] / dist) * capped;\n    layoutNodes[i].y += (dispY[i] / dist) * capped;\n  }\n\n  temperature = Math.max(temperature - cooling, 1);\n}\n\n// A force layout has no meaningful orientation: rotate it to the angle that\n// lets it fill the drawing area best, then fit the domain to the pixel aspect\nconst plotW = window.ANYPLOT_SIZE.width - CHART_MARGIN * 2;\nconst plotH = window.ANYPLOT_SIZE.height - TITLE_H - LEGEND_H - CAPTION_H - CHART_MARGIN * 2;\nconst fitW = plotW - PAD_X_PX * 2;\nconst fitH = plotH - PAD_Y_PX * 2;\n\nlet bestAngle = 0;\nlet bestScale = 0;\nfor (let step = 0; step < ROTATION_STEPS; step += 1) {\n  const angle = (step / ROTATION_STEPS) * Math.PI;\n  const cos = Math.cos(angle);\n  const sin = Math.sin(angle);\n  const rx = layoutNodes.map((n) => n.x * cos - n.y * sin);\n  const ry = layoutNodes.map((n) => n.x * sin + n.y * cos);\n  const scale = Math.min(\n    fitW / (Math.max(...rx) - Math.min(...rx)),\n    fitH / (Math.max(...ry) - Math.min(...ry)),\n  );\n  if (scale > bestScale) {\n    bestScale = scale;\n    bestAngle = angle;\n  }\n}\n\nfor (const node of layoutNodes) {\n  const x = node.x * Math.cos(bestAngle) - node.y * Math.sin(bestAngle);\n  const y = node.x * Math.sin(bestAngle) + node.y * Math.cos(bestAngle);\n  node.x = x;\n  node.y = y;\n}\n\nconst xs = layoutNodes.map((n) => n.x);\nconst ys = layoutNodes.map((n) => n.y);\nconst xMid = (Math.min(...xs) + Math.max(...xs)) / 2;\nconst yMid = (Math.min(...ys) + Math.max(...ys)) / 2;\nconst halfSpanX = plotW / 2 / bestScale;\nconst halfSpanY = plotH / 2 / bestScale;\nconst domain = {\n  xMin: xMid - halfSpanX,\n  xMax: xMid + halfSpanX,\n  yMin: yMid - halfSpanY,\n  yMax: yMid + halfSpanY,\n};\n\nfunction ForceNetworkOverlay() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  return (\n    <g data-drawing-container>\n      {edges.map((edge, i) => (\n        <line\n          key={`edge-${i}`}\n          x1={xScale(layoutNodes[edge.sourceIdx].x)}\n          y1={yScale(layoutNodes[edge.sourceIdx].y)}\n          x2={xScale(layoutNodes[edge.targetIdx].x)}\n          y2={yScale(layoutNodes[edge.targetIdx].y)}\n          stroke={t.inkSoft}\n          strokeOpacity={edge.isBridge ? 0.32 : 0.42}\n          strokeWidth={1.1 + edge.wRatio * 2.2}\n          strokeDasharray={edge.isBridge ? \"5,4\" : undefined}\n        />\n      ))}\n      {layoutNodes.map((node) => (\n        <React.Fragment key={node.id}>\n          {node.isHub && (\n            <circle\n              cx={xScale(node.x)}\n              cy={yScale(node.y)}\n              r={node.radius + HUB_HALO_OFFSET}\n              fill={t.palette[node.groupIndex]}\n              opacity={0.2}\n            />\n          )}\n          <circle\n            cx={xScale(node.x)}\n            cy={yScale(node.y)}\n            r={node.radius}\n            fill={t.palette[node.groupIndex]}\n            stroke={t.pageBg}\n            strokeWidth={1.5}\n          />\n          {node.isHub && (\n            <g>\n              <rect\n                x={Number(xScale(node.x)) - (node.groupName.length * 4.4 + 8)}\n                y={Number(yScale(node.y)) - node.radius - 24}\n                width={node.groupName.length * 8.8 + 16}\n                height={20}\n                rx={4}\n                fill={t.pageBg}\n                fillOpacity={0.88}\n              />\n              <text\n                x={xScale(node.x)}\n                y={Number(yScale(node.y)) - node.radius - 10}\n                textAnchor=\"middle\"\n                fontSize={15}\n                fontWeight={500}\n                fill={t.ink}\n              >\n                {node.groupName}\n              </text>\n            </g>\n          )}\n        </React.Fragment>\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 - CAPTION_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        {groups.map((groupName, i) => (\n          <div key={groupName} 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 }}>{groupName}</span>\n          </div>\n        ))}\n      </div>\n      <ChartsContainer\n        width={width}\n        height={chartHeight}\n        series={[]}\n        margin={{ top: CHART_MARGIN, bottom: CHART_MARGIN, left: CHART_MARGIN, right: CHART_MARGIN }}\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        <ForceNetworkOverlay />\n      </ChartsContainer>\n      <div\n        style={{\n          height: CAPTION_H,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          fontSize: 15,\n          color: t.inkSoft,\n        }}\n      >\n        {`Node size = degree · Halo & label = top hub per ${GROUP} · Edge width = ${WEIGHT} · Dashed = cross-field link`}\n      </div>\n    </div>\n  );\n}\n"}