{"spec_id":"streamgraph-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// streamgraph-basic: Basic Stream Graph\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 86/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 Y_PAD_SHARE = 0.04; // room above and below the stream\n\n// Columns: set these to your data's column names\nconst MONTH = \"Month\"; // role: time\nconst GENRE = \"Genre\"; // role: category\nconst HOURS = \"Streaming hours (thousands)\"; // role: value\n\n// Data: replace this block with your own records, one object per row\n// Monthly streaming hours by music genre over two years: Electronic and Hip-Hop\n// grow, Rock cools off, Jazz and Classical decline slowly, Pop stays flat\nconst data = (() => {\n  let seed = 42;\n  const genres = [\n    { name: \"Pop\", base: 380, amp: 55, freq: 0.55, phase: 0.4, trend: 0.4 },\n    { name: \"Hip-Hop\", base: 300, amp: 70, freq: 0.45, phase: 2.1, trend: 5.5 },\n    { name: \"Electronic\", base: 190, amp: 60, freq: 0.65, phase: 4.0, trend: 8.5 },\n    { name: \"Rock\", base: 260, amp: 45, freq: 0.35, phase: 1.2, trend: -3.5 },\n    { name: \"Jazz\", base: 140, amp: 25, freq: 0.5, phase: 3.3, trend: -2.0 },\n    { name: \"Classical\", base: 100, amp: 18, freq: 0.4, phase: 5.1, trend: -1.2 },\n  ];\n  const rows: { [k: string]: Date | string | number }[] = [];\n  for (const g of genres) {\n    for (let i = 0; i < 24; i += 1) {\n      seed = (seed * 1103515245 + 12345) & 0x7fffffff;\n      const wave = Math.sin(i * g.freq * 0.5 + g.phase) * g.amp;\n      const noise = (seed / 0x7fffffff - 0.5) * g.amp * 0.3;\n      rows.push({\n        [MONTH]: new Date(2024, i, 1),\n        [GENRE]: g.name,\n        [HOURS]: Math.round(Math.max(30, g.base + g.trend * i + wave + noise)),\n      });\n    }\n  }\n  return rows;\n})();\n\n// Plot: reads only data and the constants above\n// Layers are stacked inside-out and mirrored about the middle axis (silhouette), so only their thickness carries a value\nconst months = [...new Set(data.map((d) => (d[MONTH] as Date).getTime()))].sort((a, b) => a - b).map((ms) => new Date(ms));\nconst genres = [...new Set(data.map((d) => d[GENRE] as string))];\nconst series = genres.map((genre, i) => ({\n  id: `genre-${i}`,\n  label: genre,\n  color: t.palette[i],\n  data: months.map(\n    (m) => (data.find((d) => d[GENRE] === genre && (d[MONTH] as Date).getTime() === m.getTime())?.[HOURS] as number) ?? null,\n  ),\n  area: true,\n  stack: \"stream\",\n  stackOrder: \"insideOut\" as const,\n  stackOffset: \"silhouette\" as const,\n  curve: \"catmullRom\" as const,\n  showMark: false,\n  labelMarkType: \"square\" as const,\n}));\nconst totals = months.map((_, k) => series.reduce((sum, s) => sum + (s.data[k] ?? 0), 0));\nconst streamHalf = (Math.max(...totals) / 2) * (1 + Y_PAD_SHARE);\n\nconst MONTH_FORMAT: Intl.DateTimeFormatOptions = { month: \"short\", year: \"numeric\" };\nconst TITLE = \"streamgraph-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `Layer thickness: ${HOURS} · ${months.length} months, ${months[0].toLocaleDateString(\"en-US\", MONTH_FORMAT)} to ${months[months.length - 1].toLocaleDateString(\"en-US\", MONTH_FORMAT)}`;\nconst HEADER_H = 76;\n\nexport default function Chart() {\n  const { width: W, height: H } = window.ANYPLOT_SIZE;\n\n  return (\n    <Box sx={{ width: W, height: H, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <Box\n        sx={{\n          height: HEADER_H,\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: \"6px\",\n        }}\n      >\n        <Typography sx={{ color: t.ink, fontSize: 22, fontWeight: 600, lineHeight: 1.2 }}>{TITLE}</Typography>\n        <Typography sx={{ color: t.inkSoft, fontSize: 15, lineHeight: 1.2 }}>{SUBTITLE}</Typography>\n      </Box>\n      <LineChart\n        width={W}\n        height={H - HEADER_H - 40}\n        skipAnimation\n        series={series}\n        margin={{ top: 16, right: 48, bottom: 8, left: 48 }}\n        xAxis={[\n          {\n            scaleType: \"point\",\n            data: months,\n            label: MONTH,\n            height: 64,\n            valueFormatter: (d: Date) => d.toLocaleDateString(\"en-US\", MONTH_FORMAT),\n            tickInterval: (d: Date) => d.getMonth() % 3 === 0,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            position: \"none\",\n            min: -streamHalf,\n            max: streamHalf,\n          },\n        ]}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"top\", horizontal: \"center\" },\n            sx: { fontSize: 15, color: t.ink, gap: \"20px\" },\n          },\n        }}\n        sx={{\n          \"& .MuiLineChart-area\": { fillOpacity: 1, filter: \"none\" },\n          \"& .MuiLineChart-line\": { strokeWidth: 1.5 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n        }}\n      />\n    </Box>\n  );\n}\n"}