{"spec_id":"line-stock-comparison","library":"muix","language":"javascript","code":"// anyplot.ai\n// line-stock-comparison: Stock Price Comparison Chart\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 { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst REBASE_TO = 100; // every series is rebased to this value on its first date (the spec's definition)\nconst Y_PAD_SHARE = 0.06; // share of the rebased range added below and above the lines\n\n// Columns: set these to your data's column names\nconst DATE = \"Trading date\"; // role: date\nconst SYMBOL = \"Symbol\"; // role: symbol\nconst PRICE = \"Adjusted close (USD)\"; // role: price\n\n// Data: replace this block with your own records, one object per row\n// One trading year (252 sessions, weekends skipped) of three large-cap stocks and the S&P 500 ETF.\nconst data = (() => {\n  const sessions = 252;\n  const dates = [];\n  const cursor = new Date(2025, 0, 2);\n  while (dates.length < sessions) {\n    if (cursor.getDay() !== 0 && cursor.getDay() !== 6) dates.push(new Date(cursor));\n    cursor.setDate(cursor.getDate() + 1);\n  }\n  const stocks = [\n    { symbol: \"AAPL\", start: 182, drift: 0.0009, volatility: 0.016, seed: 11 },\n    { symbol: \"GOOGL\", start: 141, drift: 0.0004, volatility: 0.019, seed: 22 },\n    { symbol: \"MSFT\", start: 378, drift: 0.0011, volatility: 0.014, seed: 33 },\n    { symbol: \"SPY\", start: 468, drift: 0.0005, volatility: 0.009, seed: 44 },\n  ];\n  return stocks.flatMap((stock) => {\n    let state = stock.seed >>> 0;\n    let price = stock.start;\n    return dates.map((date, i) => {\n      if (i > 0) {\n        state = (state * 1664525 + 1013904223) >>> 0;\n        price *= 1 + stock.drift + (state / 4294967296 - 0.5) * stock.volatility;\n      }\n      return { [DATE]: date, [SYMBOL]: stock.symbol, [PRICE]: Math.round(price * 100) / 100 };\n    });\n  });\n})();\n\n// Plot: reads only data and the constants above\n// Dates are sorted onto one shared time axis; each symbol is rebased on its own first date.\nconst dateValues = [...new Set(data.map((d) => d[DATE].getTime()))].sort((a, b) => a - b);\nconst dates = dateValues.map((ms) => new Date(ms));\nconst symbols = [...new Set(data.map((d) => d[SYMBOL]))];\nconst lines = symbols.map((symbol, i) => {\n  const rows = data.filter((d) => d[SYMBOL] === symbol).sort((a, b) => a[DATE] - b[DATE]);\n  const base = rows[0][PRICE];\n  const byDate = new Map(rows.map((d) => [d[DATE].getTime(), (d[PRICE] / base) * REBASE_TO]));\n  return { id: `symbol-${i}`, label: symbol, color: t.palette[i], values: dateValues.map((ms) => byDate.get(ms) ?? null) };\n});\n\nconst rebased = lines.flatMap((line) => line.values).filter((v) => v != null);\nconst yLow = Math.min(REBASE_TO, ...rebased);\nconst yHigh = Math.max(REBASE_TO, ...rebased);\nconst yPad = (yHigh - yLow) * Y_PAD_SHARE;\nconst formatDay = (d: Date) => d.toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\", year: \"numeric\" });\n\nconst TITLE = \"line-stock-comparison · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${symbols.length} series · ${dates.length} trading days · ${formatDay(dates[0])} to ${formatDay(dates[dates.length - 1])} · ${PRICE} rebased to ${REBASE_TO}`;\nconst Y_LABEL = `Performance index (first date = ${REBASE_TO})`;\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 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 - LEGEND_H}\n        skipAnimation\n        grid={{ horizontal: true }}\n        margin={{ top: 12, right: 48, bottom: 16, left: 24 }}\n        xAxis={[\n          {\n            data: dates,\n            scaleType: \"time\",\n            tickNumber: 12,\n            valueFormatter: (v: Date, context) =>\n              context.location === \"tick\" ? v.toLocaleDateString(\"en-US\", { month: \"short\", year: \"numeric\" }) : formatDay(v),\n            label: DATE,\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: Y_LABEL,\n            width: 84,\n            tickLabelStyle: { fontSize: 15, 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: false,\n          valueFormatter: (v: number | null) => (v == null ? \"\" : v.toFixed(1)),\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 },\n          },\n        }}\n        sx={{\n          \"& .MuiLineChart-line\": { 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        <ChartsReferenceLine\n          y={REBASE_TO}\n          label={`Start = ${REBASE_TO}`}\n          labelAlign=\"end\"\n          spacing={{ x: 8, y: 8 }}\n          labelStyle={{ fontSize: 14, fill: t.inkSoft }}\n          lineStyle={{ stroke: t.inkSoft, strokeDasharray: \"6 5\", strokeWidth: 1.5 }}\n        />\n      </LineChart>\n    </div>\n  );\n}\n"}