{"spec_id":"cat-box-strip","library":"muix","language":"javascript","code":"// anyplot.ai\n// cat-box-strip: Box Plot with Strip Overlay\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 89/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 Y_PAD_SHARE = 0.08;\nconst BOX_WIDTH_SHARE = 0.36;\nconst JITTER_WIDTH_SHARE = 0.28;\nconst IQR_FENCE_MULT = 1.5;\nconst HEADER_H = 112;\n\n// Columns: set these to your data's column names\nconst CATEGORY = \"Transport mode\"; // role: category\nconst VALUE = \"Commute time (minutes)\"; // role: value\n\n// Data: replace this block with your own records, one object per row\nconst data = (() => {\n  const nPerCategory = 45;\n  const params = [\n    { category: \"Bike\", mean: 17, sd: 4, min: 5, jamProb: 0, jamExtra: 0 },\n    { category: \"Walk\", mean: 21, sd: 5, min: 6, jamProb: 0, jamExtra: 0 },\n    { category: \"Train\", mean: 26, sd: 5, min: 10, jamProb: 0, jamExtra: 0 },\n    { category: \"Bus\", mean: 31, sd: 8, min: 9, jamProb: 0, jamExtra: 0 },\n    { category: \"Car\", mean: 24, sd: 9, min: 7, jamProb: 0.12, jamExtra: 25 },\n  ];\n\n  // One stream of uniforms (mulberry32, seed 42), consumed in order below\n  let seed = 42;\n  const uniforms: number[] = [];\n  for (let i = 0; i < 4 * nPerCategory * params.length; i += 1) {\n    seed = (seed + 0x6d2b79f5) | 0;\n    let x = Math.imul(seed ^ (seed >>> 15), 1 | seed);\n    x = (x + Math.imul(x ^ (x >>> 7), 61 | x)) ^ x;\n    uniforms.push(((x ^ (x >>> 14)) >>> 0) / 4294967296);\n  }\n\n  let k = 0;\n  const rows: { [key: string]: string | number }[] = [];\n  for (const p of params) {\n    for (let i = 0; i < nPerCategory; i += 1) {\n      const u1 = Math.max(uniforms[k++], 1e-9);\n      const u2 = uniforms[k++];\n      const z = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);\n      let v = p.mean + z * p.sd;\n      // A share of car trips hits a traffic jam\n      if (p.jamProb > 0 && uniforms[k++] < p.jamProb) {\n        v += p.jamExtra + uniforms[k++] * p.jamExtra;\n      }\n\n      rows.push({\n        [CATEGORY]: p.category,\n        [VALUE]: Math.max(p.min, Math.round(v * 10) / 10),\n      });\n    }\n  }\n  return rows;\n})();\n\n// Plot: reads only data and the constants above\nconst categories = Array.from(new Set(data.map((d) => String(d[CATEGORY]))));\n\nlet jitterSeed = 1337;\nconst stats = categories.map((category, idx) => {\n  const catRows = data.filter((d) => String(d[CATEGORY]) === category);\n  const values = catRows.map((d) => Number(d[VALUE]));\n  const sorted = [...values].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 lowFence = q1 - IQR_FENCE_MULT * iqr;\n  const highFence = q3 + IQR_FENCE_MULT * iqr;\n  const inFence = sorted.filter((v) => v >= lowFence && v <= highFence);\n  const whiskerLow = inFence[0];\n  const whiskerHigh = inFence[inFence.length - 1];\n\n  const jittered = values.map((value) => {\n    jitterSeed = (jitterSeed + 0x6d2b79f5) | 0;\n    let jx = Math.imul(jitterSeed ^ (jitterSeed >>> 15), 1 | jitterSeed);\n    jx = (jx + Math.imul(jx ^ (jx >>> 7), 61 | jx)) ^ jx;\n    const u = ((jx ^ (jx >>> 14)) >>> 0) / 4294967296;\n    return {\n      value,\n      offset: u - 0.5,\n    };\n  });\n\n  return {\n    category,\n    color: t.palette[idx],\n    jittered,\n    q1,\n    median,\n    q3,\n    whiskerLow,\n    whiskerHigh,\n  };\n});\n\nconst allValues = data.map((d) => Number(d[VALUE]));\nconst rawMin = Math.min(...allValues);\nconst rawMax = Math.max(...allValues);\nconst span = rawMax - rawMin || 1;\nconst paddedMin = rawMin - span * Y_PAD_SHARE;\n// A non-negative variable keeps its zero floor; negative data keeps its padding\nconst yMin = rawMin >= 0 ? Math.max(0, paddedMin) : paddedMin;\nconst yMax = rawMax + span * Y_PAD_SHARE;\n\nconst TITLE = \"cat-box-strip · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `Box: median, Q1–Q3, whiskers to ${IQR_FENCE_MULT}×IQR · Dots: ${data.length} jittered observations · ${categories.length} groups (${CATEGORY})`;\n\nfunction BoxStripLayer() {\n  const xScale = useXScale(\"cat\");\n  const yScale = useYScale(\"val\");\n  const bandwidth = xScale.bandwidth();\n  const boxWidth = bandwidth * BOX_WIDTH_SHARE;\n  const capHalf = boxWidth * 0.32;\n  const jitterSpread = bandwidth * JITTER_WIDTH_SHARE;\n\n  return (\n    <g>\n      {stats.map((s) => {\n        const yLow = yScale(s.whiskerLow);\n        const yQ1 = yScale(s.q1);\n        const yQ3 = yScale(s.q3);\n        const yHigh = yScale(s.whiskerHigh);\n        const yMed = yScale(s.median);\n        const cx = xScale(s.category) + bandwidth / 2;\n\n        return (\n          <g key={s.category}>\n            <line\n              x1={cx}\n              y1={yLow}\n              x2={cx}\n              y2={yQ1}\n              stroke={s.color}\n              strokeWidth={2.2}\n              strokeOpacity={0.9}\n            />\n            <line\n              x1={cx}\n              y1={yQ3}\n              x2={cx}\n              y2={yHigh}\n              stroke={s.color}\n              strokeWidth={2.2}\n              strokeOpacity={0.9}\n            />\n            <line\n              x1={cx - capHalf}\n              y1={yLow}\n              x2={cx + capHalf}\n              y2={yLow}\n              stroke={s.color}\n              strokeWidth={2.2}\n              strokeOpacity={0.9}\n            />\n            <line\n              x1={cx - capHalf}\n              y1={yHigh}\n              x2={cx + capHalf}\n              y2={yHigh}\n              stroke={s.color}\n              strokeWidth={2.2}\n              strokeOpacity={0.9}\n            />\n            <rect\n              x={cx - boxWidth / 2}\n              y={yQ3}\n              width={boxWidth}\n              height={Math.max(1, yQ1 - yQ3)}\n              fill={s.color}\n              fillOpacity={0.18}\n              stroke={s.color}\n              strokeWidth={2.5}\n              rx={2}\n            />\n            <line\n              x1={cx - boxWidth / 2}\n              y1={yMed}\n              x2={cx + boxWidth / 2}\n              y2={yMed}\n              stroke={t.ink}\n              strokeWidth={3.2}\n              strokeLinecap=\"round\"\n            />\n          </g>\n        );\n      })}\n      {stats.map((s) => {\n        const cx = xScale(s.category) + bandwidth / 2;\n        return s.jittered.map((p, i) => (\n          <circle\n            key={`${s.category}-${i}`}\n            cx={cx + p.offset * jitterSpread}\n            cy={yScale(p.value)}\n            r={5.2}\n            fill={s.color}\n            fillOpacity={0.5}\n            stroke={t.pageBg}\n            strokeWidth={0.9}\n          />\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: 8,\n          paddingTop: 6,\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={{ top: 20, right: 48, bottom: 20, left: 40 }}\n        xAxis={[\n          {\n            id: \"cat\",\n            scaleType: \"band\",\n            data: categories,\n            height: 64,\n            label: CATEGORY,\n            labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"val\",\n            scaleType: \"linear\",\n            min: yMin,\n            max: yMax,\n            tickNumber: 7,\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.65,\n            strokeDasharray: \"3 4\",\n          },\n        }}\n      >\n        <ChartsGrid horizontal />\n        <BoxStripLayer />\n        <ChartsXAxis axisId=\"cat\" />\n        <ChartsYAxis axisId=\"val\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}