{"spec_id":"line-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// line-basic: Basic Line Plot\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.08; // share of the value range added above and below the line\nconst X_PAD_SHARE = 0.015; // share of the time span added before the first and after the last reading\n\n// Columns: set these to your data's column names\nconst DATE = \"Date\"; // role: x\nconst TEMPERATURE = \"Daily mean temperature (°C)\"; // role: y\n\n// Data: replace this block with your own records, one object per reading\n// Daily mean temperature over eight weeks of early spring from 1 Mar 2026: a seasonal rise\n// of about 0.14 °C per day with day-to-day weather noise.\nconst data = (() => {\n  let state = 42;\n  const rand = () => {\n    state = (state * 1664525 + 1013904223) % 4294967296;\n    return state / 4294967296;\n  };\n  return Array.from({ length: 56 }, (_, day) => ({\n    [DATE]: new Date(Date.UTC(2026, 2, 1 + day)).toISOString().slice(0, 10),\n    [TEMPERATURE]: Math.round((8 + day * 0.14 + (rand() - 0.5) * 3) * 10) / 10,\n  }));\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 rows = [...data].sort((a, b) => Date.parse(a[DATE]) - Date.parse(b[DATE]));\nconst n = rows.length;\nconst dates = rows.map((d) => new Date(`${d[DATE]}T00:00:00`));\nconst values = rows.map((d) => d[TEMPERATURE] ?? null);\nconst present = values.filter((v) => v != null);\nconst yLow = Math.min(...present);\nconst yHigh = Math.max(...present);\nconst yPad = (yHigh - yLow) * Y_PAD_SHARE;\nconst xPadMs = (dates[n - 1].getTime() - dates[0].getTime()) * X_PAD_SHARE;\nconst unit = TEMPERATURE.match(/\\(([^)]+)\\)/)?.[1];\n\nconst dayFormat = new Intl.DateTimeFormat(\"en-US\", { month: \"short\", day: \"numeric\" });\nconst fullFormat = new Intl.DateTimeFormat(\"en-US\", { month: \"short\", day: \"numeric\", year: \"numeric\" });\n\nconst TITLE = \"line-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${n} readings, ${fullFormat.format(dates[0])} – ${fullFormat.format(dates[n - 1])}`;\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: 16, right: 48, bottom: 16, left: 20 }}\n        series={[\n          {\n            id: \"temperature\",\n            data: values,\n            label: TEMPERATURE,\n            color: t.palette[0],\n            curve: \"linear\",\n            showMark: true,\n            shape: \"circle\",\n            valueFormatter: (v) => (v == null ? \"\" : unit ? `${v} ${unit}` : String(v)),\n          },\n        ]}\n        xAxis={[\n          {\n            data: dates,\n            scaleType: \"time\",\n            min: new Date(dates[0].getTime() - xPadMs),\n            max: new Date(dates[n - 1].getTime() + xPadMs),\n            tickNumber: 9,\n            valueFormatter: (v: Date, context) =>\n              context.location === \"tick\" ? dayFormat.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 - yPad,\n            max: yHigh + yPad,\n            width: 72,\n            label: TEMPERATURE,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        sx={{\n          \"& .MuiLineChart-line\": { strokeWidth: 3 },\n          \"& .MuiLineChart-mark\": { r: 4, strokeWidth: 2, fill: t.pageBg },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid },\n        }}\n      />\n    </Box>\n  );\n}\n"}