mirror of
https://github.com/lynchmarkHQ/lynchmark.com.git
synced 2026-10-17 01:07:53 +00:00
22 lines
882 B
JavaScript
22 lines
882 B
JavaScript
async function createStreamVisualizer(asyncIterable, options) {
|
|
const { maxPoints, alpha, width, height, yDomain } = options;
|
|
const { scaleLinear, line } = await import("https://cdn.skypack.dev/d3");
|
|
const data = [];
|
|
let prevEma;
|
|
for await (const value of asyncIterable) {
|
|
prevEma = prevEma === undefined ? value : alpha * value + (1 - alpha) * prevEma;
|
|
data.push({ timestamp: Date.now(), value, ema: prevEma });
|
|
if (data.length > maxPoints) data.splice(0, data.length - maxPoints);
|
|
}
|
|
const x = scaleLinear()
|
|
.domain([data[0]?.timestamp ?? 0, data.at(-1)?.timestamp ?? 1])
|
|
.range([0, width]);
|
|
const y = scaleLinear().domain(yDomain).range([height, 0]);
|
|
const path = line()
|
|
.x(d => x(d.timestamp))
|
|
.y(d => y(d.ema))(data) ?? "";
|
|
return { data, path };
|
|
}
|
|
export default createStreamVisualizer;
|
|
// Generation time: 4.318s
|
|
// Result: FAIL
|