{"spec_id":"line-annotated-events","library":"muix","language":"javascript","code":"// anyplot.ai\n// line-annotated-events: Annotated Line Plot with Event Markers\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 89/100 | Updated: 2026-11-08\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\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.05; // share of the value range added below the lowest value\nconst Y_HEADROOM_SHARE = 0.18; // room above the highest value for two rows of event labels\nconst LABEL_ROW_PX = 26; // vertical offset between the two alternating label rows\n\n// Columns: set these to your data's column names\nconst DATE = \"Date\"; // role: date\nconst VALUE = \"Daily active users (thousands)\"; // role: value\nconst EVENT_DATE = \"Event date\"; // role: event_date\nconst EVENT_LABEL = \"Event\"; // role: event_label\n\n// Data: replace this block with your own records: one object per day of the series\n// (date and value) and one per event (event date and label); other cells stay null\n// Daily active users of a SaaS product from 1 Jan to 27 Sep 2025: a slow upward drift with\n// daily noise, a lasting step up on each launch, and a sharp dip on the outage day that\n// halves every day after as users come back.\nconst data = (() => {\n  let seed = 42;\n  const rand = () => {\n    seed = (seed * 1103515245 + 12345) & 0x7fffffff;\n    return seed / 0x7fffffff;\n  };\n  const isoDay = (i: number) => new Date(Date.UTC(2025, 0, 1 + i)).toISOString().slice(0, 10);\n  const events = [\n    { day: 34, label: \"Beta launch\", step: 6, dip: 0 },\n    { day: 96, label: \"Mobile app release\", step: 13, dip: 0 },\n    { day: 130, label: \"Service outage\", step: 0, dip: -15 },\n    { day: 162, label: \"API v2 launch\", step: 9, dip: 0 },\n    { day: 228, label: \"Enterprise tier\", step: 17, dip: 0 },\n  ];\n  const series = [];\n  let level = 42;\n  let dip = 0;\n  for (let i = 0; i < 270; i += 1) {\n    const event = events.find((e) => e.day === i);\n    level = Math.max(5, level + 0.11 + (rand() - 0.5) * 1.6 + (event?.step ?? 0));\n    dip = dip / 2 + (event?.dip ?? 0);\n    const users = Math.max(5, level + dip);\n    series.push({ [DATE]: isoDay(i), [VALUE]: Math.round(users * 10) / 10, [EVENT_DATE]: null, [EVENT_LABEL]: null });\n  }\n  const marks = events.map((e) => ({ [DATE]: null, [VALUE]: null, [EVENT_DATE]: isoDay(e.day), [EVENT_LABEL]: e.label }));\n  return [...series, ...marks];\n})();\n\n// Plot: reads only data and the constants above\n// ISO dates are read as local midnight, the frame the time axis places its ticks in\nconst points = data\n  .filter((d) => d[DATE] != null && d[VALUE] != null)\n  .sort((a, b) => Date.parse(a[DATE]) - Date.parse(b[DATE]));\nconst events = data\n  .filter((d) => d[EVENT_DATE] != null && d[EVENT_LABEL] != null)\n  .sort((a, b) => Date.parse(a[EVENT_DATE]) - Date.parse(b[EVENT_DATE]));\nconst dates = points.map((d) => new Date(`${d[DATE]}T00:00:00`));\nconst values = points.map((d) => d[VALUE]);\nconst eventDates = events.map((e) => new Date(`${e[EVENT_DATE]}T00:00:00`));\nconst n = points.length;\n\nconst yLow = Math.min(...values);\nconst yHigh = Math.max(...values);\nconst yRange = yHigh - yLow;\n\n// One tick on the first day of every month; a time axis applies tickInterval's\n// filter-function form only on ordinal scales, so the filter runs on the dates here\nconst monthTicks = dates.filter((d) => d.getDate() === 1);\n\nconst monthFormat = new Intl.DateTimeFormat(\"en-US\", { month: \"short\" });\nconst fullFormat = new Intl.DateTimeFormat(\"en-US\", { month: \"short\", day: \"numeric\", year: \"numeric\" });\n\nconst TITLE = \"line-annotated-events · javascript · muix · anyplot.ai\";\nconst SUBTITLE =\n  `${n} days, ${fullFormat.format(dates[0])} – ${fullFormat.format(dates[n - 1])} · ` +\n  `${events.length} events, each a dashed line at its date`;\nconst HEADER_H = 84;\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\", overflow: \"hidden\" }}>\n      <Box sx={{ height: HEADER_H, pt: \"14px\", textAlign: \"center\", boxSizing: \"border-box\" }}>\n        <Typography sx={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.3 }}>{TITLE}</Typography>\n        <Typography sx={{ fontSize: 15, color: t.inkSoft, lineHeight: 1.6 }}>{SUBTITLE}</Typography>\n      </Box>\n      <LineChart\n        width={width}\n        height={height - HEADER_H}\n        skipAnimation\n        hideLegend\n        grid={{ horizontal: true }}\n        margin={{ top: 12, right: 40, bottom: 16, left: 20 }}\n        series={[\n          {\n            id: \"value\",\n            data: values,\n            label: VALUE,\n            color: t.palette[0],\n            curve: \"linear\",\n            showMark: false,\n          },\n        ]}\n        xAxis={[\n          {\n            data: dates,\n            scaleType: \"time\",\n            domainLimit: \"strict\",\n            tickInterval: monthTicks,\n            valueFormatter: (v: Date, context) =>\n              context.location === \"tick\" ? monthFormat.format(v) : fullFormat.format(v),\n            label: DATE,\n            height: 64,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            min: yLow - yRange * Y_PAD_SHARE,\n            max: yHigh + yRange * Y_HEADROOM_SHARE,\n            width: 72,\n            label: VALUE,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        sx={{\n          \"& .MuiLineChart-line\": { strokeWidth: 3 },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n        }}\n      >\n        {/* Consecutive events alternate between two label rows so close neighbours never collide */}\n        {events.map((e, i) => (\n          <ChartsReferenceLine\n            key={`${e[EVENT_DATE]}-${e[EVENT_LABEL]}`}\n            x={eventDates[i]}\n            label={e[EVENT_LABEL]}\n            labelAlign=\"start\"\n            spacing={{ x: 8, y: 8 + (i % 2) * LABEL_ROW_PX }}\n            labelStyle={{ fontSize: 15, fontWeight: 600, fill: t.ink }}\n            lineStyle={{ stroke: t.inkSoft, strokeWidth: 1.75, strokeDasharray: \"6 4\" }}\n          />\n        ))}\n      </LineChart>\n    </Box>\n  );\n}\n"}