// The `aura chart` viewer: turns the injected window.AURA_TRACES (xs + per-series // Option points, already timestamp-union-aligned by the CLI) into uPlot charts. // // buildCharts is PURE (no DOM, no uPlot) and is the unit the headless guard drives. // mount() is browser-only — it instantiates the vendored global `uPlot` per config. (function () { var PALETTE = ["#89b4fa", "#f38ba8", "#a6e3a1", "#f9e2af", "#cba6f7", "#94e2d5"]; // {xs, series:[{name, y_scale_id, points}]} + mode -> array of {opts, data} configs. // overlay -> one chart, all series, each on its own auto-ranged y-scale (shared xs). // panels -> one chart per series, each on the SAME shared xs (timestamp-aligned). function buildCharts(traces, mode) { var xs = traces.xs; var series = traces.series; if (mode === "panels") { return series.map(function (s, i) { return { opts: { title: s.name, scales: { x: { time: false }, [s.y_scale_id]: {} }, series: [{}, { label: s.name, scale: s.y_scale_id, stroke: PALETTE[i % PALETTE.length] }], axes: [{}, { scale: s.y_scale_id }], }, data: [xs, s.points], }; }); } // overlay (default) var scales = { x: { time: false } }; var uSeries = [{}]; var axes = [{}]; series.forEach(function (s, i) { scales[s.y_scale_id] = {}; uSeries.push({ label: s.name, scale: s.y_scale_id, stroke: PALETTE[i % PALETTE.length] }); // the first two series carry a drawn axis (left, then right); the rest keep // their own auto-scale, read via the legend. if (i < 2) axes.push({ scale: s.y_scale_id, side: i === 0 ? 3 : 1 }); }); return [{ opts: { scales: scales, series: uSeries, axes: axes }, data: [xs].concat(series.map(function (s) { return s.points; })) }]; } // Browser-only: instantiate the vendored global uPlot per config into #stage. function mount() { var traces = window.AURA_TRACES; var configs = buildCharts(traces, traces.mode); var stage = document.getElementById("stage"); var w = stage.clientWidth || 900; var h = configs.length > 1 ? Math.max(160, Math.floor(440 / configs.length)) : 460; configs.forEach(function (cfg) { var opts = Object.assign({ width: w, height: h }, cfg.opts); // eslint-disable-next-line no-undef new uPlot(opts, cfg.data, stage); }); } if (typeof window !== "undefined" && typeof document !== "undefined" && window.AURA_TRACES) { if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", mount); else mount(); } if (typeof module !== "undefined" && module.exports) module.exports = { buildCharts: buildCharts }; })();