{"spec_id":"scatter-categorical","library":"muix","language":"javascript","code":"// anyplot.ai\n// scatter-categorical: Categorical Scatter Plot\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 86/100 | Updated: 2026-11-08\n\nimport { ScatterChart } from \"@mui/x-charts/ScatterChart\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst PAD_SHARE = 0.08; // axis padding on each side as a share of the data span\nconst POINT_ALPHA = 0.82; // translucent fill plus a page-coloured edge keeps overlapping groups apart\n\n// Columns: set these to your data's column names\nconst FLIPPER = \"Flipper length (mm)\"; // role: x\nconst MASS = \"Body mass (kg)\"; // role: y\nconst SPECIES = \"Species\"; // role: category\n\n// Data: replace this block with your own records, one object per row\n// Penguin body measurements: three overlapping but separable species clusters.\nconst data = (() => {\n  let seed = 42;\n  const rng = () => {\n    seed = (1664525 * seed + 1013904223) >>> 0;\n    return seed / 4294967296;\n  };\n  const randn = () => Math.sqrt(-2 * Math.log(Math.max(rng(), 1e-9))) * Math.cos(2 * Math.PI * rng());\n  const species = [\n    { name: \"Adelie\", flipperMean: 190, flipperSd: 6.5, massMean: 3700, massSd: 460, n: 40 },\n    { name: \"Chinstrap\", flipperMean: 196, flipperSd: 7.2, massMean: 3730, massSd: 380, n: 34 },\n    { name: \"Gentoo\", flipperMean: 217, flipperSd: 6.5, massMean: 5090, massSd: 500, n: 38 },\n  ];\n  return species.flatMap((sp) =>\n    Array.from({ length: sp.n }, () => ({\n      [FLIPPER]: Math.round(sp.flipperMean + randn() * sp.flipperSd),\n      [MASS]: Math.round(sp.massMean + randn() * sp.massSd) / 1000,\n      [SPECIES]: sp.name,\n    })),\n  );\n})();\n\n// Plot: reads only data and the constants above\nconst groups = [...new Set(data.map((d) => d[SPECIES]))];\nconst xs = data.map((d) => d[FLIPPER]);\nconst ys = data.map((d) => d[MASS]);\nconst xPad = (Math.max(...xs) - Math.min(...xs)) * PAD_SHARE;\nconst yPad = (Math.max(...ys) - Math.min(...ys)) * PAD_SHARE;\n\nconst TITLE = \"scatter-categorical · javascript · muix · anyplot.ai\";\nconst HEADER_HEIGHT = 64;\nconst LEGEND_WIDTH = 170;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  return (\n    <div style={{ width, height, background: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <div\n        style={{\n          height: HEADER_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      <ScatterChart\n        width={width - LEGEND_WIDTH}\n        height={height - HEADER_HEIGHT}\n        skipAnimation\n        grid={{ vertical: true, horizontal: true }}\n        margin={{ top: 16, right: 32, bottom: 16, left: 24 }}\n        series={groups.map((group, i) => ({\n          id: `species-${i}`,\n          label: group,\n          color: t.palette[i],\n          markerSize: 7,\n          highlightScope: { highlight: \"series\", fade: \"global\" },\n          data: data\n            .map((d, j) => ({ id: j, x: d[FLIPPER], y: d[MASS], group: d[SPECIES] }))\n            .filter((p) => p.group === group),\n        }))}\n        xAxis={[\n          {\n            min: Math.min(...xs) - xPad,\n            max: Math.max(...xs) + xPad,\n            label: FLIPPER,\n            tickNumber: 8,\n            height: 72,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            min: Math.min(...ys) - yPad,\n            max: Math.max(...ys) + yPad,\n            label: MASS,\n            tickNumber: 6,\n            width: 72,\n            valueFormatter: (v: number) => v.toFixed(1),\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        slotProps={{\n          legend: {\n            direction: \"vertical\",\n            position: { vertical: \"middle\", horizontal: \"end\" },\n            sx: { fontSize: 16, color: t.ink, gap: 2, pl: 3 },\n          },\n        }}\n        sx={{\n          \"& .MuiScatterChart-marker\": { stroke: t.pageBg, strokeWidth: 1.5, fillOpacity: POINT_ALPHA },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n        }}\n      />\n    </div>\n  );\n}\n"}