{"spec_id":"heatmap-stripes-climate","library":"muix","language":"javascript","code":"// anyplot.ai\n// heatmap-stripes-climate: Climate Warming Stripes\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 85/100 | Updated: 2026-11-08\nimport { BarChart } from \"@mui/x-charts/BarChart\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\n// imprint_div's midpoint is the page colour, so a stripe near zero would vanish\n// into the page: every stripe starts at this share of its pole's tint and the\n// anomaly's size adds the rest. A zero anomaly is neither warm nor cold and takes\n// half this share of the neutral ink, which sits farther from the page than either\n// pole, so it stays as pale as the palest warm and cold stripes.\nconst MIN_INTENSITY = 0.2;\nconst BAND_WIDTH_SHARE = 0.94; // stripe band width as a share of the canvas width\nconst BAND_ASPECT = 3.1; // stripe band width : height\n\n// Columns: set these to your data's column names\nconst YEAR = \"Year\"; // role: year\nconst ANOMALY = \"Temperature anomaly (°C)\"; // role: anomaly\n\n// Data: replace this block with your own records, one object per year\n// Synthetic global-mean temperature anomaly against a 1961-1990-style baseline,\n// 1900-2024: a slow start, accelerating warming after about 1975, and\n// year-to-year noise from a fixed-seed LCG.\nconst data = (() => {\n  let state = 42;\n  const random = () => {\n    state = (state * 1103515245 + 12345) & 0x7fffffff;\n    return state / 0x7fffffff;\n  };\n  const rows = [];\n  for (let year = 1900; year <= 2024; year += 1) {\n    const progress = (year - 1900) / (2024 - 1900);\n    const trend = -0.25 + 1.35 * progress ** 2.2;\n    const noise = (random() - 0.5) * 0.28;\n    rows.push({ [YEAR]: year, [ANOMALY]: Math.round((trend + noise) * 100) / 100 });\n  }\n  return rows;\n})();\n\n// Plot: reads only data and the constants above\nconst rows = [...data].sort((a, b) => a[YEAR] - b[YEAR]);\n// Symmetric limits: the larger of the coldest and the warmest anomaly sets both poles\nconst maxAbsAnomaly = Math.max(...rows.map((d) => Math.abs(d[ANOMALY] ?? 0)));\nconst [coldRgb, midRgb, warmRgb, neutralRgb] = [t.div[2], t.div[1], t.div[0], t.inkSoft].map((hex) =>\n  [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16)),\n);\nconst stripeColors = rows.map((d) => {\n  const fraction = d[ANOMALY] / maxAbsAnomaly;\n  const share = fraction === 0 ? MIN_INTENSITY / 2 : MIN_INTENSITY + (1 - MIN_INTENSITY) * Math.abs(fraction);\n  const pole = fraction < 0 ? coldRgb : fraction > 0 ? warmRgb : neutralRgb;\n  const [r, g, b] = midRgb.map((c, k) => Math.round(c + (pole[k] - c) * share));\n  return `rgb(${r}, ${g}, ${b})`;\n});\n\nconst TITLE = \"heatmap-stripes-climate · javascript · muix · anyplot.ai\";\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const bandWidth = Math.round(width * BAND_WIDTH_SHARE);\n  const bandHeight = Math.round(bandWidth / BAND_ASPECT);\n\n  return (\n    <Box\n      sx={{\n        width,\n        height,\n        bgcolor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        alignItems: \"center\",\n        justifyContent: \"center\",\n      }}\n    >\n      <Typography sx={{ fontSize: 22, fontWeight: 600, color: t.ink, mb: \"24px\" }}>{TITLE}</Typography>\n      {/* Every stripe runs the full band height: only colour encodes the anomaly */}\n      <Box sx={{ width: bandWidth, height: bandHeight, flex: \"none\" }}>\n        <BarChart\n          width={bandWidth}\n          height={bandHeight}\n          skipAnimation\n          hideLegend\n          borderRadius={0}\n          margin={{ top: 0, right: 0, bottom: 0, left: 0 }}\n          series={[\n            {\n              id: \"stripes\",\n              data: rows.map((d) => (d[ANOMALY] == null ? null : 1)),\n              colorGetter: ({ dataIndex }) => stripeColors[dataIndex],\n              valueFormatter: (_value, { dataIndex }) => `${ANOMALY}: ${rows[dataIndex][ANOMALY]}`,\n            },\n          ]}\n          xAxis={[{ scaleType: \"band\", data: rows.map((d) => d[YEAR]), categoryGapRatio: 0, position: \"none\" }]}\n          yAxis={[{ min: 0, max: 1, position: \"none\" }]}\n        />\n      </Box>\n    </Box>\n  );\n}\n"}