{"spec_id":"histogram-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// histogram-basic: Basic Histogram\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 85/100 | Updated: 2026-11-08\nimport { BarChart } from \"@mui/x-charts/BarChart\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TARGET_BINS = 14; // 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];\n\n// Columns: set these to your data's column names\nconst AGE = \"Subscriber Age (years)\"; // role: values\n\n// Data: replace this block with your own records, one object per observation\n// Ages of 600 streaming-service subscribers: a log-normal draw gives a young-\n// skewed base with a long tail of older loyal customers.\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: 600 }, () => {\n    const z = Math.sqrt(-2 * Math.log(Math.max(rand(), 1e-9))) * Math.cos(2 * Math.PI * rand());\n    return { [AGE]: Math.round(Math.min(85, Math.max(16, Math.exp(Math.log(29) + 0.34 * z)))) };\n  });\n})();\n\n// Plot: reads only data and the constants above\nconst values = data.map((d) => d[AGE]).filter((v) => v != null);\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;\n// Half-open bins [edge, edge + width): the maximum falls in the last one\nconst edges = Array.from({ length: binCount + 1 }, (_, i) => Number((binStart + i * binWidth).toPrecision(12)));\nconst binLabels = edges.slice(0, -1).map((lo, i) => `${lo}–${edges[i + 1]}`);\nconst counts = new Array(binCount).fill(0);\nfor (const v of values) counts[Math.floor((v - binStart) / binWidth)] += 1;\n\nconst unit = AGE.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\nconst TITLE = \"histogram-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `n = ${values.length} · bin width ${binWidth}${unit ? ` ${unit}` : \"\"}`;\nconst COUNT_LABEL = \"Frequency (count per bin)\";\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      <BarChart\n        width={width}\n        height={height - HEADER_H}\n        skipAnimation\n        hideLegend\n        borderRadius={0}\n        grid={{ horizontal: true }}\n        margin={{ top: 12, right: 40, bottom: 16, left: 24 }}\n        series={[{ id: \"counts\", data: counts, label: COUNT_LABEL, color: t.palette[0] }]}\n        xAxis={[\n          {\n            scaleType: \"band\",\n            data: binLabels,\n            // Contiguous bins: no gap between bars, the bar stroke marks the edges\n            categoryGapRatio: 0,\n            height: 64,\n            label: AGE,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            min: 0,\n            width: 72,\n            label: COUNT_LABEL,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        sx={{\n          \"& .MuiBarChart-element\": { stroke: t.pageBg, strokeWidth: 1.5 },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n        }}\n      />\n    </Box>\n  );\n}\n"}