{"spec_id":"hive-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// hive-basic: Basic Hive Plot\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 87/100 | Updated: 2026-10-24\n//# anyplot-orientation: square\n\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ScatterPlot } from \"@mui/x-charts/ScatterChart\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst FONT = '\"Plus Jakarta Sans\", \"Inter\", -apple-system, BlinkMacSystemFont, sans-serif';\nconst MONO = '\"JetBrains Mono\", \"SFMono-Regular\", Menlo, monospace';\nconst START_ANGLE_DEG = 90;\nconst INNER_R = 18;\nconst OUTER_R = 100;\nconst CONTROL_R = 18;\nconst SPOKE_HUB_SHARE = 0.55;\nconst SPOKE_RIM_SHARE = 1.05;\nconst LABEL_R_SHARE = 1.16;\nconst DOMAIN_PAD_SHARE = 1.14;\n\n// Columns: set these to your data's column names\nconst NODE = \"Module\"; // role: nodes\nconst LAYER = \"Layer\"; // role: axis_attribute\nconst SOURCE = \"Source module\"; // role: source\nconst TARGET = \"Target module\"; // role: target\nconst WEIGHT = \"Dependency weight\"; // role: weight\n\n// Data: replace this block with your own records\nconst SEED_LAYERS = [\n  { label: \"Core\", count: 12 },\n  { label: \"Utility\", count: 10 },\n  { label: \"Interface\", count: 10 },\n];\nconst SEED_PAIRS: Array<[number, number]> = [\n  [0, 1],\n  [1, 2],\n  [2, 0],\n];\nconst EDGES_PER_PAIR = 24;\n\nconst nodes: Array<{\n  [NODE]: string;\n  [LAYER]: string;\n}> = [];\nfor (let l = 0; l < SEED_LAYERS.length; l += 1) {\n  const layer = SEED_LAYERS[l];\n  for (let i = 0; i < layer.count; i += 1) {\n    nodes.push({\n      [NODE]: `${layer.label}-${i + 1}`,\n      [LAYER]: layer.label,\n    });\n  }\n}\n\nlet lcgState = 42 >>> 0;\nconst edgeKeys = new Set<string>();\nconst data: Array<{\n  [SOURCE]: string;\n  [TARGET]: string;\n  [WEIGHT]: number;\n}> = [];\n\nfor (let p = 0; p < SEED_PAIRS.length; p += 1) {\n  const layerA = SEED_LAYERS[SEED_PAIRS[p][0]];\n  const layerB = SEED_LAYERS[SEED_PAIRS[p][1]];\n  let added = 0;\n  let attempts = 0;\n  while (added < EDGES_PER_PAIR && attempts < EDGES_PER_PAIR * 6) {\n    attempts += 1;\n    lcgState = (Math.imul(lcgState, 1664525) + 1013904223) >>> 0;\n    const idxA = Math.floor((lcgState / 4294967296) * layerA.count);\n    lcgState = (Math.imul(lcgState, 1664525) + 1013904223) >>> 0;\n    const idxB = Math.floor((lcgState / 4294967296) * layerB.count);\n    lcgState = (Math.imul(lcgState, 1664525) + 1013904223) >>> 0;\n    const weight = 1 + Math.floor((lcgState / 4294967296) * 5);\n\n    const srcId = `${layerA.label}-${idxA + 1}`;\n    const tgtId = `${layerB.label}-${idxB + 1}`;\n    const key = `${srcId}|${tgtId}`;\n    if (edgeKeys.has(key)) continue;\n    edgeKeys.add(key);\n\n    data.push({\n      [SOURCE]: srcId,\n      [TARGET]: tgtId,\n      [WEIGHT]: weight,\n    });\n    added += 1;\n  }\n}\n\n// Plot: reads only data and the constants above\nconst axisLabels = Array.from(new Set(nodes.map((n) => n[LAYER])));\n\nconst AXES = axisLabels.map((label, idx) => {\n  const angle = START_ANGLE_DEG + (idx * 360) / axisLabels.length;\n  const rad = (angle * Math.PI) / 180;\n  return {\n    id: label,\n    label,\n    angle,\n    cos: Math.cos(rad),\n    sin: Math.sin(rad),\n    color: t.palette[idx],\n  };\n});\nconst axisById = Object.fromEntries(AXES.map((a) => [a.id, a]));\n\ninterface HiveNode {\n  id: string;\n  axis: string;\n  degree: number;\n  r: number;\n  x: number;\n  y: number;\n}\n\nconst nodeMap = new Map<string, HiveNode>();\nfor (let i = 0; i < nodes.length; i += 1) {\n  const row = nodes[i];\n  nodeMap.set(row[NODE], {\n    id: row[NODE],\n    axis: row[LAYER],\n    degree: 0,\n    r: INNER_R,\n    x: 0,\n    y: 0,\n  });\n}\n\nfor (let i = 0; i < data.length; i += 1) {\n  const row = data[i];\n  const srcNode = nodeMap.get(row[SOURCE]);\n  const tgtNode = nodeMap.get(row[TARGET]);\n  if (srcNode) srcNode.degree += 1;\n  if (tgtNode) tgtNode.degree += 1;\n}\n\nconst allNodes = Array.from(nodeMap.values());\nconst degrees = allNodes.map((n) => n.degree);\nconst MIN_DEGREE = Math.min(...degrees);\nconst MAX_DEGREE = Math.max(...degrees);\nconst weights = data.map((d) => d[WEIGHT]);\nconst MIN_WEIGHT = Math.min(...weights);\nconst MAX_WEIGHT = Math.max(...weights);\n\nconst nodesByAxis: Record<string, HiveNode[]> = {};\nfor (let a = 0; a < AXES.length; a += 1) {\n  const axis = AXES[a];\n  const ordered = allNodes\n    .filter((n) => n.axis === axis.id)\n    .sort((n1, n2) => n1.degree - n2.degree || n1.id.localeCompare(n2.id));\n  for (let i = 0; i < ordered.length; i += 1) {\n    const node = ordered[i];\n    const r =\n      ordered.length > 1\n        ? INNER_R + (i / (ordered.length - 1)) * (OUTER_R - INNER_R)\n        : OUTER_R;\n    node.r = r;\n    node.x = r * axis.cos;\n    node.y = r * axis.sin;\n  }\n  nodesByAxis[axis.id] = ordered;\n}\n\nconst LABEL_R = OUTER_R * LABEL_R_SHARE;\nconst axisTipXs = AXES.map((a) => LABEL_R * a.cos);\nconst axisTipYs = AXES.map((a) => LABEL_R * a.sin);\nconst minTipX = Math.min(...axisTipXs);\nconst maxTipX = Math.max(...axisTipXs);\nconst minTipY = Math.min(...axisTipYs);\nconst maxTipY = Math.max(...axisTipYs);\n\nconst contentXCenter = (minTipX + maxTipX) / 2;\nconst contentYCenter = (minTipY + maxTipY) / 2;\nconst X_HALF_SPAN = ((maxTipX - minTipX) / 2) * DOMAIN_PAD_SHARE;\nconst Y_HALF_SPAN = ((maxTipY - minTipY) / 2) * DOMAIN_PAD_SHARE;\nconst X_MIN = contentXCenter - X_HALF_SPAN;\nconst X_MAX = contentXCenter + X_HALF_SPAN;\nconst Y_MIN = contentYCenter - Y_HALF_SPAN;\nconst Y_MAX = contentYCenter + Y_HALF_SPAN;\n\nconst HEADER_HEIGHT = 112;\nconst MIN_MARGIN = 48;\nconst FRAME_W = window.ANYPLOT_SIZE.width;\nconst FRAME_H = window.ANYPLOT_SIZE.height - HEADER_HEIGHT;\nconst domainRatio = X_HALF_SPAN / Y_HALF_SPAN;\nconst innerW = FRAME_W - 2 * MIN_MARGIN;\nconst innerH = FRAME_H - 2 * MIN_MARGIN;\nconst plotW = innerW / innerH > domainRatio ? innerH * domainRatio : innerW;\nconst plotH = innerW / innerH > domainRatio ? innerH : innerW / domainRatio;\nconst MARGIN = {\n  top: Math.round((FRAME_H - plotH) / 2),\n  bottom: Math.round((FRAME_H - plotH) / 2),\n  left: Math.round((FRAME_W - plotW) / 2),\n  right: Math.round((FRAME_W - plotW) / 2),\n};\n\nfunction HiveGeometry() {\n  const xScale = useXScale() as (v: number) => number;\n  const yScale = useYScale() as (v: number) => number;\n\n  return (\n    <g>\n      {data.map((row, i) => {\n        const source = nodeMap.get(row[SOURCE])!;\n        const target = nodeMap.get(row[TARGET])!;\n        const axisA = axisById[source.axis];\n        const axisB = axisById[target.axis];\n        const angleDiff = ((axisB.angle - axisA.angle + 540) % 360) - 180;\n        const midAngleRad = ((axisA.angle + angleDiff / 2) * Math.PI) / 180;\n        const ctrlX = xScale(CONTROL_R * Math.cos(midAngleRad));\n        const ctrlY = yScale(CONTROL_R * Math.sin(midAngleRad));\n        const p1x = xScale(source.x);\n        const p1y = yScale(source.y);\n        const p2x = xScale(target.x);\n        const p2y = yScale(target.y);\n        const wFrac =\n          MAX_WEIGHT === MIN_WEIGHT\n            ? 0.5\n            : (row[WEIGHT] - MIN_WEIGHT) / (MAX_WEIGHT - MIN_WEIGHT);\n\n        return (\n          <path\n            key={`edge-${i}`}\n            d={`M ${p1x} ${p1y} Q ${ctrlX} ${ctrlY} ${p2x} ${p2y}`}\n            fill=\"none\"\n            stroke={t.inkSoft}\n            strokeOpacity={0.25 + wFrac * 0.22}\n            strokeWidth={1.1 + wFrac * 1.1}\n          />\n        );\n      })}\n      {AXES.map((axis) => {\n        const hubX = xScale(INNER_R * SPOKE_HUB_SHARE * axis.cos);\n        const hubY = yScale(INNER_R * SPOKE_HUB_SHARE * axis.sin);\n        const rimX = xScale(OUTER_R * SPOKE_RIM_SHARE * axis.cos);\n        const rimY = yScale(OUTER_R * SPOKE_RIM_SHARE * axis.sin);\n        const labelX = xScale(OUTER_R * LABEL_R_SHARE * axis.cos);\n        const labelY = yScale(OUTER_R * LABEL_R_SHARE * axis.sin);\n        const titleOffsetY = axis.sin < 0 ? 6 : -14;\n        const subOffsetY = axis.sin < 0 ? 28 : 8;\n        const nodesOnAxis = nodesByAxis[axis.id];\n\n        return (\n          <g key={axis.id}>\n            <line\n              x1={hubX}\n              y1={hubY}\n              x2={rimX}\n              y2={rimY}\n              stroke={t.inkSoft}\n              strokeWidth={2.5}\n            />\n            <text\n              x={labelX}\n              y={labelY + titleOffsetY}\n              fill={t.ink}\n              fontSize={18}\n              fontWeight={700}\n              textAnchor=\"middle\"\n              dominantBaseline=\"middle\"\n            >\n              {axis.label} ({nodesOnAxis.length})\n            </text>\n            <text\n              x={labelX}\n              y={labelY + subOffsetY}\n              fill={t.inkSoft}\n              fontSize={14}\n              fontFamily={MONO}\n              textAnchor=\"middle\"\n              dominantBaseline=\"middle\"\n            >\n              ranked by degree {nodesOnAxis[0].degree}→\n              {nodesOnAxis[nodesOnAxis.length - 1].degree}\n            </text>\n          </g>\n        );\n      })}\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        background: t.pageBg,\n        color: t.ink,\n        fontFamily: FONT,\n        display: \"flex\",\n        flexDirection: \"column\",\n        boxSizing: \"border-box\",\n      }}\n    >\n      <div\n        style={{\n          height: HEADER_HEIGHT,\n          padding: \"18px 48px 6px 48px\",\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: 8,\n          boxSizing: \"border-box\",\n        }}\n      >\n        <div\n          style={{\n            fontSize: 22,\n            fontWeight: 700,\n            color: t.ink,\n            lineHeight: 1.2,\n          }}\n        >\n          hive-basic · javascript · muix · anyplot.ai\n        </div>\n        <div\n          style={{\n            fontSize: 14,\n            color: t.inkSoft,\n            fontFamily: MONO,\n          }}\n        >\n          {allNodes.length} nodes across {AXES.length} axes ({LAYER}) ·{\" \"}\n          {data.length} edges · nodes spaced outward by degree rank (degree{\" \"}\n          {MIN_DEGREE} → {MAX_DEGREE})\n        </div>\n        <div\n          style={{\n            display: \"flex\",\n            alignItems: \"center\",\n            gap: 24,\n            fontSize: 14,\n            color: t.ink,\n          }}\n        >\n          {AXES.map((axis) => (\n            <div\n              key={axis.id}\n              style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}\n            >\n              <span\n                style={{\n                  width: 12,\n                  height: 12,\n                  borderRadius: \"50%\",\n                  background: axis.color,\n                  display: \"inline-block\",\n                }}\n              />\n              <span style={{ fontWeight: 600 }}>{axis.label}</span>\n            </div>\n          ))}\n        </div>\n      </div>\n\n      <ChartsContainer\n        width={width}\n        height={height - HEADER_HEIGHT}\n        margin={MARGIN}\n        skipAnimation\n        xAxis={[{ scaleType: \"linear\", min: X_MIN, max: X_MAX, position: \"none\" }]}\n        yAxis={[{ scaleType: \"linear\", min: Y_MIN, max: Y_MAX, position: \"none\" }]}\n        series={AXES.map((axis) => ({\n          type: \"scatter\",\n          id: axis.id,\n          label: axis.label,\n          color: axis.color,\n          markerSize: 8,\n          data: nodesByAxis[axis.id].map((n) => ({\n            x: n.x,\n            y: n.y,\n            id: n.id,\n          })),\n        }))}\n      >\n        <HiveGeometry />\n        <ScatterPlot />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}