{"spec_id":"ecdf-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// ecdf-basic: Basic ECDF Plot\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 86/100 | Updated: 2026-11-08\nimport { LineChart } from \"@mui/x-charts/LineChart\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst X_PAD = 0.03; // share of the value range added on both sides of the outermost steps\nconst TITLE = \"ecdf-basic · javascript · muix · anyplot.ai\";\nconst HEADER_H = 84;\n\n// Columns: set these to your data's column names\nconst SCORE = \"Exam score (points)\"; // role: values\n\n// Data: replace this block with your own records, one object per row\n// Final exam scores of 80 students taught with traditional instruction (normal, mean 72, sd 12)\nconst data = (() => {\n  let seed = 42;\n  const rand = () => {\n    seed = (Math.imul(1664525, seed) + 1013904223) | 0;\n    return (seed >>> 0) / 4294967296;\n  };\n  return Array.from({ length: 80 }, () => {\n    const u = Math.max(rand(), 1e-10);\n    const z = Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * rand());\n    return { [SCORE]: Math.round((72 + 12 * z) * 10) / 10 };\n  });\n})();\n\n// Plot: reads only data and the constants above\nconst values = data.map((d) => d[SCORE]).filter((v) => Number.isFinite(v)).sort((a, b) => a - b);\nconst n = values.length;\nconst lo = values[0];\nconst hi = values[n - 1];\nconst pad = (hi - lo) * X_PAD;\n\n// Step function: 0 left of the smallest value, then +1/n at every observation (ties stack)\nconst xs = [lo - pad, ...values, hi + pad];\nconst ecdf = [0, ...values.map((_, i) => (i + 1) / n), 1];\nconst subtitle = `n = ${n} · range ${lo.toFixed(1)} to ${hi.toFixed(1)} · each step adds 1/${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: HEADER_H, padding: \"22px 40px 0\", boxSizing: \"border-box\" }}>\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.2 }}>{TITLE}</div>\n        <div style={{ fontSize: 15, color: t.inkSoft, marginTop: 6 }}>{subtitle}</div>\n      </div>\n      <LineChart\n        width={width}\n        height={height - HEADER_H}\n        skipAnimation\n        hideLegend\n        grid={{ horizontal: true, vertical: true }}\n        margin={{ top: 20, right: 48, bottom: 16, left: 24 }}\n        xAxis={[\n          {\n            data: xs,\n            scaleType: \"linear\",\n            min: lo - pad,\n            max: hi + pad,\n            label: SCORE,\n            height: 68,\n            tickNumber: 10,\n            valueFormatter: (v) => v.toFixed(0),\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            min: 0,\n            max: 1,\n            label: \"Cumulative proportion\",\n            width: 84,\n            tickNumber: 10,\n            valueFormatter: (v) => v.toFixed(1),\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        series={[\n          {\n            id: \"ecdf\",\n            data: ecdf,\n            label: \"Cumulative proportion\",\n            color: t.palette[0],\n            curve: \"stepAfter\",\n            showMark: false,\n          },\n        ]}\n        sx={{\n          \"& .MuiLineChart-line\": { strokeWidth: 3 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n        }}\n      />\n    </div>\n  );\n}\n"}