{"spec_id":"histogram-cumulative","library":"muix","language":"javascript","code":"// anyplot.ai\n// histogram-cumulative: Cumulative Histogram\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 87/100 | Updated: 2026-11-08\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TARGET_BINS = 12; // about this many bins; the width is rounded to a 1-2-2.5-5 step\nconst NICE_STEPS = [1, 2, 2.5, 5, 10];\nconst PERCENT_MAX = 100; // a cumulative share ends at 100 %\n\n// Columns: set these to your data's column names\nconst WAIT = \"Wait Time Before Answer (min)\"; // role: value\n\n// Data: replace this block with your own records, one object per observation\n// Call-centre wait times before an agent answers, 1,200 calls: a log-normal\n// draw where most callers wait a couple of minutes and a tail queues at peak load.\nconst data = (() => {\n  let state = 42 >>> 0;\n  const rand = () => {\n    state = (state * 1664525 + 1013904223) >>> 0;\n    return state / 4294967296;\n  };\n  return Array.from({ length: 1200 }, () => {\n    const z = Math.sqrt(-2 * Math.log(Math.max(rand(), 1e-9))) * Math.cos(2 * Math.PI * rand());\n    return { [WAIT]: Number(Math.min(12, Math.exp(Math.log(2.2) + 0.45 * z)).toFixed(2)) };\n  });\n})();\n\n// Plot: reads only data and the constants above\nconst values = data.map((d) => d[WAIT]).filter((v) => v != null);\nconst n = values.length;\nconst minValue = Math.min(...values);\nconst maxValue = Math.max(...values);\nconst rawWidth = (maxValue - minValue) / TARGET_BINS;\nconst magnitude = 10 ** Math.floor(Math.log10(rawWidth));\nconst binWidth = NICE_STEPS.map((s) => s * magnitude).find((w) => w >= rawWidth);\nconst binStart = Math.floor(minValue / binWidth) * binWidth;\nconst binCount = Math.floor((maxValue - binStart) / binWidth) + 1;\nconst edges = Array.from({ length: binCount + 1 }, (_, i) => Number((binStart + i * binWidth).toPrecision(12)));\n// Half-open bins [edge, edge + width): the maximum falls in the last one\nconst counts = new Array(binCount).fill(0);\nfor (const v of values) counts[Math.floor((v - binStart) / binWidth)] += 1;\n// One level per bin at the running share up to its upper edge; the leading 0\n// anchors the first step at the lower edge of the first bin\nconst cumulativeShare = [0];\nlet running = 0;\nfor (const c of counts) {\n  running += c;\n  cumulativeShare.push((running / n) * PERCENT_MAX);\n}\n\nconst sorted = [...values].sort((a, b) => a - b);\nconst median = n % 2 ? sorted[(n - 1) / 2] : (sorted[n / 2 - 1] + sorted[n / 2]) / 2;\n\nconst unit = WAIT.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\nconst unitSuffix = unit ? ` ${unit}` : \"\";\nconst TITLE = \"histogram-cumulative · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `n = ${n.toLocaleString(\"en-US\")} · bin width ${binWidth}${unitSuffix} · dashed line: median`;\nconst SHARE_LABEL = \"Cumulative share (%)\";\nconst HEADER_H = 76;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <Box sx={{ width, height, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\", overflow: \"hidden\" }}>\n      <Box sx={{ height: HEADER_H, pt: \"14px\", textAlign: \"center\", boxSizing: \"border-box\" }}>\n        <Typography sx={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.3 }}>{TITLE}</Typography>\n        <Typography sx={{ fontSize: 15, color: t.inkSoft, lineHeight: 1.5 }}>{SUBTITLE}</Typography>\n      </Box>\n      <LineChart\n        width={width}\n        height={height - HEADER_H}\n        skipAnimation\n        hideLegend\n        grid={{ horizontal: true }}\n        margin={{ top: 16, right: 40, bottom: 16, left: 24 }}\n        series={[\n          {\n            id: \"cumulative\",\n            data: cumulativeShare,\n            label: SHARE_LABEL,\n            color: t.palette[0],\n            curve: \"stepBefore\",\n            area: true,\n            showMark: false,\n            valueFormatter: (v) => `${v.toFixed(1)} %`,\n          },\n        ]}\n        xAxis={[\n          {\n            data: edges,\n            scaleType: \"linear\",\n            min: edges[0],\n            max: edges[edges.length - 1],\n            tickInterval: edges,\n            valueFormatter: (v: number) => String(v),\n            height: 64,\n            label: WAIT,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            min: 0,\n            max: PERCENT_MAX,\n            width: 72,\n            label: SHARE_LABEL,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        sx={{\n          \"& .MuiLineChart-area\": { fillOpacity: 0.18 },\n          \"& .MuiLineChart-line\": { strokeWidth: 3 },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n        }}\n      >\n        <ChartsReferenceLine\n          x={median}\n          label={`Median ${median.toFixed(1)}${unitSuffix}`}\n          labelAlign=\"start\"\n          labelStyle={{ fontSize: 15, fill: t.inkSoft }}\n          lineStyle={{ stroke: t.inkSoft, strokeWidth: 1.5, strokeDasharray: \"6 4\" }}\n        />\n      </LineChart>\n    </Box>\n  );\n}\n"}