{"spec_id":"dot-matrix-proportional","library":"muix","language":"javascript","code":"// anyplot.ai\n// dot-matrix-proportional: Dot Matrix Chart for Proportional Counts\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 83/100 | Updated: 2026-10-24\n//# anyplot-orientation: square\n\nimport * as React from \"react\";\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ScatterPlot } from \"@mui/x-charts/ScatterChart\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\nconst MUTED = window.ANYPLOT_THEME === \"dark\" ? \"#A8A79F\" : \"#6B6A63\";\n\n// Constants\nconst FONT = \"system-ui, -apple-system, sans-serif\";\nconst TITLE = \"dot-matrix-proportional · javascript · muix · anyplot.ai\";\nconst SEMANTIC_COLORS: Record<string, string> = {\n  Agreed: t.palette[0],\n  Disagreed: t.palette[4],\n  \"No opinion\": MUTED,\n};\nconst REMAINDER_LABEL = \"Remainder\";\nconst MARKER_CELL_SHARE = 0.38;\nconst TITLE_HEIGHT = 76;\nconst LEGEND_HEIGHT = 84;\n\n// Columns: set these to your data's column names\nconst CATEGORY = \"Response category\"; // role: category\nconst COUNT = \"Respondent count\"; // role: count\nconst TOTAL = \"Total population\"; // role: total\n\n// Data: replace this block with your own records\nconst data = [\n  { [CATEGORY]: \"Agreed\", [COUNT]: 47, [TOTAL]: 100 },\n  { [CATEGORY]: \"Disagreed\", [COUNT]: 35, [TOTAL]: 100 },\n  { [CATEGORY]: \"No opinion\", [COUNT]: 18, [TOTAL]: 100 },\n];\n\n// Plot: reads only data and the constants above\nconst listedSum = data.reduce((acc, r) => acc + Number(r[COUNT]), 0);\nconst declaredTotal = Math.max(...data.map((r) => Number(r[TOTAL])), listedSum);\nconst remainderCount = Math.max(0, declaredTotal - listedSum);\n\nconst gridCols = Math.ceil(Math.sqrt(declaredTotal));\nconst gridRows = Math.ceil(declaredTotal / gridCols);\n\nconst usedPalette = new Set(Object.values(SEMANTIC_COLORS));\nconst availablePalette = t.palette.filter((c: string) => !usedPalette.has(c));\nlet fallbackIdx = 0;\n\nconst categories = data.map((r) => {\n  const label = String(r[CATEGORY]);\n  const count = Number(r[COUNT]);\n  let color = SEMANTIC_COLORS[label];\n  if (!color) {\n    color = availablePalette[fallbackIdx];\n    fallbackIdx += 1;\n  }\n  return { label, count, color };\n});\n\nif (remainderCount > 0) {\n  categories.push({\n    label: REMAINDER_LABEL,\n    count: remainderCount,\n    color: t.grid,\n  });\n}\n\nlet dotIndex = 0;\nconst seriesDataByCategory = categories.map((cat) => {\n  const pts: { x: number; y: number; id: number }[] = [];\n  for (let n = 0; n < cat.count; n += 1) {\n    const row = Math.floor(dotIndex / gridCols);\n    const col = dotIndex % gridCols;\n    pts.push({ x: col, y: gridRows - 1 - row, id: dotIndex });\n    dotIndex += 1;\n  }\n  return {\n    ...cat,\n    points: pts,\n  };\n});\n\nexport default function Chart() {\n  const width = window.ANYPLOT_SIZE.width;\n  const height = window.ANYPLOT_SIZE.height;\n\n  const availW = width - 48;\n  const availH = height - TITLE_HEIGHT - LEGEND_HEIGHT;\n  const cellSize = Math.min(availW / gridCols, availH / gridRows);\n  const gridWidth = cellSize * gridCols;\n  const gridHeight = cellSize * gridRows;\n  const markerSize = cellSize * MARKER_CELL_SHARE;\n\n  return (\n    <Box\n      sx={{\n        width,\n        height,\n        backgroundColor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        alignItems: \"center\",\n      }}\n    >\n      <Box\n        sx={{\n          height: TITLE_HEIGHT,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          flexShrink: 0,\n        }}\n      >\n        <Typography sx={{ color: t.ink, fontSize: 22, fontWeight: 600, fontFamily: FONT }}>\n          {TITLE}\n        </Typography>\n      </Box>\n\n      <Box sx={{ position: \"relative\", width: gridWidth, height: gridHeight, flexShrink: 0 }}>\n        <ChartsContainer\n          width={gridWidth}\n          height={gridHeight}\n          margin={{ top: 0, right: 0, bottom: 0, left: 0 }}\n          skipAnimation\n          series={seriesDataByCategory.map((cat) => ({\n            type: \"scatter\" as const,\n            id: cat.label,\n            label: cat.label,\n            color: cat.color,\n            markerSize,\n            data: cat.points,\n          }))}\n          xAxis={[\n            {\n              scaleType: \"linear\",\n              min: -0.5,\n              max: gridCols - 0.5,\n              position: \"none\",\n              domainLimit: \"strict\",\n            },\n          ]}\n          yAxis={[\n            {\n              scaleType: \"linear\",\n              min: -0.5,\n              max: gridRows - 0.5,\n              position: \"none\",\n              domainLimit: \"strict\",\n            },\n          ]}\n        >\n          <ScatterPlot />\n        </ChartsContainer>\n      </Box>\n\n      <Box\n        sx={{\n          height: LEGEND_HEIGHT,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: \"40px\",\n          flexShrink: 0,\n        }}\n      >\n        {seriesDataByCategory.map((category) => (\n          <Box key={category.label} sx={{ display: \"flex\", alignItems: \"center\", gap: \"10px\" }}>\n            <Box\n              sx={{\n                width: 20,\n                height: 20,\n                borderRadius: \"50%\",\n                backgroundColor: category.color,\n                flexShrink: 0,\n              }}\n            />\n            <Typography sx={{ color: t.ink, fontSize: 16, fontFamily: FONT }}>\n              {category.label} — {category.count} ({Math.round((category.count / declaredTotal) * 100)}%)\n            </Typography>\n          </Box>\n        ))}\n      </Box>\n    </Box>\n  );\n}\n"}