{"spec_id":"range-interval","library":"muix","language":"javascript","code":"// anyplot.ai\n// range-interval: Range Interval Chart\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 86/100 | Updated: 2026-11-08\n\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 PAD_SHARE = 0.06; // value-axis padding beyond the lowest minimum and highest maximum, share of the span\nconst BAR_SHARE = 0.42; // range bar thickness as a share of its category band\nconst CAP_SHARE = 0.28; // endpoint marker radius as a share of the bar thickness\nconst TITLE_H = 48;\nconst SUBTITLE_H = 28;\n\n// Columns: set these to your data's column names\nconst DEPARTMENT = \"Department\"; // role: category\nconst MIN_SALARY = \"Minimum base salary ($k)\"; // role: min_value\nconst MAX_SALARY = \"Maximum base salary ($k)\"; // role: max_value\nconst SALARY = \"Base salary ($k)\"; // value-axis title shared by both bounds\n\n// Data: replace this block with your own records, one object per row\n// Annual base-salary bands by department at a mid-size tech company\nconst data = [\n  { [DEPARTMENT]: \"Customer Support\", [MIN_SALARY]: 42, [MAX_SALARY]: 58 },\n  { [DEPARTMENT]: \"Marketing\", [MIN_SALARY]: 48, [MAX_SALARY]: 72 },\n  { [DEPARTMENT]: \"Sales\", [MIN_SALARY]: 45, [MAX_SALARY]: 95 },\n  { [DEPARTMENT]: \"Data Analytics\", [MIN_SALARY]: 62, [MAX_SALARY]: 98 },\n  { [DEPARTMENT]: \"Product Management\", [MIN_SALARY]: 78, [MAX_SALARY]: 125 },\n  { [DEPARTMENT]: \"DevOps & Infrastructure\", [MIN_SALARY]: 88, [MAX_SALARY]: 145 },\n  { [DEPARTMENT]: \"Software Engineering\", [MIN_SALARY]: 85, [MAX_SALARY]: 165 },\n  { [DEPARTMENT]: \"Engineering Leadership\", [MIN_SALARY]: 120, [MAX_SALARY]: 210 },\n];\n\n// Plot: reads only data and the constants above\n// Ranges sorted by midpoint, lowest at the top; a row with a missing bound is left out\nconst ranges = data\n  .filter((d) => Number.isFinite(d[MIN_SALARY]) && Number.isFinite(d[MAX_SALARY]))\n  .map((d) => ({ category: d[DEPARTMENT], lo: Math.min(d[MIN_SALARY], d[MAX_SALARY]), hi: Math.max(d[MIN_SALARY], d[MAX_SALARY]) }))\n  .sort((a, b) => a.lo + a.hi - (b.lo + b.hi));\nconst lowest = Math.min(...ranges.map((r) => r.lo));\nconst highest = Math.max(...ranges.map((r) => r.hi));\nconst pad = (highest - lowest) * PAD_SHARE;\n\nconst TITLE = \"range-interval · javascript · muix · anyplot.ai\";\nconst unit = SALARY.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\nconst SUBTITLE = `${ranges.length} ranges sorted by midpoint · overall span ${lowest}–${highest} ${unit}`;\n\nfunction RangeBars() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  const thickness = yScale.bandwidth() * BAR_SHARE;\n\n  return (\n    <g>\n      {ranges.map((r) => {\n        const x0 = xScale(r.lo);\n        const x1 = xScale(r.hi);\n        const yMid = yScale(r.category) + yScale.bandwidth() / 2;\n        return (\n          <g key={r.category}>\n            <rect x={x0} y={yMid - thickness / 2} width={x1 - x0} height={thickness} rx={4} fill={t.palette[0]} fillOpacity={0.85} />\n            {[x0, x1].map((x, k) => (\n              <circle key={k} cx={x} cy={yMid} r={thickness * CAP_SHARE} fill={t.palette[0]} stroke={t.ink} strokeWidth={1.5} />\n            ))}\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <div style={{ height: TITLE_H, display: \"flex\", alignItems: \"flex-end\", justifyContent: \"center\", fontSize: 22, fontWeight: 600, color: t.ink }}>\n        {TITLE}\n      </div>\n      <div style={{ height: SUBTITLE_H, display: \"flex\", alignItems: \"center\", justifyContent: \"center\", fontSize: 15, color: t.inkSoft }}>\n        {SUBTITLE}\n      </div>\n      <ChartsContainer\n        width={width}\n        height={height - TITLE_H - SUBTITLE_H}\n        skipAnimation\n        series={[]}\n        margin={{ top: 20, right: 48, bottom: 16, left: 24 }}\n        xAxis={[\n          {\n            scaleType: \"linear\",\n            min: lowest - pad,\n            max: highest + pad,\n            label: SALARY,\n            height: 64,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            scaleType: \"band\",\n            data: ranges.map((r) => r.category),\n            label: DEPARTMENT,\n            width: 220,\n            disableTicks: true,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 15, fill: t.ink },\n          },\n        ]}\n        sx={{\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n        }}\n      >\n        <ChartsGrid vertical />\n        <RangeBars />\n        <ChartsXAxis />\n        <ChartsYAxis />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}