{"spec_id":"lollipop-grouped","library":"muix","language":"javascript","code":"// anyplot.ai\n// lollipop-grouped: Grouped Lollipop Chart\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 87/100 | Updated: 2026-10-24\n\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst GROUP_PAD_FRAC = 0.18;\nconst Y_PAD_FRAC = 0.25;\n\n// Columns: set these to your data's column names\nconst CATEGORY = \"Region\"; // role: category\nconst SERIES = \"Product line\"; // role: series\nconst VALUE = \"Revenue ($M)\"; // role: value\n\n// Data: replace this block with your own records\nconst data: Array<Record<string, string | number>> = [\n  { [CATEGORY]: \"North America\", [SERIES]: \"Hardware\", [VALUE]: 48 },\n  { [CATEGORY]: \"North America\", [SERIES]: \"Software\", [VALUE]: 71 },\n  { [CATEGORY]: \"North America\", [SERIES]: \"Services\", [VALUE]: 39 },\n  { [CATEGORY]: \"Asia-Pacific\", [SERIES]: \"Hardware\", [VALUE]: 52 },\n  { [CATEGORY]: \"Asia-Pacific\", [SERIES]: \"Software\", [VALUE]: 45 },\n  { [CATEGORY]: \"Asia-Pacific\", [SERIES]: \"Services\", [VALUE]: 33 },\n  { [CATEGORY]: \"Europe\", [SERIES]: \"Hardware\", [VALUE]: 33 },\n  { [CATEGORY]: \"Europe\", [SERIES]: \"Software\", [VALUE]: 58 },\n  { [CATEGORY]: \"Europe\", [SERIES]: \"Services\", [VALUE]: 30 },\n  { [CATEGORY]: \"Latin America\", [SERIES]: \"Hardware\", [VALUE]: 21 },\n  { [CATEGORY]: \"Latin America\", [SERIES]: \"Software\", [VALUE]: 24 },\n  { [CATEGORY]: \"Latin America\", [SERIES]: \"Services\", [VALUE]: 17 },\n];\n\n// Plot: reads only data and the constants above\nconst rawRegions: string[] = [];\nconst productLines: string[] = [];\nfor (const row of data) {\n  const reg = String(row[CATEGORY]);\n  const prod = String(row[SERIES]);\n  if (!rawRegions.includes(reg)) rawRegions.push(reg);\n  if (!productLines.includes(prod)) productLines.push(prod);\n}\n\nconst regionTotals: Record<string, number> = {};\nfor (const reg of rawRegions) {\n  regionTotals[reg] = data\n    .filter((r) => String(r[CATEGORY]) === reg)\n    .reduce((sum, r) => sum + Number(r[VALUE]), 0);\n}\nconst regions = [...rawRegions].sort((a, b) => regionTotals[b] - regionTotals[a]);\n\nconst seriesColors = productLines.map((_, i) => t.palette[i]);\nconst maxRevenue = Math.max(...data.map((r) => Number(r[VALUE])));\nconst yAxisMax = maxRevenue * (1 + Y_PAD_FRAC);\nconst unitMatch = VALUE.match(/\\(([^)]+)\\)/);\nconst VALUE_UNIT = unitMatch ? unitMatch[1] : \"\";\nconst UNIT_PREFIX = VALUE_UNIT.startsWith(\"$\") ? \"$\" : \"\";\nconst UNIT_SUFFIX = VALUE_UNIT.startsWith(\"$\")\n  ? VALUE_UNIT.slice(1)\n  : VALUE_UNIT;\n\nfunction GroupedLollipopsLayer() {\n  const xScale = useXScale(\"region-axis\") as unknown as ((v: string) => number) & {\n    bandwidth: () => number;\n  };\n  const yScale = useYScale(\"revenue-axis\") as (v: number) => number;\n\n  const bandwidth = xScale.bandwidth();\n  const groupPadding = bandwidth * GROUP_PAD_FRAC;\n  const slotWidth = (bandwidth - 2 * groupPadding) / productLines.length;\n  const baselineY = yScale(0);\n\n  return (\n    <g>\n      {regions.map((region) =>\n        productLines.map((prod, si) => {\n          const row = data.find(\n            (r) => String(r[CATEGORY]) === region && String(r[SERIES]) === prod,\n          );\n          if (!row) return null;\n          const value = Number(row[VALUE]);\n          const cx = xScale(region) + groupPadding + slotWidth * (si + 0.5);\n          const cy = yScale(value);\n          return (\n            <g key={`${region}-${prod}`}>\n              <line\n                x1={cx}\n                y1={baselineY}\n                x2={cx}\n                y2={cy}\n                stroke={seriesColors[si]}\n                strokeWidth={4}\n                strokeLinecap=\"round\"\n              />\n              <circle\n                cx={cx}\n                cy={cy}\n                r={12}\n                fill={seriesColors[si]}\n                stroke={t.pageBg}\n                strokeWidth={2.5}\n              />\n              <text\n                x={cx}\n                y={cy - 19}\n                textAnchor=\"middle\"\n                fontSize={14}\n                fontWeight={500}\n                fill={t.ink}\n              >\n                {`${UNIT_PREFIX}${value}${UNIT_SUFFIX}`}\n              </text>\n            </g>\n          );\n        }),\n      )}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const titleHeight = 56;\n  const legendHeight = 36;\n  const chartHeight = height - titleHeight - legendHeight;\n\n  return (\n    <div\n      style={{\n        width,\n        height,\n        backgroundColor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n      }}\n    >\n      <div\n        style={{\n          height: titleHeight,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          fontSize: 22,\n          fontWeight: 500,\n          color: t.ink,\n        }}\n      >\n        lollipop-grouped · javascript · muix · anyplot.ai\n      </div>\n      <div\n        style={{\n          display: \"flex\",\n          justifyContent: \"center\",\n          gap: 28,\n          height: legendHeight,\n          alignItems: \"center\",\n        }}\n      >\n        {productLines.map((name, i) => (\n          <div key={name} style={{ display: \"flex\", alignItems: \"center\", gap: 8 }}>\n            <span\n              style={{\n                width: 14,\n                height: 14,\n                borderRadius: \"50%\",\n                background: seriesColors[i],\n                display: \"inline-block\",\n              }}\n            />\n            <span style={{ fontSize: 15, color: t.inkSoft }}>{name}</span>\n          </div>\n        ))}\n      </div>\n      <ChartsContainer\n        width={width}\n        height={chartHeight}\n        series={[]}\n        skipAnimation\n        margin={{ top: 20, bottom: 16, left: 20, right: 24 }}\n        xAxis={[\n          {\n            id: \"region-axis\",\n            scaleType: \"band\",\n            data: regions,\n            categoryGapRatio: 0.3,\n            height: 72,\n            label: CATEGORY,\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"revenue-axis\",\n            scaleType: \"linear\",\n            min: 0,\n            max: yAxisMax,\n            width: 84,\n            label: VALUE,\n            valueFormatter: (v: number | null) =>\n              v != null ? `${UNIT_PREFIX}${v}${UNIT_SUFFIX}` : \"\",\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.inkSoft },\n          },\n        ]}\n      >\n        <ChartsGrid horizontal />\n        <GroupedLollipopsLayer />\n        <ChartsXAxis axisId=\"region-axis\" />\n        <ChartsYAxis axisId=\"revenue-axis\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}