{"spec_id":"line-stepwise","library":"muix","language":"javascript","code":"// anyplot.ai\n// line-stepwise: Step Line Plot\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-23\n\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\nimport { Box, Typography } from \"@mui/material\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Columns: set these to your data's column names\nconst X = \"Time of Day\"; // role: x\nconst Y = \"Setpoint (°F)\"; // role: y\n\n// Data: replace this block with your own records, one object per row\n// Smart-thermostat setpoint schedule, logged every 15 minutes over a day.\nconst SAMPLES_PER_HOUR = 4;\nconst schedule = [\n  { start: 0, hours: 6, setpoint: 62 }, // overnight sleep mode\n  { start: 6, hours: 2, setpoint: 68 }, // morning warm-up\n  { start: 8, hours: 9, setpoint: 65 }, // away / eco during work hours\n  { start: 17, hours: 5, setpoint: 72 }, // evening comfort\n  { start: 22, hours: 2, setpoint: 62 }, // night wind-down\n];\nconst data = schedule.flatMap((block) =>\n  Array.from({ length: block.hours * SAMPLES_PER_HOUR }, (_, i) => ({\n    [X]: block.start + i / SAMPLES_PER_HOUR,\n    [Y]: block.setpoint,\n  })),\n);\n\n// Plot: reads only data and the constants above\nconst hoursOfDay = data.map((d) => d[X]);\nconst setpoints = data.map((d) => d[Y]);\n\n// Unit from the y column's name, e.g. \"Setpoint (°F)\" -> \"°F\"\nconst Y_UNIT = Y.match(/\\(([^)]*)\\)\\s*$/)?.[1] ?? \"\";\nconst formatY = (v: number) =>\n  Y_UNIT.startsWith(\"°\") || Y_UNIT === \"%\" ? `${v}${Y_UNIT}` : `${v} ${Y_UNIT}`.trim();\n\nconst TITLE = \"line-stepwise · javascript · muix · anyplot.ai\";\nconst TITLE_H = 56;\nconst AREA_GRADIENT_ID = \"stepAreaFillGradient\";\n\nconst X_MIN = Math.floor(Math.min(...hoursOfDay));\nconst X_MAX = Math.ceil(Math.max(...hoursOfDay));\nconst MIN_SETPOINT = Math.min(...setpoints);\nconst PEAK_SETPOINT = Math.max(...setpoints);\nconst Y_SPAN = PEAK_SETPOINT - MIN_SETPOINT || 1;\nconst Y_MIN = Math.floor(MIN_SETPOINT - 0.7 * Y_SPAN);\nconst Y_MAX = Math.ceil(PEAK_SETPOINT + 0.6 * Y_SPAN);\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\" }}>\n      <Typography sx={{ fontSize: 22, fontWeight: 600, color: t.ink, px: \"40px\", pt: \"14px\", lineHeight: 1.2 }}>\n        {TITLE}\n      </Typography>\n      <LineChart\n        width={width}\n        height={height - TITLE_H}\n        skipAnimation\n        disableHitArea\n        hideLegend\n        series={[\n          {\n            data: setpoints,\n            label: Y,\n            color: t.palette[0],\n            curve: \"step\",\n            area: true,\n            showMark: false,\n            valueFormatter: (v) => (v == null ? \"\" : formatY(v)),\n          },\n        ]}\n        xAxis={[\n          {\n            height: 64,\n            data: hoursOfDay,\n            scaleType: \"linear\",\n            label: X,\n            min: X_MIN,\n            max: X_MAX,\n            tickNumber: 8,\n            valueFormatter: (h: number) =>\n              `${String(Math.floor(h)).padStart(2, \"0\")}:${String(Math.round((h % 1) * 60)).padStart(2, \"0\")}`,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            width: 76,\n            min: Y_MIN,\n            max: Y_MAX,\n            label: Y,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        margin={{ top: 20, right: 56, bottom: 24, left: 40 }}\n        grid={{ horizontal: true }}\n        sx={{\n          \"& .MuiLineChart-line\": { strokeWidth: 3 },\n          \"& .MuiLineChart-area\": { fill: `url(#${AREA_GRADIENT_ID})` },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n        }}\n      >\n        <defs>\n          <linearGradient id={AREA_GRADIENT_ID} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n            <stop offset=\"0%\" stopColor={t.palette[0]} stopOpacity={0.35} />\n            <stop offset=\"100%\" stopColor={t.palette[0]} stopOpacity={0.03} />\n          </linearGradient>\n        </defs>\n        <ChartsReferenceLine\n          y={PEAK_SETPOINT}\n          label={`Peak ${formatY(PEAK_SETPOINT)}`}\n          labelAlign=\"start\"\n          lineStyle={{ stroke: t.ink, strokeDasharray: \"6 4\", strokeOpacity: 0.5 }}\n          labelStyle={{ fill: t.inkSoft, fontSize: 13 }}\n        />\n      </LineChart>\n    </Box>\n  );\n}\n"}