{"spec_id":"density-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// density-basic: Basic Density 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 SILVERMAN = 1.06; // rule-of-thumb factor: bandwidth = 1.06 · sd · n^(-1/5)\nconst KDE_CUT = 2; // the curve extends this many bandwidths past the smallest and largest observation\nconst GRID_POINTS = 240;\nconst TITLE = \"density-basic · javascript · muix · anyplot.ai\";\nconst TITLE_H = 76;\n\n// Columns: set these to your data's column names\nconst FINISH_TIME = \"Marathon finish time (minutes)\"; // role: values\n\n// Data: replace this block with your own records, one object per row\n// 400 finish times of a city marathon, log-normal around a 230-minute median (right-skewed by slower runners)\nconst data = (() => {\n  let state = 42;\n  const rand = () => {\n    state = (state * 1664525 + 1013904223) % 4294967296;\n    return state / 4294967296;\n  };\n  return Array.from({ length: 400 }, () => {\n    const u1 = Math.max(rand(), 1e-9);\n    const u2 = rand();\n    const z = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);\n    return { [FINISH_TIME]: Math.round(Math.exp(Math.log(230) + 0.18 * z) * 10) / 10 };\n  });\n})();\n\n// Plot: reads only data and the constants above\nconst values = data.map((d) => d[FINISH_TIME]).filter((v) => Number.isFinite(v));\nconst n = values.length;\nconst mean = values.reduce((sum, v) => sum + v, 0) / n;\nconst sd = Math.sqrt(values.reduce((sum, v) => sum + (v - mean) ** 2, 0) / (n - 1));\nconst bandwidth = SILVERMAN * sd * n ** (-1 / 5);\n\n// Gaussian kernel density on an even grid; the area under the curve is 1\nconst gridStart = Math.min(...values) - KDE_CUT * bandwidth;\nconst gridEnd = Math.max(...values) + KDE_CUT * bandwidth;\nconst grid = Array.from({ length: GRID_POINTS }, (_, i) => gridStart + ((gridEnd - gridStart) * i) / (GRID_POINTS - 1));\nconst density = grid.map(\n  (x) =>\n    values.reduce((sum, v) => sum + Math.exp(-0.5 * ((x - v) / bandwidth) ** 2), 0) /\n    (n * bandwidth * Math.sqrt(2 * Math.PI)),\n);\n\nconst unit = FINISH_TIME.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\nconst subtitle = `n = ${n} · Gaussian kernel, bandwidth ${bandwidth.toFixed(1)} ${unit} (Silverman's rule)`;\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, 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 - TITLE_H}\n        skipAnimation\n        hideLegend\n        grid={{ horizontal: true }}\n        margin={{ top: 20, right: 48, bottom: 16, left: 24 }}\n        xAxis={[\n          {\n            data: grid,\n            scaleType: \"linear\",\n            min: gridStart,\n            max: gridEnd,\n            label: FINISH_TIME,\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            label: \"Probability density\",\n            width: 92,\n            tickNumber: 6,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        series={[\n          {\n            id: \"density\",\n            data: density,\n            label: \"Probability density\",\n            color: t.palette[0],\n            area: true,\n            curve: \"monotoneX\",\n            showMark: false,\n          },\n        ]}\n        sx={{\n          \"& .MuiLineChart-area\": { fillOpacity: 0.3 },\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"}