{"spec_id":"pyramid-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// pyramid-basic: Basic Pyramid Chart\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 87/100 | Updated: 2026-11-08\nimport { getContrastRatio } from \"@mui/material/styles\";\nimport { BarChart } from \"@mui/x-charts/BarChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst X_PAD = 0.08; // share of the largest value kept free beyond the longest bar\nconst DARK_INK = \"#1A1A17\";\nconst LIGHT_INK = \"#FAF8F1\";\nconst TITLE_H = 48;\nconst SUBTITLE_H = 28;\nconst LEGEND_H = 40;\n\n// Columns: set these to your data's column names\nconst GENRE = \"Favourite genre\"; // role: category\nconst STREAMVISTA = \"StreamVista (subscribers)\"; // role: value_left\nconst WAVEFLIX = \"WaveFlix (subscribers)\"; // role: value_right\n\n// Data: replace this block with your own records, one object per row\n// Survey of 400 subscribers per streaming service: the genre each watches most\nconst data = [\n  { [GENRE]: \"Action & Adventure\", [STREAMVISTA]: 75, [WAVEFLIX]: 60 },\n  { [GENRE]: \"Drama\", [STREAMVISTA]: 90, [WAVEFLIX]: 94 },\n  { [GENRE]: \"Comedy\", [STREAMVISTA]: 65, [WAVEFLIX]: 71 },\n  { [GENRE]: \"Sci-Fi & Fantasy\", [STREAMVISTA]: 50, [WAVEFLIX]: 38 },\n  { [GENRE]: \"Reality TV\", [STREAMVISTA]: 33, [WAVEFLIX]: 46 },\n  { [GENRE]: \"Anime\", [STREAMVISTA]: 40, [WAVEFLIX]: 45 },\n  { [GENRE]: \"Documentary\", [STREAMVISTA]: 27, [WAVEFLIX]: 36 },\n  { [GENRE]: \"Kids & Family\", [STREAMVISTA]: 20, [WAVEFLIX]: 10 },\n];\n\n// Plot: reads only data and the constants above\n// Genres have no natural order: rank them by their combined count, the largest at the base\nconst rows = [...data].sort((a, b) => (a[STREAMVISTA] ?? 0) + (a[WAVEFLIX] ?? 0) - ((b[STREAMVISTA] ?? 0) + (b[WAVEFLIX] ?? 0)));\nconst sides = [STREAMVISTA, WAVEFLIX].map((column, i) => {\n  const match = column.match(/^(.*?)\\s*\\(([^)]+)\\)$/);\n  const hex = t.palette[i];\n  return {\n    column,\n    name: match ? match[1] : column,\n    unit: match ? match[2] : \"\",\n    color: hex,\n    // the ink with the higher WCAG contrast on this bar colour\n    ink: getContrastRatio(hex, DARK_INK) >= getContrastRatio(hex, LIGHT_INK) ? DARK_INK : LIGHT_INK,\n    total: rows.reduce((sum, d) => sum + (d[column] ?? 0), 0),\n  };\n});\nconst maxValue = Math.max(...rows.flatMap((d) => [d[STREAMVISTA], d[WAVEFLIX]]).filter((v) => Number.isFinite(v)));\nconst limit = maxValue * (1 + X_PAD);\n\nconst TITLE = \"pyramid-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${sides[0].total} ${sides[0].name} and ${sides[1].total} ${sides[1].name} ${sides[0].unit} across ${rows.length} genres`;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <div style={{ height: TITLE_H, display: \"flex\", alignItems: \"flex-end\", justifyContent: \"center\", fontSize: 22, fontWeight: 600, color: t.ink }}>\n        {TITLE}\n      </div>\n      <div style={{ height: SUBTITLE_H, display: \"flex\", alignItems: \"center\", justifyContent: \"center\", fontSize: 15, color: t.inkSoft }}>\n        {SUBTITLE}\n      </div>\n      <BarChart\n        width={width}\n        height={height - TITLE_H - SUBTITLE_H - LEGEND_H}\n        layout=\"horizontal\"\n        skipAnimation\n        margin={{ top: 12, right: 48, bottom: 8, left: 16 }}\n        grid={{ vertical: true }}\n        series={sides.map((side, i) => ({\n          barLabel: (item) => (item.value === null ? null : `${Math.abs(item.value)}`),\n          id: `side-${i}`,\n          stack: \"pyramid\",\n          data: rows.map((d) => (d[side.column] === null || d[side.column] === undefined ? null : i === 0 ? -d[side.column] : d[side.column])),\n          label: side.name,\n          color: side.color,\n          valueFormatter: (v) => (v === null ? \"\" : `${Math.abs(v)} ${side.unit}`),\n        }))}\n        xAxis={[\n          {\n            id: \"value\",\n            min: -limit,\n            max: limit,\n            tickNumber: 10,\n            valueFormatter: (v) => `${Math.abs(v)}`,\n            label: `${sides[0].unit[0].toUpperCase()}${sides[0].unit.slice(1)} (← ${sides[0].name} · ${sides[1].name} →)`,\n            height: 64,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            scaleType: \"band\",\n            data: rows.map((d) => d[GENRE]),\n            categoryGapRatio: 0.3,\n            label: GENRE,\n            width: 180,\n            disableTicks: true,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 15, fill: t.ink },\n          },\n        ]}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"top\", horizontal: \"center\" },\n            sx: { fontSize: 15, color: t.inkSoft },\n          },\n        }}\n        sx={{\n          ...Object.fromEntries(\n            sides.map((side, i) => [`& [data-series=\"side-${i}\"] .MuiBarChart-label`, { fill: side.ink, fontSize: 14, fontWeight: 600 }]),\n          ),\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n        }}\n      >\n        <ChartsReferenceLine x={0} axisId=\"value\" lineStyle={{ stroke: t.ink, strokeWidth: 2 }} />\n      </BarChart>\n    </div>\n  );\n}\n"}