{"spec_id":"band-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// band-basic: Basic Band Plot\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 87/100 | Updated: 2026-11-08\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TITLE = \"band-basic · javascript · muix · anyplot.ai\";\nconst TITLE_H = 84;\nconst LEGEND_H = 44;\nconst BAND_OPACITY = 0.28;\nconst BAND_COLOR = t.palette[0];\nconst CENTER_COLOR = t.palette[1];\n\n// Columns: set these to your data's column names\nconst DAY = \"Forecast day\"; // role: x\nconst LOWER = \"Lower bound (°C)\"; // role: y_lower\nconst UPPER = \"Upper bound (°C)\"; // role: y_upper\nconst CENTER = \"Forecast temperature (°C)\"; // role: y_center\nconst BAND_NAME = \"90% confidence band\"; // what the lower and upper bounds describe\n\n// Data: replace this block with your own records, one object per row\n// 30-day temperature forecast: the further out the prediction, the wider the\n// interval around it. Seeded LCG, since the browser has no seeded RNG.\nconst data = (() => {\n  let state = 42;\n  const random = () => {\n    state = (state * 1103515245 + 12345) % 2147483648;\n    return state / 2147483648;\n  };\n  const days = Array.from({ length: 30 }, (_, i) => i + 1);\n  const temps = days.map((day) => {\n    const seasonalDrift = 16 + 5 * Math.sin((day / 30) * Math.PI * 0.9);\n    return Math.round((seasonalDrift + day * 0.1 + (random() - 0.5) * 0.9) * 10) / 10;\n  });\n  const lowers = temps.map((v, i) => Math.round((v - (0.6 + days[i] * 0.13 + random() * 0.3)) * 10) / 10);\n  const uppers = temps.map((v, i) => Math.round((v + (0.6 + days[i] * 0.13 + random() * 0.3)) * 10) / 10);\n  return days.map((day, i) => ({ [DAY]: day, [LOWER]: lowers[i], [UPPER]: uppers[i], [CENTER]: temps[i] }));\n})();\n\n// Plot: reads only data and the constants above\nconst rows = [...data].sort((a, b) => a[DAY] - b[DAY]);\nconst xs = rows.map((d) => d[DAY]);\n\n// The band is two stacked series: an invisible one at the lower bound and a filled\n// one adding the band's width, so the fill spans exactly lower to upper\nconst lowerEdge = rows.map((d) => (d[LOWER] == null || d[UPPER] == null ? null : d[LOWER]));\nconst bandWidth = rows.map((d) => (d[LOWER] == null || d[UPPER] == null ? null : d[UPPER] - d[LOWER]));\nconst widths = bandWidth.filter((v) => v != null);\n\nconst UNIT = CENTER.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\nconst SUBTITLE = `${rows.length} points, ${DAY.toLowerCase()} ${xs[0]}–${xs[xs.length - 1]} · shaded: ${BAND_NAME}, width ${Math.min(...widths).toFixed(1)}–${Math.max(...widths).toFixed(1)} ${UNIT}`.trim();\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <Box sx={{ width, height, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <Box sx={{ height: TITLE_H, px: \"40px\", pt: \"18px\", boxSizing: \"border-box\" }}>\n        <Typography sx={{ color: t.ink, fontSize: \"22px\", fontWeight: 600, lineHeight: 1.2 }}>\n          {TITLE}\n        </Typography>\n        <Typography sx={{ color: t.inkSoft, fontSize: \"15px\", lineHeight: 1.2, mt: \"8px\" }}>\n          {SUBTITLE}\n        </Typography>\n      </Box>\n      <LineChart\n        width={width}\n        height={height - TITLE_H - LEGEND_H}\n        skipAnimation\n        hideLegend\n        grid={{ horizontal: true }}\n        xAxis={[\n          {\n            data: xs,\n            scaleType: \"linear\",\n            min: xs[0],\n            max: xs[xs.length - 1],\n            label: DAY,\n            height: 64,\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            label: CENTER,\n            width: 76,\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        series={[\n          { id: \"lower\", data: lowerEdge, label: LOWER, stack: \"band\", showMark: false, color: BAND_COLOR, curve: \"monotoneX\" },\n          {\n            id: \"band\",\n            data: bandWidth,\n            label: BAND_NAME,\n            stack: \"band\",\n            area: true,\n            showMark: false,\n            color: BAND_COLOR,\n            curve: \"monotoneX\",\n            valueFormatter: (v: number | null, { dataIndex }: { dataIndex: number }) =>\n              v == null ? \"\" : `${rows[dataIndex][LOWER]} – ${rows[dataIndex][UPPER]}`,\n          },\n          { id: \"center\", data: rows.map((d) => d[CENTER]), label: CENTER, showMark: false, color: CENTER_COLOR, curve: \"monotoneX\" },\n        ]}\n        margin={{ top: 12, right: 40, bottom: 8, left: 24 }}\n        sx={{\n          '& .MuiLineChart-line[data-series=\"lower\"]': { display: \"none\" },\n          '& .MuiLineChart-line[data-series=\"band\"]': { display: \"none\" },\n          '& .MuiLineChart-area[data-series=\"band\"]': { fillOpacity: BAND_OPACITY, filter: \"none\" },\n          '& .MuiLineChart-line[data-series=\"center\"]': { strokeWidth: 3 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n        }}\n      />\n      <Box sx={{ height: LEGEND_H, display: \"flex\", alignItems: \"center\", justifyContent: \"center\", gap: \"36px\" }}>\n        <Box sx={{ display: \"flex\", alignItems: \"center\", gap: \"10px\" }}>\n          <Box sx={{ width: 28, height: 16, bgcolor: BAND_COLOR, opacity: BAND_OPACITY, borderRadius: \"3px\" }} />\n          <Typography sx={{ fontSize: 15, color: t.ink }}>{BAND_NAME}</Typography>\n        </Box>\n        <Box sx={{ display: \"flex\", alignItems: \"center\", gap: \"10px\" }}>\n          <Box sx={{ width: 28, height: 3, bgcolor: CENTER_COLOR }} />\n          <Typography sx={{ fontSize: 15, color: t.ink }}>{CENTER}</Typography>\n        </Box>\n      </Box>\n    </Box>\n  );\n}\n"}