{"spec_id":"scatter-annotated","library":"muix","language":"javascript","code":"// anyplot.ai\n// scatter-annotated: Annotated Scatter Plot with Text Labels\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-23\n\nimport { ScatterChart } from \"@mui/x-charts/ScatterChart\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\nconst isDark = window.ANYPLOT_THEME === \"dark\";\nconst MUTED = isDark ? \"#A8A79F\" : \"#6B6A63\";\n\n// Columns: set these to your data's column names\nconst X = \"Annual Revenue ($B)\"; // role: x\nconst Y = \"Market Cap ($B)\"; // role: y\nconst LABEL = \"Company\"; // role: label\n\n// Data: replace this block with your own records, one object per row\n// Annual revenue ($B) vs. market cap ($B) for a cohort of tech companies;\n// notable outliers carry a company label while peer companies have an empty label.\nconst data = [\n  { [X]: 8, [Y]: 210, [LABEL]: \"NexaCloud\" },\n  { [X]: 45, [Y]: 60, [LABEL]: \"Solstice Robotics\" },\n  { [X]: 62, [Y]: 340, [LABEL]: \"Vertex Analytics\" },\n  { [X]: 120, [Y]: 95, [LABEL]: \"Ferrotech Dynamics\" },\n  { [X]: 15, [Y]: 180, [LABEL]: \"Lumen Bioworks\" },\n  { [X]: 88, [Y]: 410, [LABEL]: \"Aurora Semiconductors\" },\n  { [X]: 150, [Y]: 60, [LABEL]: \"Cascade Networks\" },\n  { [X]: 30, [Y]: 265, [LABEL]: \"Nimbus Software\" },\n  { [X]: 95, [Y]: 70, [LABEL]: \"Ridgeline Hardware\" },\n  { [X]: 55, [Y]: 40, [LABEL]: \"Halcyon Games\" },\n  { [X]: 22, [Y]: 155, [LABEL]: \"Zephyr Mobility\" },\n  { [X]: 110, [Y]: 130, [LABEL]: \"Granite Payments\" },\n  { [X]: 7, [Y]: 38, [LABEL]: \"\" },\n  { [X]: 12, [Y]: 72, [LABEL]: \"\" },\n  { [X]: 19, [Y]: 58, [LABEL]: \"\" },\n  { [X]: 27, [Y]: 104, [LABEL]: \"\" },\n  { [X]: 31, [Y]: 79, [LABEL]: \"\" },\n  { [X]: 38, [Y]: 126, [LABEL]: \"\" },\n  { [X]: 44, [Y]: 98, [LABEL]: \"\" },\n  { [X]: 51, [Y]: 141, [LABEL]: \"\" },\n  { [X]: 58, [Y]: 112, [LABEL]: \"\" },\n  { [X]: 64, [Y]: 168, [LABEL]: \"\" },\n  { [X]: 71, [Y]: 139, [LABEL]: \"\" },\n  { [X]: 77, [Y]: 192, [LABEL]: \"\" },\n  { [X]: 84, [Y]: 158, [LABEL]: \"\" },\n  { [X]: 92, [Y]: 214, [LABEL]: \"\" },\n  { [X]: 101, [Y]: 176, [LABEL]: \"\" },\n  { [X]: 107, [Y]: 238, [LABEL]: \"\" },\n  { [X]: 118, [Y]: 197, [LABEL]: \"\" },\n  { [X]: 126, [Y]: 252, [LABEL]: \"\" },\n  { [X]: 133, [Y]: 214, [LABEL]: \"\" },\n  { [X]: 141, [Y]: 271, [LABEL]: \"\" },\n];\n\n// Plot: reads only data and the constants above\nconst allX = data.map((d) => d[X]);\nconst allY = data.map((d) => d[Y]);\nconst X_MAX = Math.ceil((Math.max(...allX) * 1.1) / 5) * 5;\nconst Y_MAX = Math.ceil((Math.max(...allY) + 20) / 10) * 10;\n\nconst OFFSET_CYCLE = [\n  { dx: 40, dy: -16 },\n  { dx: -44, dy: -18 },\n  { dx: 42, dy: -14 },\n  { dx: 44, dy: -16 },\n  { dx: 40, dy: 20 },\n  { dx: 44, dy: 18 },\n  { dx: -46, dy: -16 },\n  { dx: 42, dy: -12 },\n  { dx: 40, dy: 20 },\n  { dx: 40, dy: -20 },\n  { dx: -42, dy: 20 },\n  { dx: -46, dy: -16 },\n];\n\nconst highlighted = data\n  .filter((d) => Boolean(d[LABEL]))\n  .map((d, i) => {\n    const candidate = OFFSET_CYCLE[i % OFFSET_CYCLE.length];\n    const nearLeft = d[X] < X_MAX * 0.12;\n    const nearRight = d[X] > X_MAX * 0.85;\n    const dx = nearLeft\n      ? Math.abs(candidate.dx)\n      : nearRight\n      ? -Math.abs(candidate.dx)\n      : candidate.dx;\n    const dy = candidate.dy;\n    return {\n      id: `hl-${i}`,\n      x: d[X],\n      y: d[Y],\n      label: d[LABEL],\n      dx,\n      dy,\n    };\n  });\n\nconst peers = data\n  .filter((d) => !d[LABEL])\n  .map((d, i) => ({\n    id: `peer-${i}`,\n    x: d[X],\n    y: d[Y],\n  }));\n\nfunction PointAnnotations() {\n  const xScale = useXScale() as any;\n  const yScale = useYScale() as any;\n  if (!xScale || !yScale) return null;\n\n  return (\n    <g>\n      {highlighted.map((p) => {\n        const px = xScale(p.x);\n        const py = yScale(p.y);\n        const lx = px + p.dx;\n        const ly = py + p.dy;\n        const tx = lx + (p.dx >= 0 ? 6 : -6);\n        return (\n          <g key={p.id}>\n            <line x1={px} y1={py} x2={lx} y2={ly} stroke={MUTED} strokeWidth={1.2} opacity={0.65} />\n            <text\n              x={tx}\n              y={ly}\n              fontSize={14}\n              fontWeight={500}\n              fill={t.ink}\n              textAnchor={p.dx >= 0 ? \"start\" : \"end\"}\n              dominantBaseline=\"middle\"\n            >\n              {p.label}\n            </text>\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const titleHeight = 56;\n\n  return (\n    <div\n      style={{\n        width,\n        height,\n        background: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        fontFamily: \"'Roboto', 'Helvetica', 'Arial', sans-serif\",\n      }}\n    >\n      <div\n        style={{\n          height: titleHeight,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          fontSize: 20,\n          fontWeight: 600,\n          color: t.ink,\n        }}\n      >\n        Tech Company Valuations · scatter-annotated · javascript · muix · anyplot.ai\n      </div>\n\n      <ScatterChart\n        width={width}\n        height={height - titleHeight}\n        skipAnimation\n        disableHitArea\n        grid={{ horizontal: true, vertical: true }}\n        xAxis={[\n          {\n            height: 64,\n            label: X,\n            min: 0,\n            max: X_MAX,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            width: 84,\n            label: Y,\n            min: 0,\n            max: Y_MAX,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        series={[\n          {\n            id: \"highlighted\",\n            label: \"Highlighted\",\n            data: highlighted,\n            markerSize: 9,\n            color: t.palette[0],\n          },\n          {\n            id: \"peers\",\n            label: \"Peer companies\",\n            data: peers,\n            markerSize: 5.5,\n            color: MUTED,\n          },\n        ]}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"bottom\", horizontal: \"center\" },\n          },\n        }}\n        sx={{\n          \"& .MuiScatterChart-marker\": { fillOpacity: 0.7 },\n          \"& .MuiChartsLegend-root\": { color: t.ink, pb: \"12px\" },\n          \"& .MuiChartsLegend-label\": { fontSize: \"15px !important\", color: t.ink },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n        }}\n        margin={{ top: 20, right: 60, bottom: 16, left: 40 }}\n      >\n        <PointAnnotations />\n      </ScatterChart>\n    </div>\n  );\n}\n"}