Published
Edited
Feb 21, 2019
3 forks
13 stars
Insert cell
Insert cell
Insert cell
Insert cell
{
const svg = d3.select(DOM.svg(width, height))
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 0.5);

while (true) {
yield svg.node();

svg.append("path")
.attr("d", line(dampenedRandomWalk()))
.attr("stroke-dasharray", strokeNone)
.transition()
.ease(d3.easeLinear)
.duration(750)
.attrTween("stroke-dasharray", strokeIn)
.transition()
.attrTween("stroke-dasharray", strokeOut)
.remove();
}
}
Insert cell
function dampenedRandomWalk() {
let target = 0.5;
let value = Math.random();
let values = [value];
for (let i = 0; i < n; ++i) {
values.push(value += (Math.random() - 0.5) * noise + (target - value) * decay);
}
return values;
}
Insert cell
function strokeNone() {
return `0 ${this.getTotalLength()}`;
}
Insert cell
function strokeIn() {
const l = this.getTotalLength();
return d3.interpolate(`0 ${l}`, `${l} 0`);
}
Insert cell
function strokeOut() {
const l = this.getTotalLength();
return d3.interpolate(`0 0 ${l}`, `0 ${l} 0`);
}
Insert cell
line = d3.line().x((d, i) => i / n * width).y(d => d * height)
Insert cell
n = 200
Insert cell
height = 500
Insert cell
d3 = require("d3@5")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more