{"spec_id":"bubble-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// bubble-basic: Basic Bubble Chart\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 87/100 | Updated: 2026-10-24\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst BUBBLE_OPACITY = 0.58;\nconst MAX_RADIUS = 34;\nconst X_PAD_SHARE = 0.1;\nconst Y_PAD_SHARE = 0.12;\nconst HEADER_H = 148;\n\n// Columns: set these to your data's column names\nconst X = \"Revenue growth rate (%)\"; // role: x\nconst Y = \"Operating profit margin (%)\"; // role: y\nconst SIZE = \"Market share index\"; // role: size\nconst SEGMENT = \"Company segment\"; // role: extra\n\n// Data: replace this block with your own records, one object per row\nconst data = (() => {\n  let state = 42;\n  const clusters = [\n    { name: \"Growth leaders\", growth: 24, margin: 19, share: 64, count: 16, xSpread: 8.5, ySpread: 6.5 },\n    { name: \"Mid-market\", growth: 13, margin: 11, share: 36, count: 20, xSpread: 6.5, ySpread: 5.5 },\n    { name: \"Niche players\", growth: 4, margin: 3.5, share: 16, count: 16, xSpread: 6.5, ySpread: 5.5 },\n  ];\n  const rows: { [key: string]: string | number }[] = [];\n\n  for (const c of clusters) {\n    for (let i = 0; i < c.count; i += 1) {\n      const normals: number[] = [];\n      for (let g = 0; g < 3; g += 1) {\n        state = (state * 1664525 + 1013904223) % 4294967296;\n        const u1 = Math.max(state / 4294967296, 1e-9);\n        state = (state * 1664525 + 1013904223) % 4294967296;\n        const u2 = state / 4294967296;\n        normals.push(Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2));\n      }\n      const xVal = Math.round((c.growth + normals[0] * c.xSpread) * 10) / 10;\n      const yVal = Math.round((c.margin + normals[1] * c.ySpread) * 10) / 10;\n      const sVal = Math.min(100, Math.max(10, Math.round(c.share + normals[2] * 16)));\n      rows.push({\n        [SEGMENT]: c.name,\n        [X]: xVal,\n        [Y]: yVal,\n        [SIZE]: sVal,\n      });\n    }\n  }\n  return rows;\n})();\n\n// Plot: reads only data and the constants above\nconst xValues = data.map((d) => d[X]);\nconst yValues = data.map((d) => d[Y]);\nconst sizeValues = data.map((d) => d[SIZE]);\nconst segments = [...new Set(data.map((d) => d[SEGMENT]))];\n\nconst sizeMin = Math.min(...sizeValues);\nconst sizeMax = Math.max(...sizeValues) || 1;\n\nconst bubbles = data.map((d, i) => ({\n  id: i,\n  x: d[X],\n  y: d[Y],\n  size: d[SIZE],\n  r: MAX_RADIUS * Math.sqrt(Math.max(0, d[SIZE]) / sizeMax),\n  color: t.palette[segments.indexOf(d[SEGMENT])],\n}));\n\nconst xMinRaw = Math.min(...xValues);\nconst xMaxRaw = Math.max(...xValues);\nconst xSpan = xMaxRaw - xMinRaw || 1;\nconst xMin = xMinRaw - xSpan * X_PAD_SHARE;\nconst xMax = xMaxRaw + xSpan * X_PAD_SHARE;\n\nconst yMinRaw = Math.min(...yValues);\nconst yMaxRaw = Math.max(...yValues);\nconst ySpan = yMaxRaw - yMinRaw || 1;\nconst yMin = yMinRaw - ySpan * Y_PAD_SHARE;\nconst yMax = yMaxRaw + ySpan * Y_PAD_SHARE;\n\nconst legendSizes = [\n  Math.round(sizeMin),\n  Math.round((sizeMin + sizeMax) / 2),\n  Math.round(sizeMax),\n].map((val) => ({\n  val,\n  r: MAX_RADIUS * Math.sqrt(Math.max(0, val) / sizeMax),\n}));\n\nconst TITLE = \"bubble-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${data.length} observations · Bubble area ∝ ${SIZE}, ${sizeMin}–${sizeMax}`;\n\nfunction Bubbles() {\n  const xScale = useXScale(\"growth\");\n  const yScale = useYScale(\"margin\");\n\n  return (\n    <g>\n      {bubbles.map((d) => (\n        <circle\n          key={d.id}\n          cx={xScale(d.x)}\n          cy={yScale(d.y)}\n          r={d.r}\n          fill={d.color}\n          fillOpacity={BUBBLE_OPACITY}\n          stroke={t.pageBg}\n          strokeWidth={2}\n        />\n      ))}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const chartHeight = height - HEADER_H;\n\n  return (\n    <div\n      style={{\n        width,\n        height,\n        background: t.pageBg,\n        fontFamily: \"Inter, system-ui, sans-serif\",\n        display: \"flex\",\n        flexDirection: \"column\",\n      }}\n    >\n      <div\n        style={{\n          height: HEADER_H,\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 }}>\n          {TITLE}\n        </div>\n        <div style={{ fontSize: 15, color: t.inkSoft }}>{SUBTITLE}</div>\n        <div\n          style={{\n            display: \"flex\",\n            alignItems: \"center\",\n            gap: 48,\n            fontSize: 14,\n            color: t.inkSoft,\n          }}\n        >\n          <div style={{ display: \"flex\", alignItems: \"center\", gap: 18 }}>\n            <span style={{ fontWeight: 600, color: t.ink }}>{SEGMENT}:</span>\n            {segments.map((segment, i) => (\n              <div\n                key={segment}\n                style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}\n              >\n                <svg width={20} height={20}>\n                  <circle\n                    cx={10}\n                    cy={10}\n                    r={9}\n                    fill={t.palette[i]}\n                    fillOpacity={BUBBLE_OPACITY}\n                    stroke={t.pageBg}\n                    strokeWidth={1.5}\n                  />\n                </svg>\n                <span>{segment}</span>\n              </div>\n            ))}\n          </div>\n          <div style={{ display: \"flex\", alignItems: \"center\", gap: 24 }}>\n            <span style={{ fontWeight: 600, color: t.ink }}>{SIZE}:</span>\n            {legendSizes.map((item) => (\n              <div\n                key={item.val}\n                style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}\n              >\n                <svg\n                  width={item.r * 2 + 4}\n                  height={item.r * 2 + 4}\n                  style={{ overflow: \"visible\" }}\n                >\n                  <circle\n                    cx={item.r + 2}\n                    cy={item.r + 2}\n                    r={item.r}\n                    fill={t.inkSoft}\n                    fillOpacity={BUBBLE_OPACITY}\n                    stroke={t.pageBg}\n                    strokeWidth={1.5}\n                  />\n                </svg>\n                <span>{item.val}</span>\n              </div>\n            ))}\n          </div>\n        </div>\n      </div>\n\n      <ChartsContainer\n        width={width}\n        height={chartHeight}\n        series={[]}\n        skipAnimation\n        margin={{ top: 20, right: 48, bottom: 20, left: 40 }}\n        xAxis={[\n          {\n            id: \"growth\",\n            min: xMin,\n            max: xMax,\n            tickNumber: 9,\n            height: 64,\n            label: X,\n            labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"margin\",\n            min: yMin,\n            max: yMax,\n            tickNumber: 8,\n            width: 84,\n            label: Y,\n            labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        sx={{\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": {\n            stroke: t.grid,\n            opacity: 0.65,\n            strokeDasharray: \"3 4\",\n          },\n        }}\n      >\n        <ChartsGrid horizontal vertical />\n        <Bubbles />\n        <ChartsXAxis axisId=\"growth\" />\n        <ChartsYAxis axisId=\"margin\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}