{"spec_id":"andrews-curves","library":"muix","language":"javascript","code":"// anyplot.ai\n// andrews-curves: Andrews Curves for Multivariate Data\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 88/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 TITLE = \"andrews-curves · javascript · muix · anyplot.ai\";\nconst TITLE_H = 84;\nconst LEGEND_H = 44;\nconst T_MIN = -Math.PI; // the Fourier parameter covers one full period\nconst T_MAX = Math.PI;\nconst T_COUNT = 121; // samples along t, dense enough that no corners show\nconst T_TICKS = [-Math.PI, -Math.PI / 2, 0, Math.PI / 2, Math.PI];\nconst T_TICK_LABELS = [\"−π\", \"−π/2\", \"0\", \"π/2\", \"π\"];\nconst X_LABEL = \"Fourier parameter t\";\nconst Y_LABEL = \"Andrews function f(t)\";\nconst CURVE_OPACITY = 0.4; // single observations stay translucent so bundles read darker\n\n// Columns: set these to your data's column names\nconst SPECIES = \"Species\"; // role: category\nconst VARIABLES = [\n  \"Sepal length (cm)\",\n  \"Sepal width (cm)\",\n  \"Petal length (cm)\",\n  \"Petal width (cm)\",\n]; // role: variable\n\n// Data: replace this block with your own records, one object per row\n// Iris-like flower measurements, 20 per species: means and spreads follow the\n// classic Iris species, drawn with a fixed-seed LCG (the browser has no seeded RNG)\nconst data = [\n  { name: \"Setosa\", seed: 11, means: [5.0, 3.4, 1.5, 0.25], spreads: [0.35, 0.38, 0.17, 0.11] },\n  { name: \"Versicolor\", seed: 23, means: [5.9, 2.8, 4.3, 1.33], spreads: [0.52, 0.31, 0.47, 0.2] },\n  { name: \"Virginica\", seed: 37, means: [6.6, 3.0, 5.55, 2.03], spreads: [0.64, 0.32, 0.55, 0.27] },\n].flatMap(({ name, seed, means, spreads }) => {\n  let state = seed;\n  const rand = () => {\n    state = (state * 1664525 + 1013904223) % 4294967296;\n    return state / 4294967296;\n  };\n  return Array.from({ length: 20 }, () => ({\n    [SPECIES]: name,\n    ...Object.fromEntries(\n      VARIABLES.map((col, j) => {\n        const u1 = Math.max(rand(), 1e-9);\n        const u2 = rand();\n        const z = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);\n        return [col, Number((means[j] + z * spreads[j]).toFixed(2))];\n      }),\n    ),\n  }));\n});\n\n// Plot: reads only data and the constants above\nconst speciesList = [...new Set(data.map((d) => d[SPECIES]))];\n\n// z-score every variable over the whole table so no single measurement dominates\nconst varMeans = VARIABLES.map((col) => data.reduce((s, d) => s + d[col], 0) / data.length);\nconst varSds = VARIABLES.map((col, j) =>\n  Math.sqrt(data.reduce((s, d) => s + (d[col] - varMeans[j]) ** 2, 0) / (data.length - 1)),\n);\n\n// f(t) = x1/√2 + x2·sin(t) + x3·cos(t) + x4·sin(2t) + x5·cos(2t) + …\nconst tGrid = Array.from({ length: T_COUNT }, (_, k) => T_MIN + (k * (T_MAX - T_MIN)) / (T_COUNT - 1));\nconst curves = data.map((d) =>\n  tGrid.map((tv) =>\n    VARIABLES.reduce((sum, col, j) => {\n      const z = (d[col] - varMeans[j]) / (varSds[j] || 1);\n      const harmonic = Math.ceil(j / 2);\n      const basis = j === 0 ? Math.SQRT1_2 : j % 2 === 1 ? Math.sin(harmonic * tv) : Math.cos(harmonic * tv);\n      return sum + z * basis;\n    }, 0),\n  ),\n);\n\n// Pointwise mean curve per species, drawn bold on top of its translucent bundle\nconst meanCurves = speciesList.map((sp) => {\n  const members = curves.filter((_, i) => data[i][SPECIES] === sp);\n  return tGrid.map((_, k) => members.reduce((s, c) => s + c[k], 0) / members.length);\n});\n\nconst SUBTITLE = `${data.length} observations · ${VARIABLES.length} variables, z-scored · bold line: mean curve per ${SPECIES.toLowerCase()}`;\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      <Box sx={{ height: TITLE_H, px: \"40px\", pt: \"18px\", boxSizing: \"border-box\" }}>\n        <Typography sx={{ color: t.ink, fontSize: \"22px\", fontWeight: 600, lineHeight: 1.2 }}>\n          {TITLE}\n        </Typography>\n        <Typography sx={{ color: t.inkSoft, fontSize: \"15px\", lineHeight: 1.2, mt: \"8px\" }}>\n          {SUBTITLE}\n        </Typography>\n      </Box>\n      <LineChart\n        width={width}\n        height={height - TITLE_H - LEGEND_H}\n        skipAnimation\n        grid={{ horizontal: true }}\n        axisHighlight={{ x: \"line\", y: \"none\" }}\n        xAxis={[\n          {\n            data: tGrid,\n            scaleType: \"linear\",\n            min: T_MIN,\n            max: T_MAX,\n            tickInterval: T_TICKS,\n            valueFormatter: (v: number, ctx: { location: string }) =>\n              ctx.location === \"tick\" ? T_TICK_LABELS[T_TICKS.indexOf(v)] : `t = ${v.toFixed(2)}`,\n            label: X_LABEL,\n            height: 64,\n            disableTicks: true,\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            label: Y_LABEL,\n            width: 76,\n            disableLine: true, // the horizontal grid already carries the y scale\n            disableTicks: true,\n            valueFormatter: (v: number) => v.toFixed(1),\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        series={[\n          ...data.map((d, i) => ({\n            id: `obs-${i}`,\n            data: curves[i],\n            color: t.palette[speciesList.indexOf(d[SPECIES])],\n            curve: \"natural\" as const,\n            showMark: false,\n          })),\n          ...speciesList.map((sp, g) => ({\n            id: `mean-${g}`,\n            data: meanCurves[g],\n            color: t.palette[g],\n            label: sp,\n            curve: \"natural\" as const,\n            showMark: false,\n          })),\n        ]}\n        margin={{ top: 12, right: 40, bottom: 8, left: 24 }}\n        slotProps={{\n          legend: {\n            position: { vertical: \"bottom\", horizontal: \"center\" },\n            sx: { fontSize: 15, color: t.ink },\n          },\n        }}\n        sx={{\n          \"& .MuiLineChart-line\": { strokeWidth: 1.75 },\n          '& .MuiLineChart-line[data-series^=\"obs-\"]': { strokeOpacity: CURVE_OPACITY },\n          '& .MuiLineChart-line[data-series^=\"mean-\"]': { strokeWidth: 3.5 },\n          \"& .MuiChartsAxisHighlight-root\": { stroke: t.inkSoft, strokeDasharray: \"4 3\" },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n        }}\n      />\n    </Box>\n  );\n}\n"}