{"spec_id":"scatter-text","library":"muix","language":"javascript","code":"// anyplot.ai\n// scatter-text: Scatter Plot with Text Labels Instead of Points\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 89/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 { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { ChartsLegend } from \"@mui/x-charts/ChartsLegend\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst FONT_SIZE = 22; // px, one size for all labels, fitted to ~32 words; bold at this size is large text, so 3:1 contrast suffices in both themes\nconst X_PAD_SHARE = 0.07; // axis padding as a share of the data span, room for the text\nconst Y_PAD_SHARE = 0.08;\n\n// Columns: set these to your data's column names\nconst X = \"Embedding dimension 1\"; // role: x\nconst Y = \"Embedding dimension 2\"; // role: y\nconst LABEL = \"Word\"; // role: label\nconst CLUSTER = \"Topic cluster\"; // role: extra\n\n// Data: replace this block with your own records, one object per row\n// Words positioned like a t-SNE / UMAP projection of a word embedding: four topic clusters.\nconst topics: [string, number, number, string[]][] = [\n  [\"Animals\", 24, 76, [\"tiger\", \"falcon\", \"dolphin\", \"panther\", \"sparrow\", \"otter\", \"lynx\", \"heron\"]],\n  [\"Food & drink\", 76, 74, [\"saffron\", \"espresso\", \"truffle\", \"paprika\", \"chutney\", \"brioche\", \"sorbet\", \"tempeh\"]],\n  [\"Technology\", 26, 24, [\"kernel\", \"compiler\", \"firmware\", \"latency\", \"endpoint\", \"container\", \"pipeline\", \"runtime\"]],\n  [\"Sports\", 78, 22, [\"sprinter\", \"goalie\", \"referee\", \"marathon\", \"dugout\", \"scrimmage\", \"podium\", \"striker\"]],\n];\nconst offsets = [[-16, 9], [0, 13], [16, 9], [-19, -4], [0, 1], [19, -4], [-11, -15], [11, -15]];\nlet state = 20260902;\nconst data: Record<string, number | string>[] = [];\nfor (const [topic, cx, cy, words] of topics) {\n  words.forEach((word, i) => {\n    state = (state * 1103515245 + 12345) % 2147483648;\n    const jx = (state / 2147483648 - 0.5) * 4;\n    state = (state * 1103515245 + 12345) % 2147483648;\n    const jy = (state / 2147483648 - 0.5) * 4;\n    data.push({ [X]: cx + offsets[i][0] + jx, [Y]: cy + offsets[i][1] + jy, [LABEL]: word, [CLUSTER]: topic });\n  });\n}\n\n// Plot: reads only data and the constants above\nconst clusters = [...new Set(data.map((d) => String(d[CLUSTER])))];\n\nconst labels = clusters.flatMap((cluster, ci) =>\n  data\n    .filter((d) => d[CLUSTER] === cluster)\n    .map((d, i) => ({\n      id: `${ci}-${i}`,\n      cluster: ci,\n      x: Number(d[X]),\n      y: Number(d[Y]),\n      text: String(d[LABEL]),\n      color: t.palette[ci],\n    })),\n);\n\nconst xs = data.map((d) => Number(d[X]));\nconst ys = data.map((d) => Number(d[Y]));\nconst xMin = Math.min(...xs);\nconst xMax = Math.max(...xs);\nconst yMin = Math.min(...ys);\nconst yMax = Math.max(...ys);\nconst xPad = (xMax - xMin) * X_PAD_SHARE;\nconst yPad = (yMax - yMin) * Y_PAD_SHARE;\n\nconst title = \"scatter-text · javascript · muix · anyplot.ai\";\nconst subtitle = `${data.length} labels in ${clusters.length} clusters · each word is centered on its (x, y)`;\nconst HEADER_H = 84;\nconst LEGEND_H = 40;\n\nfunction TextLabels() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  return (\n    <g>\n      {labels.map((p) => (\n        <text\n          key={p.id}\n          x={xScale(p.x)}\n          y={yScale(p.y)}\n          fill={p.color}\n          fontSize={FONT_SIZE}\n          fontWeight={700}\n          textAnchor=\"middle\"\n          dominantBaseline=\"central\"\n          paintOrder=\"stroke\"\n          stroke={t.pageBg}\n          strokeWidth={FONT_SIZE / 4}\n          strokeLinejoin=\"round\"\n        >\n          {p.text}\n        </text>\n      ))}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div style={{ width, height, background: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <div\n        style={{\n          height: HEADER_H,\n          display: \"flex\",\n          flexDirection: \"column\",\n          justifyContent: \"center\",\n          alignItems: \"center\",\n          gap: 6,\n        }}\n      >\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>{title}</div>\n        <div style={{ fontSize: 15, color: t.inkSoft }}>{subtitle}</div>\n      </div>\n      <ChartsDataProvider\n        width={width}\n        height={height - HEADER_H - LEGEND_H}\n        margin={{ top: 12, right: 48, bottom: 16, left: 32 }}\n        skipAnimation\n        series={clusters.map((cluster, ci) => ({\n          type: \"scatter\" as const,\n          id: `cluster-${ci}`,\n          label: cluster,\n          color: t.palette[ci],\n          data: labels.filter((p) => p.cluster === ci).map((p) => ({ x: p.x, y: p.y, id: p.id })),\n        }))}\n        xAxis={[\n          {\n            scaleType: \"linear\",\n            min: xMin - xPad,\n            max: xMax + xPad,\n            label: X,\n            height: 64,\n            tickNumber: 8,\n            disableLine: true,\n            disableTicks: true,\n            valueFormatter: (v: number) => v.toFixed(0),\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            scaleType: \"linear\",\n            min: yMin - yPad,\n            max: yMax + yPad,\n            label: Y,\n            width: 72,\n            tickNumber: 6,\n            disableLine: true,\n            disableTicks: true,\n            valueFormatter: (v: number) => v.toFixed(0),\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n      >\n        <ChartsWrapper legendPosition={{ horizontal: \"end\", vertical: \"top\" }} legendDirection=\"horizontal\">\n          <ChartsLegend sx={{ fontSize: 16, color: t.ink, pr: 6 }} />\n          <ChartsSurface>\n            <ChartsGrid horizontal vertical />\n            <ChartsXAxis />\n            <ChartsYAxis />\n            <TextLabels />\n          </ChartsSurface>\n        </ChartsWrapper>\n      </ChartsDataProvider>\n    </div>\n  );\n}\n"}