{"spec_id":"donut-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// donut-basic: Basic Donut Chart\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 89/100 | Updated: 2026-11-08\n//# anyplot-orientation: square\nimport { PieChart } from \"@mui/x-charts/PieChart\";\nimport { useDrawingArea } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst DARK_INK = \"#1A1A17\"; // fixed label inks: segment colours do not change with the theme\nconst LIGHT_INK = \"#FAF8F1\";\nconst INK_SPLIT = 0.194; // relative luminance at which both inks reach the same WCAG contrast\nconst TITLE = \"donut-basic · javascript · muix · anyplot.ai\";\nconst TITLE_H = 72;\nconst LEGEND_H = 56;\n\n// Columns: set these to your data's column names\nconst ASSET_CLASS = \"Asset class\"; // role: category\nconst ALLOCATION = \"Allocation (million USD)\"; // role: value\n\n// Data: replace this block with your own records, one object per row\n// A 500 million USD portfolio by asset class\nconst data = [\n  { [ASSET_CLASS]: \"Equities\", [ALLOCATION]: 210 },\n  { [ASSET_CLASS]: \"Fixed Income\", [ALLOCATION]: 140 },\n  { [ASSET_CLASS]: \"Real Estate\", [ALLOCATION]: 70 },\n  { [ASSET_CLASS]: \"Commodities\", [ALLOCATION]: 45 },\n  { [ASSET_CLASS]: \"Cash & Equivalents\", [ALLOCATION]: 35 },\n];\n\n// Plot: reads only data and the constants above\nconst total = data.reduce((sum, d) => sum + d[ALLOCATION], 0);\nconst slices = data.map((d, i) => {\n  const color = t.palette[i];\n  // WCAG relative luminance of the segment colour decides which fixed ink reads better on it\n  const [r, g, b] = [1, 3, 5].map((k) => {\n    const c = parseInt(color.slice(k, k + 2), 16) / 255;\n    return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;\n  });\n  return {\n    id: String(i),\n    label: d[ASSET_CLASS],\n    value: d[ALLOCATION],\n    color,\n    ink: 0.2126 * r + 0.7152 * g + 0.0722 * b > INK_SPLIT ? DARK_INK : LIGHT_INK,\n  };\n});\nconst inkRules = Object.fromEntries(\n  slices.map((s, i) => [`& .MuiPieChart-arcLabel:nth-of-type(${i + 1})`, { fill: s.ink }]),\n);\n\nfunction CenterTotal() {\n  const { left, top, width, height } = useDrawingArea();\n  const cx = left + width / 2;\n  const cy = top + height / 2;\n  return (\n    <g textAnchor=\"middle\">\n      <text x={cx} y={cy - 58} fontSize={18} fontWeight={600} letterSpacing=\"0.12em\" fill={t.inkSoft}>\n        TOTAL\n      </text>\n      <text x={cx} y={cy + 18} fontSize={68} fontWeight={700} fill={t.ink}>\n        {total.toLocaleString(\"en-US\")}\n      </text>\n      <text x={cx} y={cy + 56} fontSize={18} fill={t.inkSoft}>\n        {ALLOCATION}\n      </text>\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <div style={{ height: TITLE_H, paddingTop: 26, boxSizing: \"border-box\", textAlign: \"center\" }}>\n        <span style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>{TITLE}</span>\n      </div>\n      <PieChart\n        width={width}\n        height={height - TITLE_H - LEGEND_H}\n        skipAnimation\n        margin={{ top: 16, right: 40, bottom: 16, left: 40 }}\n        series={[\n          {\n            id: \"allocation\",\n            data: slices,\n            innerRadius: \"52%\",\n            outerRadius: \"96%\",\n            paddingAngle: 1.5,\n            cornerRadius: 4,\n            arcLabel: (item) => `${Math.round((item.value / total) * 100)}%`,\n            arcLabelMinAngle: 12,\n          },\n        ]}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"bottom\", horizontal: \"center\" },\n            sx: { fontSize: 16, color: t.ink, gap: 3 },\n          },\n        }}\n        sx={{\n          \"& .MuiPieChart-arc\": { stroke: t.pageBg, strokeWidth: 2 },\n          \"& .MuiPieChart-arcLabel\": { fontSize: 22, fontWeight: 700 },\n          ...inkRules,\n        }}\n      >\n        <CenterTotal />\n      </PieChart>\n    </div>\n  );\n}\n"}