{"spec_id":"network-bipartite","library":"muix","language":"javascript","code":"// anyplot.ai\n// network-bipartite: Bipartite Network 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 { useState } from \"react\";\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-bipartite · javascript · muix · anyplot.ai\";\nconst TITLE_HEIGHT = 60;\nconst LEGEND_HEIGHT = 86;\nconst MARGIN = { top: 44, right: 220, bottom: 24, left: 220 };\nconst NODE_MIN_R = 12;\nconst NODE_MAX_R = 26;\nconst ISOLATED_R = 9;\nconst EDGE_MIN_W = 1.6;\nconst EDGE_MAX_W = 6;\nconst EDGE_MIN_OP = 0.42;\nconst EDGE_OP_RANGE = 0.4;\n\n// Columns: set these to your data's column names\nconst STUDENT = \"Student (Set A)\"; // role: source\nconst COURSE = \"Course (Set B)\"; // role: target\nconst HOURS = \"Weekly contact (h)\"; // role: weight\n\n// Data: replace this block with your own records\nconst data: Array<Record<string, string | number | null>> = [\n  { [STUDENT]: \"Ava Chen\", [COURSE]: \"Linear Algebra\", [HOURS]: 4 },\n  { [STUDENT]: \"Ava Chen\", [COURSE]: \"Data Structures\", [HOURS]: 5 },\n  { [STUDENT]: \"Ava Chen\", [COURSE]: \"Machine Learning\", [HOURS]: 3 },\n  { [STUDENT]: \"Ava Chen\", [COURSE]: \"Statistics\", [HOURS]: 3 },\n  { [STUDENT]: \"Liam Brooks\", [COURSE]: \"Data Structures\", [HOURS]: 5 },\n  { [STUDENT]: \"Liam Brooks\", [COURSE]: \"Machine Learning\", [HOURS]: 4 },\n  { [STUDENT]: \"Liam Brooks\", [COURSE]: \"Digital Design\", [HOURS]: 3 },\n  { [STUDENT]: \"Noor Malik\", [COURSE]: \"Organic Chemistry\", [HOURS]: 6 },\n  { [STUDENT]: \"Noor Malik\", [COURSE]: \"Cell Biology\", [HOURS]: 4 },\n  { [STUDENT]: \"Noor Malik\", [COURSE]: \"Statistics\", [HOURS]: 3 },\n  { [STUDENT]: \"Ethan Diaz\", [COURSE]: \"Microeconomics\", [HOURS]: 4 },\n  { [STUDENT]: \"Ethan Diaz\", [COURSE]: \"World History\", [HOURS]: 2 },\n  { [STUDENT]: \"Ethan Diaz\", [COURSE]: \"Statistics\", [HOURS]: 4 },\n  { [STUDENT]: \"Priya Nair\", [COURSE]: \"Organic Chemistry\", [HOURS]: 4 },\n  { [STUDENT]: \"Priya Nair\", [COURSE]: \"Cell Biology\", [HOURS]: 5 },\n  { [STUDENT]: \"Priya Nair\", [COURSE]: \"Statistics\", [HOURS]: 3 },\n  { [STUDENT]: \"Marcus Lee\", [COURSE]: \"Linear Algebra\", [HOURS]: 3 },\n  { [STUDENT]: \"Marcus Lee\", [COURSE]: \"Thermodynamics\", [HOURS]: 5 },\n  { [STUDENT]: \"Marcus Lee\", [COURSE]: \"Statistics\", [HOURS]: 3 },\n  { [STUDENT]: \"Sofia Reyes\", [COURSE]: \"Microeconomics\", [HOURS]: 3 },\n  { [STUDENT]: \"Sofia Reyes\", [COURSE]: \"World History\", [HOURS]: 3 },\n  { [STUDENT]: \"Jamal Carter\", [COURSE]: \"Data Structures\", [HOURS]: 4 },\n  { [STUDENT]: \"Jamal Carter\", [COURSE]: \"Machine Learning\", [HOURS]: 5 },\n  { [STUDENT]: \"Jamal Carter\", [COURSE]: \"Digital Design\", [HOURS]: 4 },\n  { [STUDENT]: \"Elena Popov\", [COURSE]: \"Linear Algebra\", [HOURS]: 4 },\n  { [STUDENT]: \"Elena Popov\", [COURSE]: \"Thermodynamics\", [HOURS]: 4 },\n  { [STUDENT]: \"Diego Silva\", [COURSE]: \"Microeconomics\", [HOURS]: 5 },\n  { [STUDENT]: \"Diego Silva\", [COURSE]: \"World History\", [HOURS]: 3 },\n  { [STUDENT]: \"Diego Silva\", [COURSE]: \"Statistics\", [HOURS]: 2 },\n  { [STUDENT]: \"Grace Kim\", [COURSE]: \"Cell Biology\", [HOURS]: 4 },\n  { [STUDENT]: \"Grace Kim\", [COURSE]: \"Machine Learning\", [HOURS]: 3 },\n  { [STUDENT]: \"Grace Kim\", [COURSE]: \"Statistics\", [HOURS]: 4 },\n  { [STUDENT]: \"Omar Haddad\", [COURSE]: \"Data Structures\", [HOURS]: 3 },\n  { [STUDENT]: \"Omar Haddad\", [COURSE]: \"Digital Design\", [HOURS]: 5 },\n  { [STUDENT]: \"Isla Fraser\", [COURSE]: \"Organic Chemistry\", [HOURS]: 5 },\n  { [STUDENT]: \"Isla Fraser\", [COURSE]: \"Cell Biology\", [HOURS]: 3 },\n  { [STUDENT]: \"Victor Alves\", [COURSE]: \"Linear Algebra\", [HOURS]: 3 },\n  { [STUDENT]: \"Victor Alves\", [COURSE]: \"Thermodynamics\", [HOURS]: 3 },\n  { [STUDENT]: \"Victor Alves\", [COURSE]: \"Digital Design\", [HOURS]: 3 },\n  { [STUDENT]: \"Talia Novak\", [COURSE]: null, [HOURS]: 0 },\n  { [STUDENT]: null, [COURSE]: \"Advanced Robotics\", [HOURS]: 0 },\n];\n\n// Plot: reads only data and the constants above\nconst unitMatch = /\\(([^)]+)\\)/.exec(HOURS);\nconst unit = unitMatch ? unitMatch[1] : \"\";\nconst unitSuffix = unit ? `${unit}` : \"\";\n\nconst students: string[] = [];\nconst courses: string[] = [];\nconst edges: Array<{ student: number; course: number; hours: number }> = [];\n\nfor (let i = 0; i < data.length; i++) {\n  const sVal = data[i][STUDENT];\n  const cVal = data[i][COURSE];\n  if (sVal !== null && sVal !== undefined) {\n    const sName = String(sVal);\n    if (!students.includes(sName)) students.push(sName);\n  }\n  if (cVal !== null && cVal !== undefined) {\n    const cName = String(cVal);\n    if (!courses.includes(cName)) courses.push(cName);\n  }\n}\n\nfor (let i = 0; i < data.length; i++) {\n  const sVal = data[i][STUDENT];\n  const cVal = data[i][COURSE];\n  const hours = Number(data[i][HOURS]);\n  if (sVal !== null && sVal !== undefined && cVal !== null && cVal !== undefined && hours > 0) {\n    edges.push({\n      student: students.indexOf(String(sVal)),\n      course: courses.indexOf(String(cVal)),\n      hours,\n    });\n  }\n}\n\nconst minHours = Math.min(...edges.map((e) => e.hours));\nconst maxHours = Math.max(...edges.map((e) => e.hours));\nconst hoursSpan = maxHours - minHours;\n\nconst studentDegree = students.map(() => 0);\nconst courseDegree = courses.map(() => 0);\nconst studentNeighbors: number[][] = students.map(() => []);\nconst courseNeighbors: number[][] = courses.map(() => []);\n\nfor (let i = 0; i < edges.length; i++) {\n  const e = edges[i];\n  studentDegree[e.student] += 1;\n  courseDegree[e.course] += 1;\n  studentNeighbors[e.student].push(e.course);\n  courseNeighbors[e.course].push(e.student);\n}\n\nconst initStudentOrder = students\n  .map((_, i) => i)\n  .sort((a, b) => studentDegree[b] - studentDegree[a] || a - b);\nconst initCourseOrder = courses\n  .map((_, i) => i)\n  .sort((a, b) => courseDegree[b] - courseDegree[a] || a - b);\n\nconst initStudentRanks = students.map(() => 0);\nfor (let r = 0; r < initStudentOrder.length; r++) initStudentRanks[initStudentOrder[r]] = r;\nconst initCourseRanks = courses.map(() => 0);\nfor (let r = 0; r < initCourseOrder.length; r++) initCourseRanks[initCourseOrder[r]] = r;\n\nconst courseOrder = courseDegree\n  .map((d, i) => {\n    const neigh = courseNeighbors[i];\n    const bary =\n      neigh.length === 0\n        ? Infinity\n        : neigh.reduce((sum, j) => sum + initStudentRanks[j], 0) / neigh.length;\n    return { i, d, bary };\n  })\n  .sort((a, b) => b.d - a.d || a.bary - b.bary || a.i - b.i)\n  .map((x) => x.i);\n\nconst studentOrder = studentDegree\n  .map((d, i) => {\n    const neigh = studentNeighbors[i];\n    const bary =\n      neigh.length === 0\n        ? Infinity\n        : neigh.reduce((sum, j) => sum + initCourseRanks[j], 0) / neigh.length;\n    return { i, d, bary };\n  })\n  .sort((a, b) => b.d - a.d || a.bary - b.bary || a.i - b.i)\n  .map((x) => x.i);\n\nconst studentRow = students.map(() => 0);\nfor (let r = 0; r < studentOrder.length; r++) studentRow[studentOrder[r]] = r;\nconst courseRow = courses.map(() => 0);\nfor (let r = 0; r < courseOrder.length; r++) courseRow[courseOrder[r]] = r;\n\nconst minDegree = 1;\nconst maxDegree = Math.max(...studentDegree, ...courseDegree);\nconst degreeSpan = maxDegree - minDegree;\n\nconst studentRadii = studentDegree.map((deg) =>\n  deg === 0\n    ? ISOLATED_R\n    : NODE_MIN_R + ((deg - minDegree) / degreeSpan) * (NODE_MAX_R - NODE_MIN_R),\n);\nconst courseRadii = courseDegree.map((deg) =>\n  deg === 0\n    ? ISOLATED_R\n    : NODE_MIN_R + ((deg - minDegree) / degreeSpan) * (NODE_MAX_R - NODE_MIN_R),\n);\n\ninterface HoverInfo {\n  label: string;\n  detail: string;\n  x: number;\n  y: number;\n}\n\nfunction BipartiteOverlay({\n  hover,\n  onHoverChange,\n}: {\n  hover: HoverInfo | null;\n  onHoverChange: (info: HoverInfo | null) => void;\n}) {\n  const { top } = useDrawingArea();\n  const xScale = useXScale();\n  const yScale = useYScale();\n  const canvasW = window.ANYPLOT_SIZE.width;\n\n  const studentX = Number(xScale(0));\n  const courseX = Number(xScale(1));\n\n  return (\n    <g>\n      <text\n        x={studentX}\n        y={top - 22}\n        textAnchor=\"middle\"\n        fontSize={16}\n        fontWeight={700}\n        fill={t.ink}\n      >\n        <tspan fill={t.palette[0]}>● </tspan>\n        {STUDENT}\n      </text>\n      <text\n        x={courseX}\n        y={top - 22}\n        textAnchor=\"middle\"\n        fontSize={16}\n        fontWeight={700}\n        fill={t.ink}\n      >\n        <tspan fill={t.palette[1]}>● </tspan>\n        {COURSE}\n      </text>\n\n      {edges.map((edge, i) => {\n        const y1 = Number(yScale(1 - (studentRow[edge.student] + 0.5) / students.length));\n        const y2 = Number(yScale(1 - (courseRow[edge.course] + 0.5) / courses.length));\n        const hRatio = (edge.hours - minHours) / hoursSpan;\n        const strokeW = EDGE_MIN_W + hRatio * (EDGE_MAX_W - EDGE_MIN_W);\n        const strokeOp = EDGE_MIN_OP + hRatio * EDGE_OP_RANGE;\n        const tooltip: HoverInfo = {\n          label: `${students[edge.student]} → ${courses[edge.course]}`,\n          detail: `${edge.hours} ${unitSuffix}`,\n          x: (studentX + courseX) / 2,\n          y: (y1 + y2) / 2,\n        };\n        return (\n          <g key={`edge-${i}`}>\n            <line\n              x1={studentX}\n              y1={y1}\n              x2={courseX}\n              y2={y2}\n              stroke={t.inkSoft}\n              strokeOpacity={strokeOp}\n              strokeWidth={strokeW}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={studentX}\n              y1={y1}\n              x2={courseX}\n              y2={y2}\n              stroke=\"transparent\"\n              strokeWidth={16}\n              style={{ cursor: \"pointer\" }}\n              onMouseEnter={() => onHoverChange(tooltip)}\n              onMouseLeave={() => onHoverChange(null)}\n            />\n          </g>\n        );\n      })}\n\n      {students.map((label, i) => {\n        const cy = Number(yScale(1 - (studentRow[i] + 0.5) / students.length));\n        const r = studentRadii[i];\n        const tooltip: HoverInfo = {\n          label,\n          detail: `degree = ${studentDegree[i]}`,\n          x: studentX,\n          y: cy,\n        };\n        return (\n          <g key={`student-${i}`}>\n            <circle\n              cx={studentX}\n              cy={cy}\n              r={r}\n              fill={studentDegree[i] === 0 ? t.pageBg : t.palette[0]}\n              stroke={studentDegree[i] === 0 ? t.palette[0] : t.pageBg}\n              strokeWidth={studentDegree[i] === 0 ? 3.5 : 2.5}\n              style={{ cursor: \"pointer\" }}\n              onMouseEnter={() => onHoverChange(tooltip)}\n              onMouseLeave={() => onHoverChange(null)}\n            />\n            <text\n              x={studentX - r - 12}\n              y={cy}\n              textAnchor=\"end\"\n              dominantBaseline=\"middle\"\n              fontSize={15}\n              fill={t.ink}\n              style={{ pointerEvents: \"none\" }}\n            >\n              {label}\n            </text>\n          </g>\n        );\n      })}\n\n      {courses.map((label, i) => {\n        const cy = Number(yScale(1 - (courseRow[i] + 0.5) / courses.length));\n        const r = courseRadii[i];\n        const tooltip: HoverInfo = {\n          label,\n          detail: `degree = ${courseDegree[i]}`,\n          x: courseX,\n          y: cy,\n        };\n        return (\n          <g key={`course-${i}`}>\n            <circle\n              cx={courseX}\n              cy={cy}\n              r={r}\n              fill={courseDegree[i] === 0 ? t.pageBg : t.palette[1]}\n              stroke={courseDegree[i] === 0 ? t.palette[1] : t.pageBg}\n              strokeWidth={courseDegree[i] === 0 ? 3.5 : 2.5}\n              style={{ cursor: \"pointer\" }}\n              onMouseEnter={() => onHoverChange(tooltip)}\n              onMouseLeave={() => onHoverChange(null)}\n            />\n            <text\n              x={courseX + r + 12}\n              y={cy}\n              textAnchor=\"start\"\n              dominantBaseline=\"middle\"\n              fontSize={15}\n              fill={t.ink}\n              style={{ pointerEvents: \"none\" }}\n            >\n              {label}\n            </text>\n          </g>\n        );\n      })}\n\n      {hover && (\n        <g style={{ pointerEvents: \"none\" }}>\n          {(() => {\n            const boxW = Math.max(hover.label.length, hover.detail.length) * 8 + 24;\n            const boxH = 50;\n            const tx = Math.min(Math.max(hover.x - boxW / 2, 8), canvasW - boxW - 8);\n            const ty = Math.max(hover.y - boxH - 16, 8);\n            return (\n              <>\n                <rect\n                  x={tx}\n                  y={ty}\n                  width={boxW}\n                  height={boxH}\n                  rx={6}\n                  fill={t.elevatedBg}\n                  stroke={t.inkSoft}\n                  strokeOpacity={0.45}\n                />\n                <text\n                  x={tx + boxW / 2}\n                  y={ty + 20}\n                  textAnchor=\"middle\"\n                  fontSize={14}\n                  fontWeight={600}\n                  fill={t.ink}\n                >\n                  {hover.label}\n                </text>\n                <text\n                  x={tx + boxW / 2}\n                  y={ty + 39}\n                  textAnchor=\"middle\"\n                  fontSize={14}\n                  fill={t.inkSoft}\n                >\n                  {hover.detail}\n                </text>\n              </>\n            );\n          })()}\n        </g>\n      )}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const [hover, setHover] = useState<HoverInfo | null>(null);\n  const { width, height } = window.ANYPLOT_SIZE;\n  const chartHeight = height - TITLE_HEIGHT - LEGEND_HEIGHT;\n  const hourSamples = [minHours, Math.round((minHours + maxHours) / 2), maxHours];\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={chartHeight}\n        margin={MARGIN}\n        skipAnimation\n        series={[]}\n        xAxis={[{ position: \"none\", scaleType: \"linear\", min: 0, max: 1 }]}\n        yAxis={[{ position: \"none\", scaleType: \"linear\", min: 0, max: 1 }]}\n      >\n        <BipartiteOverlay hover={hover} onHoverChange={setHover} />\n      </ChartsContainer>\n      <div\n        style={{\n          height: LEGEND_HEIGHT,\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: 10,\n          fontSize: 14,\n          color: t.inkSoft,\n        }}\n      >\n        <div style={{ display: \"flex\", alignItems: \"center\", gap: 36 }}>\n          <span style={{ display: \"inline-flex\", alignItems: \"center\", gap: 8, color: t.ink }}>\n            <span\n              style={{\n                width: 14,\n                height: 14,\n                borderRadius: \"50%\",\n                backgroundColor: t.palette[0],\n                display: \"inline-block\",\n              }}\n            />\n            {`${STUDENT} · node size = degree`}\n          </span>\n          <span style={{ display: \"inline-flex\", alignItems: \"center\", gap: 8, color: t.ink }}>\n            <span\n              style={{\n                width: 14,\n                height: 14,\n                borderRadius: \"50%\",\n                backgroundColor: t.palette[1],\n                display: \"inline-block\",\n              }}\n            />\n            {`${COURSE} · node size = degree`}\n          </span>\n        </div>\n        <div style={{ display: \"flex\", alignItems: \"center\", gap: 24 }}>\n          <span>{`Edge width = ${HOURS}:`}</span>\n          {hourSamples.map((h) => {\n            const hRatio = (h - minHours) / hoursSpan;\n            const w = EDGE_MIN_W + hRatio * (EDGE_MAX_W - EDGE_MIN_W);\n            const op = EDGE_MIN_OP + hRatio * EDGE_OP_RANGE;\n            return (\n              <span key={h} style={{ display: \"inline-flex\", alignItems: \"center\", gap: 6 }}>\n                <span\n                  style={{\n                    width: 34,\n                    height: Math.round(w),\n                    backgroundColor: t.inkSoft,\n                    opacity: op,\n                    borderRadius: 2,\n                    display: \"inline-block\",\n                  }}\n                />\n                <span>{`${h}${unitSuffix}`}</span>\n              </span>\n            );\n          })}\n          <span>· Hollow ring = isolated node (no edges)</span>\n        </div>\n      </div>\n    </div>\n  );\n}\n"}