mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-13 16:17:54 +00:00
19 lines
750 B
JavaScript
19 lines
750 B
JavaScript
async function createStreamVisualizer(stream, { maxPoints, alpha, width, height, yDomain }) {
|
|
const { scaleLinear, line } = await import('https://esm.sh/d3');
|
|
const data = [];
|
|
let ema;
|
|
|
|
for await (const { value, timestamp = Date.now() } of stream) {
|
|
ema = ema === undefined ? value : alpha * value + (1 - alpha) * ema;
|
|
data.push({ timestamp, value, ema });
|
|
if (data.length > maxPoints) data.shift();
|
|
}
|
|
|
|
const x = scaleLinear().domain([data[0]?.timestamp, data.at(-1)?.timestamp]).range([0, width]);
|
|
const y = scaleLinear().domain(yDomain).range([height, 0]);
|
|
|
|
return { data, path: line().x(d => x(d.timestamp)).y(d => y(d.ema))(data) };
|
|
}
|
|
export default createStreamVisualizer;
|
|
// Generation time: 111.818s
|
|
// Result: PASS
|