mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-14 08:37:56 +00:00
23 lines
784 B
JavaScript
23 lines
784 B
JavaScript
export async function createStreamVisualizer(iter, o) {
|
|
const { maxPoints, alpha, width, height, yDomain } = o
|
|
const d3 = await import('https://cdn.jsdelivr.net/npm/d3@7/+esm')
|
|
const data = []
|
|
let ema
|
|
for await (const { timestamp, value } of iter) {
|
|
ema = ema == null ? value : alpha * value + (1 - alpha) * ema
|
|
data.push({ timestamp, value, ema })
|
|
if (data.length > maxPoints) data.shift()
|
|
}
|
|
const xs = d3.scaleLinear()
|
|
.domain([data[0].timestamp, data[data.length - 1].timestamp])
|
|
.range([0, width])
|
|
const ys = d3.scaleLinear()
|
|
.domain(yDomain)
|
|
.range([height, 0])
|
|
const line = d3.line()
|
|
.x(d => xs(d.timestamp))
|
|
.y(d => ys(d.ema))
|
|
const path = line(data) || ''
|
|
return { data, path }
|
|
}
|
|
export default createStreamVisualizer; |