{"spec_id":"pie-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// pie-basic: Basic Pie Chart\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 87/100 | Updated: 2026-10-23\n//# anyplot-orientation: square\nimport { getContrastRatio } from \"@mui/material/styles\";\nimport { PieChart, PieArcLabel } from \"@mui/x-charts/PieChart\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst EXPLODE_OFFSET = 30; // px the largest slice is pulled out along its bisector\nconst TITLE = \"pie-basic · javascript · muix · anyplot.ai\";\nconst MARGIN = { top: 100, right: 60, bottom: 40, left: 60 };\nconst LEGEND_BAND = 60;\n\n// Columns: set these to your data's column names\nconst SOURCE = \"Traffic source\"; // role: category\nconst SHARE = \"Share of visits (%)\"; // role: value\n\n// Data: replace this block with your own records, one object per row\n// Website traffic sources for a mid-size e-commerce site (share of visits, sums to 100)\nconst data = [\n  { [SOURCE]: \"Organic Search\", [SHARE]: 38 },\n  { [SOURCE]: \"Direct\", [SHARE]: 22 },\n  { [SOURCE]: \"Social Media\", [SHARE]: 16 },\n  { [SOURCE]: \"Referral\", [SHARE]: 12 },\n  { [SOURCE]: \"Paid Search\", [SHARE]: 8 },\n  { [SOURCE]: \"Email\", [SHARE]: 4 },\n];\n\n// Plot: reads only data and the constants above\nconst total = data.reduce((sum, d) => sum + d[SHARE], 0);\n// Categorical pool without slot 5, the matte red reserved for bad/loss/error roles\nconst categorical = t.palette.filter((_, i) => i !== 4);\nconst slices = data.map((d, i) => ({\n  id: String(i),\n  label: d[SOURCE],\n  value: d[SHARE],\n  color: categorical[i % categorical.length],\n}));\nconst explodedIndex = slices.reduce((best, s, i) => (s.value > slices[best].value ? i : best), 0);\nconst percent = (value) => `${Math.round((value / total) * 100)}%`;\n\n// Arc-label ink: whichever of dark ink / white has the higher WCAG contrast\n// against the slice's own fill, so every label stays legible on any Imprint color.\nconst DARK_INK = \"#1A1A17\";\nconst labelInk = (fill) =>\n  getContrastRatio(fill, DARK_INK) > getContrastRatio(fill, \"#FFFFFF\") ? DARK_INK : \"#FFFFFF\";\nconst ContrastArcLabel = (props) => <PieArcLabel {...props} style={{ fill: labelInk(props.color) }} />;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const chartHeight = height - LEGEND_BAND;\n  const drawingWidth = width - MARGIN.left - MARGIN.right;\n  const drawingHeight = chartHeight - MARGIN.top - MARGIN.bottom;\n  const outerRadius = Math.round((Math.min(drawingWidth, drawingHeight) / 2) * 0.86);\n\n  // Cumulative angle bounds (deg) of the exploded slice, mirroring MUI X's pie\n  // layout (0 = 12 o'clock, clockwise, data order).\n  const before = slices.slice(0, explodedIndex).reduce((sum, s) => sum + s.value, 0);\n  const startAngle = (before / total) * 360;\n  const endAngle = ((before + slices[explodedIndex].value) / total) * 360;\n  const mid = ((startAngle + endAngle) / 2) * (Math.PI / 180);\n\n  // The main series keeps every slice (and drives the legend in data order); its\n  // copy of the exploded slice is hidden, and an unlabelled one-item overlay\n  // series redraws that slice offset outward along its bisector.\n  return (\n    <div style={{ position: \"relative\", width, height }}>\n      <div\n        style={{\n          position: \"absolute\",\n          top: 28,\n          left: 0,\n          right: 0,\n          textAlign: \"center\",\n          fontSize: 26,\n          fontWeight: 600,\n          color: t.ink,\n          fontFamily: \"'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif\",\n        }}\n      >\n        {TITLE}\n      </div>\n      <PieChart\n        width={width}\n        height={chartHeight}\n        skipAnimation\n        margin={MARGIN}\n        slots={{ pieArcLabel: ContrastArcLabel }}\n        series={[\n          {\n            id: \"main\",\n            data: slices,\n            cornerRadius: 2,\n            outerRadius,\n            arcLabel: (item) => (item.id === slices[explodedIndex].id ? \"\" : percent(item.value)),\n            arcLabelMinAngle: 6,\n          },\n          {\n            id: \"exploded\",\n            data: [{ ...slices[explodedIndex], id: \"exploded\", label: undefined }],\n            cornerRadius: 2,\n            outerRadius,\n            startAngle,\n            endAngle,\n            cx: drawingWidth / 2 + Math.sin(mid) * EXPLODE_OFFSET,\n            cy: drawingHeight / 2 - Math.cos(mid) * EXPLODE_OFFSET,\n            arcLabel: (item) => percent(item.value),\n          },\n        ]}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"bottom\", horizontal: \"center\" },\n            sx: { fontSize: 18, color: t.ink, gap: 3 },\n          },\n        }}\n        sx={{\n          \"& .MuiPieChart-arc\": { stroke: t.pageBg, strokeWidth: 3 },\n          [`& [data-series=\"main\"] .MuiPieChart-arc[data-index=\"${explodedIndex}\"]`]: {\n            opacity: 0,\n            pointerEvents: \"none\",\n          },\n          \"& .MuiPieChart-arcLabel\": { fontSize: 22, fontWeight: 700 },\n        }}\n      />\n    </div>\n  );\n}\n"}