Files
lynchmark.com/tests/9_stream_visualizer/outputs_gemini/gemini-3-pro-preview TEMP_0.7.js
T

20 lines
756 B
JavaScript

export const createStreamVisualizer = async (src, { maxPoints: m, alpha: a, width: w, height: h, yDomain: yd }) => {
const { scaleLinear, line } = await import('https://esm.sh/d3@7');
const data = [];
let ema;
for await (const { timestamp: t, value: v } of src) {
ema = ema === undefined ? v : a * v + (1 - a) * ema;
data.push({ timestamp: t, value: v, ema });
if (data.length > m) data.shift();
}
if (!data.length) return { data, path: '' };
const x = scaleLinear().domain([data[0].timestamp, data.at(-1).timestamp]).range([0, w]);
const y = scaleLinear().domain(yd).range([h, 0]);
const d = line().x(p => x(p.timestamp)).y(p => y(p.ema))(data);
return { data, path: d || '' };
};
export default createStreamVisualizer;