{"spec_id":"box-horizontal","library":"muix","language":"javascript","code":"// anyplot.ai\n// box-horizontal: Horizontal 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 X_PAD_SHARE = 0.08;\nconst BOX_HEIGHT_SHARE = 0.56;\nconst HEADER_H = 82;\n\n// Columns: set these to your data's column names\nconst CATEGORIES = \"Job title\"; // role: categories\nconst VALUES = \"Annual base salary ($k)\"; // role: values\n\n// Data: replace this block with your own records, one object per row\nconst data = (() => {\n  let state = 42;\n  const roles = [\n    { title: \"Senior Software Engineer\", mean: 152, std: 16, size: 13, extra: 206 },\n    { title: \"Data Scientist\", mean: 138, std: 15, size: 12 },\n    { title: \"Product Manager\", mean: 134, std: 19, size: 11 },\n    { title: \"DevOps Engineer\", mean: 121, std: 13, size: 12 },\n    { title: \"UX/UI Designer\", mean: 104, std: 14, size: 11 },\n    { title: \"Marketing Analyst\", mean: 79, std: 11, size: 11, extra: 43 },\n    { title: \"Customer Success Manager\", mean: 73, std: 9, size: 11 },\n  ];\n  const rows: { [key: string]: string | number }[] = [];\n\n  for (const role of roles) {\n    for (let i = 0; i < role.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 salary = Math.max(28, role.mean + z * role.std);\n      rows.push({\n        [CATEGORIES]: role.title,\n        [VALUES]: Number(salary.toFixed(1)),\n      });\n    }\n    if (role.extra !== undefined) {\n      rows.push({ [CATEGORIES]: role.title, [VALUES]: role.extra });\n    }\n  }\n  return rows;\n})();\n\n// Plot: reads only data and the constants above\nconst rawCategories = [...new Set(data.map((row) => String(row[CATEGORIES])))];\n\nconst boxStats = rawCategories\n  .map((title) => {\n    const salaries = data\n      .filter((row) => String(row[CATEGORIES]) === title)\n      .map((row) => Number(row[VALUES]))\n      .sort((a, b) => a - b);\n    const n = salaries.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 ? salaries[b] + (p - b) * (salaries[b + 1] - salaries[b]) : salaries[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 = salaries.filter((v) => v >= lowerFence && v <= upperFence);\n    const outliers = salaries.filter((v) => v < lowerFence || v > upperFence);\n\n    return {\n      title,\n      q1,\n      median,\n      q3,\n      whiskerLow: inliers[0],\n      whiskerHigh: inliers[inliers.length - 1],\n      outliers,\n    };\n  })\n  .sort((a, b) => b.median - a.median);\n\nconst categories = boxStats.map((s) => s.title);\nconst allValues = data.map((row) => Number(row[VALUES]));\nconst minVal = Math.min(...allValues);\nconst maxVal = Math.max(...allValues);\nconst valSpan = maxVal - minVal || 1;\nconst valueMin = minVal - valSpan * X_PAD_SHARE;\nconst valueMax = maxVal + valSpan * X_PAD_SHARE;\n\nconst TITLE = \"box-horizontal · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${data.length} observations · ${categories.length} groups (${CATEGORIES}), sorted by median · Whiskers at ${IQR_FENCE_MULTIPLIER}×IQR`;\n\nfunction BoxWhiskers() {\n  const xScale = useXScale(\"salary\");\n  const yScale = useYScale(\"role\");\n  const bandwidth = yScale.bandwidth();\n  const boxHeight = bandwidth * BOX_HEIGHT_SHARE;\n  const capHeight = boxHeight * 0.65;\n\n  return (\n    <g>\n      {boxStats.map((s, i) => {\n        const center = yScale(s.title) + bandwidth / 2;\n        const top = center - boxHeight / 2;\n        const bottom = center + boxHeight / 2;\n        const capTop = center - capHeight / 2;\n        const capBottom = center + capHeight / 2;\n        const color = t.palette[i];\n        const xWhiskerLow = xScale(s.whiskerLow);\n        const xWhiskerHigh = xScale(s.whiskerHigh);\n        const xQ1 = xScale(s.q1);\n        const xQ3 = xScale(s.q3);\n        const xMedian = xScale(s.median);\n\n        return (\n          <g key={s.title}>\n            <line\n              x1={xWhiskerLow}\n              x2={xQ1}\n              y1={center}\n              y2={center}\n              stroke={color}\n              strokeWidth={2}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={xQ3}\n              x2={xWhiskerHigh}\n              y1={center}\n              y2={center}\n              stroke={color}\n              strokeWidth={2}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={xWhiskerLow}\n              x2={xWhiskerLow}\n              y1={capTop}\n              y2={capBottom}\n              stroke={color}\n              strokeWidth={2}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={xWhiskerHigh}\n              x2={xWhiskerHigh}\n              y1={capTop}\n              y2={capBottom}\n              stroke={color}\n              strokeWidth={2}\n              strokeLinecap=\"round\"\n            />\n            <rect\n              x={xQ1}\n              y={top}\n              width={Math.max(1, xQ3 - xQ1)}\n              height={boxHeight}\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={xMedian}\n              x2={xMedian}\n              y1={top}\n              y2={bottom}\n              stroke={color}\n              strokeWidth={3.5}\n              strokeLinecap=\"round\"\n            />\n            {s.outliers.map((v, j) => (\n              <circle\n                key={j}\n                cx={xScale(v)}\n                cy={center}\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: 48, bottom: 20, left: 36 }}\n        xAxis={[\n          {\n            id: \"salary\",\n            scaleType: \"linear\",\n            min: valueMin,\n            max: valueMax,\n            tickNumber: 9,\n            height: 64,\n            label: VALUES,\n            labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"role\",\n            scaleType: \"band\",\n            data: categories,\n            width: 240,\n            label: CATEGORIES,\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 vertical />\n        <BoxWhiskers />\n        <ChartsXAxis axisId=\"salary\" />\n        <ChartsYAxis axisId=\"role\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}