{"spec_id":"line-timeseries","library":"muix","language":"javascript","code":"// anyplot.ai\n// line-timeseries: Time Series Line Plot\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 { useXScale, useDrawingArea } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst DAY_MS = 86_400_000;\nconst DAY_ROW_MAX_DAYS = 14; // up to this span the ticks show times of day under a row of day labels\nconst MONTH_DAY_MAX_DAYS = 730; // up to this span the ticks show month and day, beyond it the year\nconst Y_PAD_SHARE = 0.1; // share of the value range added below and above the line\n\n// Columns: set these to your data's column names\nconst TIMESTAMP = \"Timestamp\"; // role: date\nconst TEMP = \"Air temperature (°F)\"; // role: value\n\n// Data: replace this block with your own records, one object per row\n// Hourly rooftop sensor readings for one week: diurnal cycle, a gentle warm-up and sensor noise.\nconst data = (() => {\n  let seed = 11;\n  const rand = () => {\n    seed = (seed * 1103515245 + 12345) & 0x7fffffff;\n    return seed / 0x7fffffff;\n  };\n  const start = new Date(2024, 5, 3, 0, 0).getTime();\n  return Array.from({ length: 7 * 24 }, (_, i) => {\n    const diurnal = 8 * Math.sin((((i % 24) - 9) / 24) * 2 * Math.PI);\n    const warmup = Math.floor(i / 24) * 0.7;\n    const noise = (rand() - 0.5) * 2.2;\n    return { [TIMESTAMP]: new Date(start + i * 3_600_000), [TEMP]: Math.round((61 + diurnal + warmup + noise) * 10) / 10 };\n  });\n})();\n\n// Plot: reads only data and the constants above\nconst rows = [...data].sort((a, b) => a[TIMESTAMP] - b[TIMESTAMP]);\nconst times = rows.map((d) => d[TIMESTAMP]);\nconst values = rows.map((d) => d[TEMP]);\nconst tMin = times[0].getTime();\nconst tMax = times[times.length - 1].getTime();\nconst spanDays = (tMax - tMin) / DAY_MS;\nconst unit = TEMP.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\n\nconst present = values.filter((v) => v != null);\nconst yLow = Math.min(...present);\nconst yHigh = Math.max(...present);\nconst yPad = (yHigh - yLow) * Y_PAD_SHARE;\n\n// Tick format adapts to the span of the data; a short span adds a row of day labels below the times\nconst tickFormat =\n  spanDays <= DAY_ROW_MAX_DAYS\n    ? { hour: \"2-digit\", minute: \"2-digit\", hourCycle: \"h23\" }\n    : spanDays <= MONTH_DAY_MAX_DAYS\n      ? { month: \"short\", day: \"numeric\" }\n      : { year: \"numeric\" };\nconst days = [];\nif (spanDays <= DAY_ROW_MAX_DAYS) {\n  const cursor = new Date(tMin);\n  cursor.setHours(0, 0, 0, 0);\n  while (cursor.getTime() <= tMax) {\n    const dayStart = cursor.getTime();\n    cursor.setDate(cursor.getDate() + 1);\n    days.push({\n      start: Math.max(dayStart, tMin),\n      end: Math.min(cursor.getTime(), tMax),\n      label: new Date(dayStart).toLocaleDateString(\"en-US\", { weekday: \"short\", month: \"short\", day: \"numeric\" }),\n    });\n  }\n}\nconst formatFull = (d: Date) =>\n  d.toLocaleString(\"en-US\", { weekday: \"short\", month: \"short\", day: \"numeric\", hour: \"2-digit\", minute: \"2-digit\", hourCycle: \"h23\" });\nconst formatDay = (d: Date) => d.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\", year: \"numeric\" });\n\nconst TITLE = \"Rooftop Sensor Temperature · line-timeseries · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${rows.length} readings · ${formatDay(times[0])} to ${formatDay(times[times.length - 1])} · ${yLow} to ${yHigh} ${unit}`;\nconst HEADER_H = 80;\nconst X_AXIS_H = days.length > 0 ? 112 : 72;\n\nfunction DayRow() {\n  const xScale = useXScale();\n  const { top, height } = useDrawingArea();\n  const bottom = top + height;\n  return (\n    <g>\n      {days.map((day) => (\n        <g key={day.start}>\n          {/* The separator breaks at the tick row so it never crosses the \"00:00\" labels */}\n          {day.start > tMin &&\n            [\n              [top, bottom],\n              [bottom + 34, bottom + 62],\n            ].map(([y1, y2]) => (\n              <line\n                key={y1}\n                x1={xScale(new Date(day.start))}\n                x2={xScale(new Date(day.start))}\n                y1={y1}\n                y2={y2}\n                stroke={t.inkSoft}\n                strokeOpacity={0.5}\n                strokeWidth={1}\n                strokeDasharray=\"3 3\"\n              />\n            ))}\n          <text\n            x={(xScale(new Date(day.start)) + xScale(new Date(day.end))) / 2}\n            y={bottom + 54}\n            textAnchor=\"middle\"\n            fontSize={15}\n            fontWeight={600}\n            fill={t.ink}\n          >\n            {day.label}\n          </text>\n        </g>\n      ))}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <div style={{ height: HEADER_H, padding: \"16px 40px 0 112px\", boxSizing: \"border-box\" }}>\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.25 }}>{TITLE}</div>\n        <div style={{ fontSize: 15, color: t.inkSoft, marginTop: 6 }}>{SUBTITLE}</div>\n      </div>\n      <LineChart\n        width={width}\n        height={height - HEADER_H}\n        skipAnimation\n        hideLegend\n        grid={{ horizontal: true, vertical: true }}\n        margin={{ top: 16, right: 48, bottom: 12, left: 24 }}\n        xAxis={[\n          {\n            data: times,\n            scaleType: \"time\",\n            min: times[0],\n            max: times[times.length - 1],\n            tickNumber: 14,\n            valueFormatter: (v: Date, context) =>\n              context.location === \"tick\" ? v.toLocaleString(\"en-US\", tickFormat) : formatFull(v),\n            label: TIMESTAMP,\n            height: X_AXIS_H,\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            min: yLow - yPad,\n            max: yHigh + yPad,\n            label: TEMP,\n            width: 84,\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        series={[\n          {\n            id: \"value\",\n            data: values,\n            label: TEMP,\n            color: t.palette[0],\n            curve: \"linear\",\n            showMark: false,\n            valueFormatter: (v: number | null) => (v == null ? \"\" : `${v.toFixed(1)} ${unit}`),\n          },\n        ]}\n        sx={{\n          \"& .MuiLineChart-line\": { 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        <DayRow />\n      </LineChart>\n    </div>\n  );\n}\n"}