{"spec_id":"line-multi","library":"muix","language":"javascript","code":"// anyplot.ai\n// line-multi: Multi-Line Comparison Plot\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 86/100 | Updated: 2026-11-08\n\nimport { LineChart } from \"@mui/x-charts/LineChart\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst X_PAD_SHARE = 0.03; // share of the x range added on both sides so the end markers stay whole\nconst Y_PAD_SHARE = 0.06; // share of the value range added below and above the data\n\n// Columns: set these to your data's column names\nconst MONTH = \"Month\"; // role: x\nconst CITY = \"City\"; // role: series\nconst TEMP = \"Average temperature (°C)\"; // role: y\n\n// Data: replace this block with your own records, one object per row\n// Monthly mean temperatures of four cities in distinct climate zones.\nconst data = Object.entries({\n  Reykjavik: [0, 1, 2, 5, 8, 11, 13, 13, 10, 6, 3, 1],\n  Berlin: [1, 2, 6, 11, 16, 19, 21, 21, 16, 11, 6, 2],\n  Dubai: [19, 20, 24, 29, 34, 36, 38, 38, 35, 31, 25, 21],\n  Singapore: [27, 27, 28, 28, 28, 28, 28, 28, 27, 27, 27, 27],\n}).flatMap(([city, temps]) =>\n  [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"].map((month, i) => ({\n    [MONTH]: month,\n    [CITY]: city,\n    [TEMP]: temps[i],\n  })),\n);\n\n// Plot: reads only data and the constants above\n// Months keep their order of appearance and sit on an index axis, so the axis can pad past the end markers.\nconst months = [...new Set(data.map((d) => d[MONTH]))];\nconst cities = [...new Set(data.map((d) => d[CITY]))];\nconst xs = months.map((_, i) => i);\nconst lines = cities.map((city, i) => ({\n  id: `city-${i}`,\n  label: city,\n  color: t.palette[i],\n  values: months.map((m) => data.find((d) => d[CITY] === city && d[MONTH] === m)?.[TEMP] ?? null),\n}));\n\nconst temps = data.map((d) => d[TEMP]);\nconst yLow = Math.min(...temps);\nconst yHigh = Math.max(...temps);\nconst yPad = (yHigh - yLow) * Y_PAD_SHARE;\nconst xPad = (xs.length - 1) * X_PAD_SHARE;\nconst unit = TEMP.match(/\\(([^)]+)\\)/)?.[1] ?? \"\";\n\nconst TITLE = \"line-multi · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${cities.length} series × ${months.length} ${MONTH.toLowerCase()}s · range ${yLow} to ${yHigh} ${unit}`;\nconst HEADER_H = 80;\nconst LEGEND_H = 44;\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 104px\", 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 - LEGEND_H}\n        skipAnimation\n        grid={{ horizontal: true }}\n        margin={{ top: 12, right: 48, bottom: 16, left: 24 }}\n        xAxis={[\n          {\n            data: xs,\n            scaleType: \"linear\",\n            min: -xPad,\n            max: xs.length - 1 + xPad,\n            tickInterval: xs,\n            valueFormatter: (v: number) => months[v] ?? \"\",\n            label: MONTH,\n            height: 64,\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: 76,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        series={lines.map((line) => ({\n          id: line.id,\n          data: line.values,\n          label: line.label,\n          color: line.color,\n          curve: \"monotoneX\",\n          showMark: true,\n          labelMarkType: \"line+mark\",\n          valueFormatter: (v: number | null) => (v == null ? \"\" : `${v} ${unit}`),\n        }))}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"top\", horizontal: \"end\" },\n            sx: { fontSize: 16, color: t.ink, columnGap: 3, pr: 5, \"& .MuiChartsLegend-mark\": { width: 24, height: 24 } },\n          },\n        }}\n        sx={{\n          \"& .MuiLineChart-line\": { strokeWidth: 3 },\n          \"& .MuiLineChart-mark\": { strokeWidth: 2.5 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n        }}\n      />\n    </div>\n  );\n}\n"}