{"spec_id":"scatter-embedding","library":"muix","language":"javascript","code":"// anyplot.ai\n// scatter-embedding: t-SNE and UMAP Embedding Visualization\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 88/100 | Updated: 2026-11-08\n\nimport { ChartsDataProvider } from \"@mui/x-charts/ChartsDataProvider\";\nimport { ChartsWrapper } from \"@mui/x-charts/ChartsWrapper\";\nimport { ChartsSurface } from \"@mui/x-charts/ChartsSurface\";\nimport { ChartsLegend } from \"@mui/x-charts/ChartsLegend\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\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 EMBEDDING_METHOD = \"UMAP (n_neighbors = 15)\"; // the projection behind the two embedding columns\nconst POINT_OPACITY = 0.62; // slight transparency, so dense cores read darker\nconst EXTENT_PAD_SHARE = 0.05; // room beyond the outermost points, as a share of each axis span\nconst LABEL_FONT_SIZE = 15;\nconst LABEL_CHAR_WIDTH = 0.56; // average glyph width of the centroid labels, as a share of the font size\nconst LABEL_BOX_OPACITY = 0.35; // light enough that the cluster core shows through; a text halo keeps the label legible\n\n// Columns: set these to your data's column names\nconst X = \"UMAP dimension 1\"; // role: x\nconst Y = \"UMAP dimension 2\"; // role: y\nconst LABEL = \"Cell type\"; // role: label\n\n// Data: replace this block with your own records, one object per row\n// Simulated UMAP projection of single-cell RNA-seq profiles: eight cell types\n// of differing size and compactness around their own centres.\nconst data = (() => {\n  let seed = 42;\n  const uniform = () => {\n    seed = (1664525 * seed + 1013904223) >>> 0;\n    return seed / 4294967296;\n  };\n  const gaussian = () => Math.sqrt(-2 * Math.log(Math.max(uniform(), 1e-9))) * Math.cos(2 * Math.PI * uniform());\n  const cellTypes = [\n    { name: \"T cells\", cx: -9, cy: 5, spread: 1.9, n: 90 },\n    { name: \"B cells\", cx: 8, cy: 6.5, spread: 1.3, n: 65 },\n    { name: \"Monocytes\", cx: -7, cy: -6.5, spread: 2.1, n: 95 },\n    { name: \"NK cells\", cx: 9.5, cy: -4, spread: 1.4, n: 60 },\n    { name: \"Dendritic cells\", cx: 0.5, cy: 10, spread: 1.6, n: 70 },\n    { name: \"Platelets\", cx: -1, cy: -10, spread: 1.0, n: 55 },\n    { name: \"Erythrocytes\", cx: -12, cy: -1, spread: 1.75, n: 80 },\n    { name: \"Neutrophils\", cx: 13, cy: 1.5, spread: 1.5, n: 65 },\n  ];\n  return cellTypes.flatMap((c) =>\n    Array.from({ length: c.n }, () => ({\n      [X]: Number((c.cx + gaussian() * c.spread).toFixed(3)),\n      [Y]: Number((c.cy + gaussian() * c.spread).toFixed(3)),\n      [LABEL]: c.name,\n    })),\n  );\n})();\n\n// Plot: reads only data and the constants above\nconst labels = [...new Set(data.map((d) => d[LABEL]))];\nconst groups = labels.map((label, i) => {\n  const rows = data.filter((d) => d[LABEL] === label);\n  return {\n    label,\n    color: t.palette[i],\n    points: rows.map((d, k) => ({ id: `${i}-${k}`, x: d[X], y: d[Y] })),\n    centroidX: rows.reduce((s, d) => s + d[X], 0) / rows.length,\n    centroidY: rows.reduce((s, d) => s + d[Y], 0) / rows.length,\n  };\n});\n\nconst xs = data.map((d) => d[X]);\nconst ys = data.map((d) => d[Y]);\nconst xPad = (Math.max(...xs) - Math.min(...xs)) * EXTENT_PAD_SHARE;\nconst yPad = (Math.max(...ys) - Math.min(...ys)) * EXTENT_PAD_SHARE;\n\nconst TITLE = \"scatter-embedding · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${EMBEDDING_METHOD} · n = ${data.length} · coloured by ${LABEL.toLowerCase()} (${labels.length} groups)`;\nconst HEADER_HEIGHT = 84;\nconst LEGEND_WIDTH = 220;\n\nfunction CentroidLabels() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  return (\n    <g>\n      {groups.map((g) => {\n        const cx = xScale(g.centroidX);\n        const cy = yScale(g.centroidY);\n        const halfWidth = (g.label.length * LABEL_FONT_SIZE * LABEL_CHAR_WIDTH) / 2 + 10;\n        return (\n          <g key={g.label}>\n            <rect\n              x={cx - halfWidth}\n              y={cy - 14}\n              width={halfWidth * 2}\n              height={28}\n              rx={6}\n              fill={t.elevatedBg}\n              fillOpacity={LABEL_BOX_OPACITY}\n              stroke={g.color}\n              strokeWidth={2}\n            />\n            <text\n              x={cx}\n              y={cy + 1}\n              textAnchor=\"middle\"\n              dominantBaseline=\"central\"\n              fontSize={LABEL_FONT_SIZE}\n              fontWeight={600}\n              fill={t.ink}\n              stroke={t.pageBg}\n              strokeWidth={4}\n              strokeLinejoin=\"round\"\n              paintOrder=\"stroke\"\n            >\n              {g.label}\n            </text>\n          </g>\n        );\n      })}\n    </g>\n  );\n}\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          flexDirection: \"column\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: 6,\n        }}\n      >\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>{TITLE}</div>\n        <div style={{ fontSize: 16, color: t.inkSoft }}>{SUBTITLE}</div>\n      </div>\n      <ChartsDataProvider\n        width={width - LEGEND_WIDTH}\n        height={height - HEADER_HEIGHT}\n        margin={{ top: 12, right: 24, bottom: 16, left: 24 }}\n        skipAnimation\n        series={groups.map((g, i) => ({\n          type: \"scatter\" as const,\n          id: `group-${i}`,\n          label: g.label,\n          data: g.points,\n          color: g.color,\n          markerSize: 5,\n        }))}\n        xAxis={[\n          {\n            id: \"x\",\n            scaleType: \"linear\",\n            min: Math.min(...xs) - xPad,\n            max: Math.max(...xs) + xPad,\n            label: X,\n            height: 40,\n            disableLine: true,\n            disableTicks: true,\n            tickLabelStyle: { display: \"none\" },\n            labelStyle: { fontSize: 16, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"y\",\n            scaleType: \"linear\",\n            min: Math.min(...ys) - yPad,\n            max: Math.max(...ys) + yPad,\n            label: Y,\n            width: 40,\n            disableLine: true,\n            disableTicks: true,\n            tickLabelStyle: { display: \"none\" },\n            labelStyle: { fontSize: 16, fill: t.inkSoft },\n          },\n        ]}\n      >\n        <ChartsWrapper legendPosition={{ vertical: \"middle\", horizontal: \"end\" }} legendDirection=\"vertical\">\n          <ChartsLegend direction=\"vertical\" sx={{ fontSize: 16, color: t.ink, width: LEGEND_WIDTH, gap: 1.5, pl: 3 }} />\n          <ChartsSurface sx={{ \"& .MuiScatterChart-marker\": { fillOpacity: POINT_OPACITY } }}>\n            <ChartsXAxis />\n            <ChartsYAxis />\n            <ScatterPlot />\n            <CentroidLabels />\n          </ChartsSurface>\n        </ChartsWrapper>\n      </ChartsDataProvider>\n    </div>\n  );\n}\n"}