{"spec_id":"box-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// box-basic: Basic Box Plot\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-24\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst IQR_FENCE_MULTIPLIER = 1.5;\nconst Y_PAD_SHARE = 0.08;\nconst BOX_WIDTH_SHARE = 0.5;\nconst HEADER_H = 82;\n\n// Columns: set these to your data's column names\nconst CATEGORY = \"Course section\"; // role: category\nconst VALUE = \"Exam score (%)\"; // role: value\n\n// Data: replace this block with your own records, one object per row\nconst data = (() => {\n  let state = 42;\n  const sections = [\n    { name: \"Calculus I\", mean: 78, std: 7, size: 62 },\n    { name: \"Linear Algebra\", mean: 71, std: 13, size: 58, extra: 18 },\n    { name: \"Probability\", mean: 85, std: 5, size: 74 },\n    { name: \"Differential Equations\", mean: 68, std: 11, size: 55, extra: 24 },\n    { name: \"Numerical Analysis\", mean: 80, std: 9, size: 66 },\n  ];\n  const rows: { [key: string]: string | number }[] = [];\n\n  for (const sec of sections) {\n    for (let i = 0; i < sec.size; i += 1) {\n      state = (state * 1664525 + 1013904223) % 4294967296;\n      const u1 = Math.max(state / 4294967296, 1e-9);\n      state = (state * 1664525 + 1013904223) % 4294967296;\n      const u2 = state / 4294967296;\n      const z = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);\n      const score = Math.min(100, Math.max(0, sec.mean + z * sec.std));\n      rows.push({ [CATEGORY]: sec.name, [VALUE]: Number(score.toFixed(2)) });\n    }\n    if (sec.extra !== undefined) {\n      rows.push({ [CATEGORY]: sec.name, [VALUE]: sec.extra });\n    }\n  }\n  return rows;\n})();\n\n// Plot: reads only data and the constants above\nconst categories: string[] = [];\nfor (const row of data) {\n  const c = String(row[CATEGORY]);\n  if (!categories.includes(c)) categories.push(c);\n}\n\nconst boxStats = categories.map((name) => {\n  const scores = data\n    .filter((row) => String(row[CATEGORY]) === name)\n    .map((row) => Number(row[VALUE]))\n    .sort((a, b) => a - b);\n  const n = scores.length;\n\n  const [q1, median, q3] = [0.25, 0.5, 0.75].map((p) => {\n    const pos = (n - 1) * p;\n    const base = Math.floor(pos);\n    return base + 1 < n\n      ? scores[base] + (pos - base) * (scores[base + 1] - scores[base])\n      : scores[base];\n  });\n\n  const iqr = q3 - q1;\n  const lowerFence = q1 - IQR_FENCE_MULTIPLIER * iqr;\n  const upperFence = q3 + IQR_FENCE_MULTIPLIER * iqr;\n  const inliers = scores.filter((s) => s >= lowerFence && s <= upperFence);\n  const outliers = scores.filter((s) => s < lowerFence || s > upperFence);\n\n  return {\n    name,\n    q1,\n    median,\n    q3,\n    whiskerLow: inliers[0],\n    whiskerHigh: inliers[inliers.length - 1],\n    outliers,\n  };\n});\n\nconst allValues = data.map((row) => Number(row[VALUE]));\nconst minVal = Math.min(...allValues);\nconst maxVal = Math.max(...allValues);\nconst valSpan = maxVal - minVal || 1;\nconst yMin = minVal - valSpan * Y_PAD_SHARE;\nconst yMax = maxVal + valSpan * Y_PAD_SHARE;\n\nconst totalOutliers = boxStats.reduce((sum, s) => sum + s.outliers.length, 0);\nconst TITLE = \"box-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${data.length} observations · ${CATEGORY}: ${categories.length} · Whiskers at ${IQR_FENCE_MULTIPLIER}×IQR · ${totalOutliers} outliers`;\n\nfunction BoxWhiskers() {\n  const xScale = useXScale(\"classes\");\n  const yScale = useYScale(\"scores\");\n  const bandwidth = xScale.bandwidth();\n  const boxWidth = bandwidth * BOX_WIDTH_SHARE;\n  const capWidth = boxWidth * 0.65;\n\n  return (\n    <g>\n      {boxStats.map((s, i) => {\n        const center = xScale(s.name) + bandwidth / 2;\n        const left = center - boxWidth / 2;\n        const right = center + boxWidth / 2;\n        const capLeft = center - capWidth / 2;\n        const capRight = center + capWidth / 2;\n        const color = t.palette[i];\n        const yWhiskerHigh = yScale(s.whiskerHigh);\n        const yWhiskerLow = yScale(s.whiskerLow);\n        const yQ3 = yScale(s.q3);\n        const yQ1 = yScale(s.q1);\n        const yMedian = yScale(s.median);\n\n        return (\n          <g key={s.name}>\n            <line\n              x1={center}\n              x2={center}\n              y1={yWhiskerHigh}\n              y2={yQ3}\n              stroke={color}\n              strokeWidth={2}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={center}\n              x2={center}\n              y1={yQ1}\n              y2={yWhiskerLow}\n              stroke={color}\n              strokeWidth={2}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={capLeft}\n              x2={capRight}\n              y1={yWhiskerHigh}\n              y2={yWhiskerHigh}\n              stroke={color}\n              strokeWidth={2}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={capLeft}\n              x2={capRight}\n              y1={yWhiskerLow}\n              y2={yWhiskerLow}\n              stroke={color}\n              strokeWidth={2}\n              strokeLinecap=\"round\"\n            />\n            <rect\n              x={left}\n              y={yQ3}\n              width={boxWidth}\n              height={Math.max(1, yQ1 - yQ3)}\n              rx={4}\n              ry={4}\n              fill={color}\n              fillOpacity={0.3}\n              stroke={color}\n              strokeWidth={2.5}\n            />\n            <line\n              x1={left}\n              x2={right}\n              y1={yMedian}\n              y2={yMedian}\n              stroke={color}\n              strokeWidth={3.5}\n              strokeLinecap=\"round\"\n            />\n            {s.outliers.map((v, j) => (\n              <circle\n                key={j}\n                cx={center}\n                cy={yScale(v)}\n                r={5.5}\n                fill={t.pageBg}\n                stroke={color}\n                strokeWidth={2.2}\n              />\n            ))}\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const chartHeight = window.ANYPLOT_SIZE.height - HEADER_H;\n\n  return (\n    <div\n      style={{\n        width: window.ANYPLOT_SIZE.width,\n        height: window.ANYPLOT_SIZE.height,\n        background: t.pageBg,\n        fontFamily: \"Inter, system-ui, sans-serif\",\n        display: \"flex\",\n        flexDirection: \"column\",\n      }}\n    >\n      <div\n        style={{\n          height: HEADER_H,\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: 4,\n        }}\n      >\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>\n          {TITLE}\n        </div>\n        <div style={{ fontSize: 15, color: t.inkSoft }}>{SUBTITLE}</div>\n      </div>\n      <ChartsContainer\n        width={window.ANYPLOT_SIZE.width}\n        height={chartHeight}\n        series={[]}\n        skipAnimation\n        margin={{ top: 20, right: 40, bottom: 20, left: 40 }}\n        xAxis={[\n          {\n            id: \"classes\",\n            data: categories,\n            scaleType: \"band\",\n            height: 64,\n            label: CATEGORY,\n            labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"scores\",\n            min: yMin,\n            max: yMax,\n            tickNumber: 9,\n            width: 84,\n            label: VALUE,\n            labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        sx={{\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": {\n            stroke: t.grid,\n            opacity: 0.7,\n            strokeDasharray: \"3 4\",\n          },\n        }}\n      >\n        <ChartsGrid horizontal />\n        <BoxWhiskers />\n        <ChartsXAxis axisId=\"classes\" />\n        <ChartsYAxis axisId=\"scores\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}