{"spec_id":"parliament-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// parliament-basic: Parliament Seat Chart\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-24\n\nimport { ScatterChart } from \"@mui/x-charts/ScatterChart\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TITLE = \"parliament-basic · javascript · muix · anyplot.ai\";\nconst TITLE_FONT_SIZE = 22;\nconst TITLE_H = 44;\nconst LEGEND_H = 40;\nconst LABEL_FONT_SIZE = 16;\nconst OUTER_R = 628;\nconst INNER_R = 252;\nconst TOP_PAD_SHARE = 0.14;\nconst BOTTOM_PAD_SHARE = 0.13;\nconst X_PAD_SHARE = 0.12;\n\n// Columns: set these to your data's column names\nconst PARTY = \"Party\"; // role: category\nconst SEATS = \"Seats\"; // role: value\n\n// Data: replace this block with your own records\nconst data: Array<Record<string, string | number>> = [\n  { [PARTY]: \"Harbor Guild\", [SEATS]: 24 },\n  { [PARTY]: \"Meadow Union\", [SEATS]: 38 },\n  { [PARTY]: \"Lantern League\", [SEATS]: 52 },\n  { [PARTY]: \"Bridge Assembly\", [SEATS]: 46 },\n  { [PARTY]: \"Granite Circle\", [SEATS]: 28 },\n  { [PARTY]: \"Orchard Compact\", [SEATS]: 12 },\n];\n\n// Plot: reads only data and the constants above\nconst parties = data.map((row, idx) => ({\n  name: String(row[PARTY]),\n  seats: Number(row[SEATS]),\n  color: t.palette[idx],\n}));\n\nconst totalSeats = parties.reduce((sum, p) => sum + p.seats, 0);\nconst majoritySeats = Math.floor(totalSeats / 2) + 1;\n\nconst rowCount = Math.min(9, Math.max(3, Math.round(Math.sqrt(totalSeats / 6))));\nconst rowRadii = Array.from({ length: rowCount }, (_, i) =>\n  rowCount === 1 ? OUTER_R : INNER_R + (i * (OUTER_R - INNER_R)) / (rowCount - 1),\n);\nconst radiusSum = rowRadii.reduce((sum, r) => sum + r, 0);\nconst rowCapacities = rowRadii.map((r) => Math.max(1, Math.round((totalSeats * r) / radiusSum)));\nconst capacityDrift = totalSeats - rowCapacities.reduce((sum, c) => sum + c, 0);\nrowCapacities[rowCount - 1] += capacityDrift;\n\ninterface SeatSlot {\n  rowIndex: number;\n  r: number;\n  theta: number;\n  x: number;\n  y: number;\n}\n\nconst slots: SeatSlot[] = [];\nfor (let rowIndex = 0; rowIndex < rowRadii.length; rowIndex += 1) {\n  const r = rowRadii[rowIndex];\n  const capacity = rowCapacities[rowIndex];\n  for (let j = 0; j < capacity; j += 1) {\n    const theta = Math.PI - ((j + 0.5) / capacity) * Math.PI;\n    slots.push({ rowIndex, r, theta, x: r * Math.cos(theta), y: r * Math.sin(theta) });\n  }\n}\nslots.sort((a, b) => b.theta - a.theta || a.r - b.r);\n\ninterface AssignedSeat extends SeatSlot {\n  partyIndex: number;\n}\n\nconst assignedSeats: AssignedSeat[] = [];\nlet cursor = 0;\nfor (let partyIndex = 0; partyIndex < parties.length; partyIndex += 1) {\n  const count = parties[partyIndex].seats;\n  for (let i = 0; i < count; i += 1) {\n    if (cursor < slots.length) {\n      assignedSeats.push({ ...slots[cursor], partyIndex });\n      cursor += 1;\n    }\n  }\n}\n\nconst series = parties.map((party, partyIndex) => ({\n  id: `party-${partyIndex}`,\n  label: `${party.name} (${party.seats})`,\n  color: party.color,\n  data: assignedSeats\n    .filter((seat) => seat.partyIndex === partyIndex)\n    .map((seat, i) => ({ id: `${partyIndex}-${i}`, x: seat.x, y: seat.y })),\n  valueFormatter: () => `${party.seats} ${SEATS.toLowerCase()}`,\n}));\n\nconst rowRadialGap = rowCount > 1 ? (OUTER_R - INNER_R) / (rowCount - 1) : OUTER_R - INNER_R;\nconst minAngularSpacing = Math.min(...rowRadii.map((r, i) => (r * Math.PI) / rowCapacities[i]));\nconst seatDataRadius = 0.42 * Math.min(rowRadialGap, minAngularSpacing);\n\n// Stepped majority boundary through each row's gap between the first majoritySeats and the rest\nconst rowGapThetas = rowRadii.map((_, rIdx) => {\n  const leftInRow = slots.filter((s, idx) => s.rowIndex === rIdx && idx < majoritySeats);\n  const rightInRow = slots.filter((s, idx) => s.rowIndex === rIdx && idx >= majoritySeats);\n  const lastLeft = leftInRow[leftInRow.length - 1];\n  const firstRight = rightInRow[0];\n  return (lastLeft.theta + firstRight.theta) / 2;\n});\n\nconst topPad = OUTER_R * TOP_PAD_SHARE;\nconst bottomPad = OUTER_R * BOTTOM_PAD_SHARE;\nconst xPad = OUTER_R * X_PAD_SHARE;\n\nconst boundaryPoints: Array<{ x: number; y: number }> = [];\nconst innerStartR = INNER_R - rowRadialGap * 0.55;\nboundaryPoints.push({\n  x: innerStartR * Math.cos(rowGapThetas[0]),\n  y: innerStartR * Math.sin(rowGapThetas[0]),\n});\nfor (let rIdx = 0; rIdx < rowCount; rIdx += 1) {\n  const theta = rowGapThetas[rIdx];\n  if (rIdx < rowCount - 1) {\n    const midR = (rowRadii[rIdx] + rowRadii[rIdx + 1]) / 2;\n    boundaryPoints.push({ x: midR * Math.cos(theta), y: midR * Math.sin(theta) });\n    const nextTheta = rowGapThetas[rIdx + 1];\n    boundaryPoints.push({ x: midR * Math.cos(nextTheta), y: midR * Math.sin(nextTheta) });\n  } else {\n    const outerEndR = OUTER_R + topPad * 0.48;\n    boundaryPoints.push({ x: outerEndR * Math.cos(theta), y: outerEndR * Math.sin(theta) });\n  }\n}\n\nconst outerTheta = rowGapThetas[rowCount - 1];\nconst domain = {\n  xMin: -(OUTER_R + xPad),\n  xMax: OUTER_R + xPad,\n  yMin: -bottomPad,\n  yMax: OUTER_R + topPad,\n};\nconst domainWidth = domain.xMax - domain.xMin;\nconst domainHeight = domain.yMax - domain.yMin;\n\nfunction MajorityOverlay() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n\n  const labelX = (OUTER_R + topPad * 0.8) * Math.cos(outerTheta);\n  const labelY = (OUTER_R + topPad * 0.8) * Math.sin(outerTheta);\n  const boundaryPath = boundaryPoints\n    .map((pt, i) => `${i === 0 ? \"M\" : \"L\"} ${xScale(pt.x)} ${yScale(pt.y)}`)\n    .join(\" \");\n\n  return (\n    <g>\n      <path\n        d={boundaryPath}\n        fill=\"none\"\n        stroke={t.ink}\n        strokeOpacity={0.55}\n        strokeWidth={1.6}\n        strokeDasharray=\"6,5\"\n      />\n      <text x={xScale(labelX)} y={yScale(labelY)} textAnchor=\"middle\" fontSize={LABEL_FONT_SIZE} fill={t.inkSoft}>\n        {`Majority · ${majoritySeats}`}\n      </text>\n      <text\n        x={xScale(0)}\n        y={yScale(-bottomPad * 0.45)}\n        textAnchor=\"middle\"\n        fontSize={LABEL_FONT_SIZE}\n        fontWeight={500}\n        fill={t.ink}\n      >\n        {`${totalSeats} (${SEATS})`}\n      </text>\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const availW = width;\n  const availH = height - TITLE_H - LEGEND_H;\n\n  const margin = 12;\n  const maxDrawW = availW - margin * 2;\n  const maxDrawH = availH - margin * 2;\n  const pxPerUnit = Math.min(maxDrawW / domainWidth, maxDrawH / domainHeight);\n  const drawW = Math.round(domainWidth * pxPerUnit);\n  const drawH = Math.round(domainHeight * pxPerUnit);\n  const hPad = Math.max(margin, Math.floor((availW - drawW) / 2));\n  const vPad = Math.max(margin, Math.floor((availH - drawH) / 2));\n  const seatPxRadius = seatDataRadius * pxPerUnit;\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: \"24px\",\n        }}\n      >\n        {TITLE}\n      </div>\n      <ScatterChart\n        width={availW}\n        height={availH}\n        series={series.map((s) => ({ ...s, markerSize: seatPxRadius }))}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"top\", horizontal: \"start\" },\n            sx: { fontSize: LABEL_FONT_SIZE, color: t.inkSoft, paddingLeft: \"24px\", minHeight: `${LEGEND_H}px` },\n          },\n        }}\n        sx={{ \"& .MuiScatterChart-marker\": { stroke: t.pageBg, strokeWidth: 1 } }}\n        margin={{\n          top: vPad,\n          bottom: availH - drawH - vPad,\n          left: hPad,\n          right: availW - drawW - hPad,\n        }}\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        <MajorityOverlay />\n      </ScatterChart>\n    </div>\n  );\n}\n"}