{"spec_id":"box-notched","library":"muix","language":"javascript","code":"// anyplot.ai\n// box-notched: Notched Box Plot\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 87/100 | Updated: 2026-10-24\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst IQR_FENCE_MULTIPLIER = 1.5;\nconst NOTCH_FACTOR = 1.57;\nconst Y_PAD_SHARE = 0.08;\nconst BOX_WIDTH_SHARE = 0.55;\nconst NOTCH_INSET_SHARE = 0.28;\nconst HEADER_H = 82;\n\n// Columns: set these to your data's column names\nconst CATEGORY = \"Department\"; // role: category\nconst VALUE = \"Annual salary ($k)\"; // role: value\n\n// Data: replace this block with your own records, one object per row\nconst data = (() => {\n  let seed = 42;\n  const deptSpecs = [\n    { name: \"Engineering\", n: 55, mean: 92, sd: 14, extra: 136 },\n    { name: \"Finance\", n: 45, mean: 88, sd: 11 },\n    { name: \"Sales\", n: 60, mean: 74, sd: 16, extra: 31 },\n    { name: \"Marketing\", n: 42, mean: 68, sd: 9 },\n    { name: \"Customer Support\", n: 38, mean: 58, sd: 7, extra: 36 },\n  ];\n  const rows: { [key: string]: string | number }[] = [];\n\n  for (const spec of deptSpecs) {\n    for (let i = 0; i < spec.n; i += 1) {\n      seed = (seed * 1103515245 + 12345) & 0x7fffffff;\n      const u1 = Math.max(seed / 0x7fffffff, 1e-9);\n      seed = (seed * 1103515245 + 12345) & 0x7fffffff;\n      const u2 = seed / 0x7fffffff;\n      const z = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);\n      const val = Math.max(28, spec.mean + spec.sd * z);\n      rows.push({\n        [CATEGORY]: spec.name,\n        [VALUE]: Number(val.toFixed(2)),\n      });\n    }\n    if (spec.extra !== undefined) {\n      rows.push({ [CATEGORY]: spec.name, [VALUE]: spec.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 salaryGroups = categories.map((name) => {\n  const sorted = data\n    .filter((row) => String(row[CATEGORY]) === name)\n    .map((row) => Number(row[VALUE]))\n    .sort((a, b) => a - b);\n  const n = sorted.length;\n  const [q1, median, q3] = [0.25, 0.5, 0.75].map((q) => {\n    const p = (n - 1) * q;\n    const b = Math.floor(p);\n    return b + 1 < n ? sorted[b] + (p - b) * (sorted[b + 1] - sorted[b]) : sorted[b];\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 = sorted.filter((v) => v >= lowerFence && v <= upperFence);\n  const outliers = sorted.filter((v) => v < lowerFence || v > upperFence);\n  const notch = (NOTCH_FACTOR * iqr) / Math.sqrt(n);\n\n  return {\n    name,\n    stats: {\n      n,\n      q1,\n      median,\n      q3,\n      whiskerLow: inliers[0],\n      whiskerHigh: inliers[inliers.length - 1],\n      outliers,\n      notch,\n    },\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 TITLE = \"box-notched · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${data.length} observations · ${categories.length} groups (${CATEGORY}) · Notches at ±${NOTCH_FACTOR}×IQR/√n (95% CI around median)`;\n\nfunction NotchedBoxes() {\n  const xScale = useXScale(\"dept\");\n  const yScale = useYScale(\"salary\");\n  const bandwidth = xScale.bandwidth();\n  const boxWidth = bandwidth * BOX_WIDTH_SHARE;\n  const notchHalf = boxWidth * NOTCH_INSET_SHARE;\n  const capHalf = boxWidth * 0.28;\n\n  return (\n    <g>\n      {salaryGroups.map((g, i) => {\n        const color = t.palette[i];\n        const cx = xScale(g.name) + bandwidth / 2;\n        const xLeft = cx - boxWidth / 2;\n        const xRight = cx + boxWidth / 2;\n        const xNotchLeft = cx - notchHalf;\n        const xNotchRight = cx + notchHalf;\n        const yQ3 = yScale(g.stats.q3);\n        const yQ1 = yScale(g.stats.q1);\n        const yMed = yScale(g.stats.median);\n        const yMedTop = yScale(g.stats.median + g.stats.notch);\n        const yMedBot = yScale(g.stats.median - g.stats.notch);\n        const yWhiskerHigh = yScale(g.stats.whiskerHigh);\n        const yWhiskerLow = yScale(g.stats.whiskerLow);\n\n        const boxPath = [\n          `M ${xLeft} ${yQ3}`,\n          `L ${xRight} ${yQ3}`,\n          `L ${xRight} ${yMedTop}`,\n          `L ${xNotchRight} ${yMed}`,\n          `L ${xRight} ${yMedBot}`,\n          `L ${xRight} ${yQ1}`,\n          `L ${xLeft} ${yQ1}`,\n          `L ${xLeft} ${yMedBot}`,\n          `L ${xNotchLeft} ${yMed}`,\n          `L ${xLeft} ${yMedTop}`,\n          \"Z\",\n        ].join(\" \");\n\n        return (\n          <g key={g.name}>\n            <line\n              x1={cx}\n              y1={yWhiskerHigh}\n              x2={cx}\n              y2={yQ3}\n              stroke={color}\n              strokeWidth={2}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={cx}\n              y1={yQ1}\n              x2={cx}\n              y2={yWhiskerLow}\n              stroke={color}\n              strokeWidth={2}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={cx - capHalf}\n              y1={yWhiskerHigh}\n              x2={cx + capHalf}\n              y2={yWhiskerHigh}\n              stroke={color}\n              strokeWidth={2}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={cx - capHalf}\n              y1={yWhiskerLow}\n              x2={cx + capHalf}\n              y2={yWhiskerLow}\n              stroke={color}\n              strokeWidth={2}\n              strokeLinecap=\"round\"\n            />\n            <path\n              d={boxPath}\n              fill={color}\n              fillOpacity={0.32}\n              stroke={color}\n              strokeWidth={2.5}\n              strokeLinejoin=\"round\"\n            />\n            <line\n              x1={xNotchLeft}\n              y1={yMed}\n              x2={xNotchRight}\n              y2={yMed}\n              stroke={color}\n              strokeWidth={3.5}\n              strokeLinecap=\"round\"\n            />\n            {g.stats.outliers.map((v, j) => (\n              <circle\n                key={j}\n                cx={cx}\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 { width, height } = window.ANYPLOT_SIZE;\n  const chartHeight = height - HEADER_H;\n\n  return (\n    <div\n      style={{\n        width,\n        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\n      <ChartsContainer\n        width={width}\n        height={chartHeight}\n        series={[]}\n        skipAnimation\n        margin={{ left: 40, right: 40, top: 20, bottom: 20 }}\n        xAxis={[\n          {\n            id: \"dept\",\n            scaleType: \"band\",\n            data: categories,\n            categoryGapRatio: 0.38,\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: \"salary\",\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        <NotchedBoxes />\n        <ChartsXAxis axisId=\"dept\" />\n        <ChartsYAxis axisId=\"salary\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}