{"spec_id":"horizon-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// horizon-basic: Horizon Chart\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-23\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { AreaPlot } from \"@mui/x-charts/LineChart\";\nimport { Box, Typography } from \"@mui/material\";\n\nconst t = window.ANYPLOT_TOKENS;\nconst SIZE = window.ANYPLOT_SIZE;\n\n// Constants\nconst OUTER_PAD = 32;\nconst LABEL_COL_WIDTH = 180;\nconst RIGHT_PAD = 16;\nconst CHART_WIDTH = SIZE.width - OUTER_PAD * 2 - LABEL_COL_WIDTH - RIGHT_PAD;\nconst ROW_HEIGHT = 70;\nconst HALF_HEIGHT = ROW_HEIGHT / 2;\nconst ROW_GAP = 10;\nconst BAND_COUNT = 3;\nconst ZERO_MARGIN = { top: 0, right: 0, bottom: 0, left: 0 };\nconst SWATCH_WIDTH = 24;\n\n// Columns: set these to your data's column names\nconst DATE = \"Time\"; // role: date\nconst VALUE = \"Deviation from 24h rolling baseline, in percentage points (pp)\"; // role: value\nconst SERIES = \"Service\"; // role: series\n\n// Data: replace this block with your own records, one object per row\nconst data = (() => {\n  const pointCount = 168;\n  const maxDev = 45;\n  const mulberry32 = (seed) => {\n    let s = seed;\n    return () => {\n      s = (s + 0x6d2b79f5) | 0;\n      let v = Math.imul(s ^ (s >>> 15), 1 | s);\n      v = (v + Math.imul(v ^ (v >>> 7), 61 | v)) ^ v;\n      return ((v ^ (v >>> 14)) >>> 0) / 4294967296;\n    };\n  };\n  const bump = (i, center, width) => Math.exp(-(((i - center) / width) ** 2));\n  const daily = (i, phase) => Math.sin((2 * Math.PI * i) / 24 + phase);\n  // Each service has its own load profile: i = hour since start, d = day index (0-6)\n  const servers = [\n    // Business-hours traffic, quieter at the weekend\n    { name: \"api-gateway\", noise: 5, seed: 1013, shape: (i, d) => (d >= 5 ? 9 : 20) * daily(i, -1.9) },\n    // Quiet all week until a login storm on Day 3 morning\n    { name: \"auth-service\", noise: 3, seed: 1069, shape: (i) => 4 * daily(i, -1.9) + 40 * bump(i, 57, 2.5) },\n    // Evening order peaks that grow toward the weekend\n    { name: \"orders-db\", noise: 6, seed: 1151, shape: (i, d) => (14 + 4 * d) * daily(i, -2.8) },\n    // Slow drift across the week plus a nightly 02:00 batch job\n    {\n      name: \"inventory-db\",\n      noise: 4,\n      seed: 1223,\n      shape: (i, d) => -22 + (44 * i) / 167 + 16 * bump(i % 24, 2, 1.2) * (d < 5 ? 1 : 0.4),\n    },\n    // Flat, then a cache flush on Day 5 that drops the hit rate\n    { name: \"redis-cache\", noise: 2.5, seed: 1291, shape: (i) => -42 * bump(i, 106, 3) * (i >= 103 ? 1 : 0.4) },\n    // Nightly re-index bursts, slightly under baseline during the day\n    { name: \"search-index\", noise: 3, seed: 1373, shape: (i) => -7 + 34 * bump(i % 24, 4, 1.4) },\n    // Strong daily cycle, an outage on Day 4 afternoon and the backlog surge after it\n    {\n      name: \"payment-worker\",\n      noise: 5,\n      seed: 1447,\n      shape: (i) => 18 * daily(i, -2.2) - 60 * bump(i, 87, 2) + 52 * bump(i, 93, 2.5),\n    },\n    // Near idle except two newsletter sends at 09:00\n    { name: \"email-worker\", noise: 2, seed: 1523, shape: (i) => -3 + 30 * bump(i, 33, 1.5) + 36 * bump(i, 129, 1.5) },\n  ];\n  const rows = [];\n  servers.forEach((server) => {\n    const rand = mulberry32(server.seed);\n    let walk = 0;\n    for (let i = 0; i < pointCount; i += 1) {\n      const day = Math.floor(i / 24);\n      walk += (rand() - 0.5) * server.noise - walk * 0.3;\n      const val = Math.max(-maxDev, Math.min(maxDev, server.shape(i, day) + walk));\n      rows.push({\n        [DATE]: `Day ${day + 1} · ${String(i % 24).padStart(2, \"0\")}:00`,\n        [SERIES]: server.name,\n        [VALUE]: val,\n      });\n    }\n  });\n  return rows;\n})();\n\n// Plot: reads only data and the constants above\nconst seriesNames = [...new Set(data.map((d) => d[SERIES]))];\nconst dates = [...new Set(data.map((d) => d[DATE]))];\nconst pointCount = dates.length;\nconst xIndex = Array.from({ length: pointCount }, (_, i) => i);\nconst maxDeviation =\n  Math.ceil(Math.max(...data.map((d) => Math.abs(d[VALUE]))) / 15) * 15 || 45;\nconst bandThresholds = Array.from({ length: BAND_COUNT }, (_, k) =>\n  Math.round((maxDeviation * (k + 1)) / BAND_COUNT),\n);\n\nconst seriesData = seriesNames.map((name) => {\n  const values = data.filter((d) => d[SERIES] === name).map((d) => d[VALUE]);\n  return {\n    name,\n    positive: values.map((v) => Math.max(v, 0)),\n    negative: values.map((v) => Math.max(-v, 0)),\n  };\n});\n\nconst TITLE = \"horizon-basic · javascript · muix · anyplot.ai\";\nconst TITLE_FONT_SIZE = Math.round(22 * (TITLE.length > 67 ? 67 / TITLE.length : 1));\n\n// Band colors: each step mixes the theme-adaptive diverging midpoint toward a pole\nconst mid = parseInt(t.div[1].slice(1), 16);\nconst [POS_BAND_COLORS, NEG_BAND_COLORS] = [t.div[2], t.div[0]].map((poleHex) => {\n  const pole = parseInt(poleHex.slice(1), 16);\n  return Array.from({ length: BAND_COUNT }, (_, k) => {\n    const strength = 0.45 + 0.55 * ((k + 1) / BAND_COUNT);\n    const rgb = [16, 8, 0].map((shift) => {\n      const from = (mid >> shift) & 255;\n      const to = (pole >> shift) & 255;\n      return Math.round(from + (to - from) * strength);\n    });\n    return `#${rgb.map((c) => c.toString(16).padStart(2, \"0\")).join(\"\")}`;\n  });\n});\n\n// Top half folds positive values upward; bottom half mirrors negatives downward\nconst HALVES = [\n  { key: \"positive\", colors: POS_BAND_COLORS, top: 0, transform: \"none\" },\n  { key: \"negative\", colors: NEG_BAND_COLORS, top: HALF_HEIGHT, transform: \"scaleY(-1)\" },\n];\n\nconst LEGEND = [\n  {\n    label: \"Below baseline\",\n    colors: [...NEG_BAND_COLORS].reverse(),\n    thresholds: [...bandThresholds].reverse(),\n    direction: \"row-reverse\",\n  },\n  { label: \"Above baseline\", colors: POS_BAND_COLORS, thresholds: bandThresholds, direction: \"row\" },\n];\n\nconst TICKS = [\n  0,\n  Math.round((pointCount - 1) / 4),\n  Math.round((pointCount - 1) / 2),\n  Math.round((3 * (pointCount - 1)) / 4),\n  pointCount - 1,\n];\n\nexport default function Chart() {\n  return (\n    <Box\n      sx={{\n        width: SIZE.width,\n        height: SIZE.height,\n        boxSizing: \"border-box\",\n        p: `${OUTER_PAD}px`,\n        display: \"flex\",\n        flexDirection: \"column\",\n        justifyContent: \"center\",\n      }}\n    >\n      <Typography sx={{ m: 0, fontSize: TITLE_FONT_SIZE, fontWeight: 600, color: t.ink }}>{TITLE}</Typography>\n      <Typography sx={{ m: 0, mb: \"10px\", fontSize: 14, color: t.inkSoft }}>\n        {VALUE}\n      </Typography>\n\n      <Box sx={{ display: \"flex\", justifyContent: \"flex-end\", alignItems: \"center\", gap: \"20px\", mb: \"14px\" }}>\n        {LEGEND.map((entry, entryIndex) => (\n          <Box key={entry.label} sx={{ display: \"flex\", alignItems: \"center\", gap: \"20px\" }}>\n            {entryIndex > 0 && <Box sx={{ width: \"1px\", height: \"20px\", bgcolor: t.grid }} />}\n            <Box sx={{ display: \"flex\", alignItems: \"center\", gap: \"8px\", flexDirection: entry.direction }}>\n              <Typography sx={{ m: 0, fontSize: 15, color: t.inkSoft, whiteSpace: \"nowrap\" }}>{entry.label}</Typography>\n              <Box sx={{ display: \"flex\", flexDirection: \"column\", alignItems: \"center\", gap: \"2px\" }}>\n                <Box sx={{ display: \"flex\", gap: \"3px\" }}>\n                  {entry.colors.map((color) => (\n                    <Box key={color} sx={{ width: SWATCH_WIDTH, height: 14, borderRadius: \"3px\", bgcolor: color }} />\n                  ))}\n                </Box>\n                <Box sx={{ display: \"flex\", gap: \"3px\" }}>\n                  {entry.thresholds.map((threshold) => (\n                    <Typography\n                      key={threshold}\n                      sx={{ m: 0, fontSize: 14, color: t.inkSoft, width: SWATCH_WIDTH, textAlign: \"center\", whiteSpace: \"nowrap\" }}\n                    >\n                      {threshold}\n                    </Typography>\n                  ))}\n                </Box>\n              </Box>\n            </Box>\n          </Box>\n        ))}\n      </Box>\n\n      <Box>\n        {seriesData.map((series, rowIndex) => (\n          <Box\n            key={series.name}\n            sx={{\n              display: \"flex\",\n              alignItems: \"center\",\n              height: ROW_HEIGHT,\n              mb: rowIndex === seriesData.length - 1 ? 0 : `${ROW_GAP}px`,\n            }}\n          >\n            <Box sx={{ width: LABEL_COL_WIDTH, pr: \"14px\", boxSizing: \"border-box\", textAlign: \"right\" }}>\n              <Typography\n                sx={{\n                  m: 0,\n                  fontSize: 17,\n                  fontWeight: 500,\n                  color: t.ink,\n                  whiteSpace: \"nowrap\",\n                  overflow: \"hidden\",\n                  textOverflow: \"ellipsis\",\n                }}\n              >\n                {series.name}\n              </Typography>\n            </Box>\n            <Box sx={{ position: \"relative\", width: CHART_WIDTH, height: ROW_HEIGHT }}>\n              {HALVES.map((half) => (\n                <Box\n                  key={half.key}\n                  sx={{\n                    position: \"absolute\",\n                    top: half.top,\n                    left: 0,\n                    width: CHART_WIDTH,\n                    height: HALF_HEIGHT,\n                    overflow: \"hidden\",\n                    transform: half.transform,\n                  }}\n                >\n                  {/* Each band shares the 0–max domain, shifted down one band height and clipped */}\n                  {half.colors.map((color, bandIndex) => (\n                    <Box\n                      key={color}\n                      sx={{\n                        position: \"absolute\",\n                        left: 0,\n                        bottom: 0,\n                        width: CHART_WIDTH,\n                        height: HALF_HEIGHT * BAND_COUNT,\n                        transform: `translateY(${bandIndex * HALF_HEIGHT}px)`,\n                      }}\n                    >\n                      <ChartsContainer\n                        width={CHART_WIDTH}\n                        height={HALF_HEIGHT * BAND_COUNT}\n                        margin={ZERO_MARGIN}\n                        disableAxisListener\n                        skipAnimation\n                        series={[\n                          {\n                            type: \"line\",\n                            data: series[half.key],\n                            area: true,\n                            baseline: 0,\n                            curve: \"monotoneX\",\n                            color,\n                          },\n                        ]}\n                        xAxis={[\n                          {\n                            scaleType: \"linear\",\n                            data: xIndex,\n                            min: 0,\n                            max: pointCount - 1,\n                            domainLimit: \"strict\",\n                            position: \"none\",\n                          },\n                        ]}\n                        yAxis={[\n                          {\n                            scaleType: \"linear\",\n                            min: 0,\n                            max: maxDeviation,\n                            domainLimit: \"strict\",\n                            position: \"none\",\n                          },\n                        ]}\n                        sx={{ \"& .MuiLineChart-area\": { fill: color, fillOpacity: 1 } }}\n                      >\n                        <AreaPlot skipAnimation slotProps={{ area: { style: { fill: color, fillOpacity: 1 } } }} />\n                      </ChartsContainer>\n                    </Box>\n                  ))}\n                </Box>\n              ))}\n              <Box sx={{ position: \"absolute\", top: HALF_HEIGHT, left: 0, width: CHART_WIDTH, height: \"1px\", bgcolor: t.grid }} />\n            </Box>\n          </Box>\n        ))}\n      </Box>\n\n      <Box sx={{ display: \"flex\", mt: \"14px\" }}>\n        <Box sx={{ width: LABEL_COL_WIDTH, flexShrink: 0 }} />\n        <Box sx={{ position: \"relative\", width: CHART_WIDTH, height: \"18px\" }}>\n          {TICKS.map((index) => (\n            <Typography\n              key={index}\n              component=\"span\"\n              sx={{\n                m: 0,\n                position: \"absolute\",\n                left: `${(index / (pointCount - 1)) * 100}%`,\n                transform: index === 0 ? \"none\" : index === pointCount - 1 ? \"translateX(-100%)\" : \"translateX(-50%)\",\n                fontSize: 14,\n                color: t.inkSoft,\n                whiteSpace: \"nowrap\",\n              }}\n            >\n              {dates[index]}\n            </Typography>\n          ))}\n        </Box>\n      </Box>\n    </Box>\n  );\n}\n"}