Published
Edited
Jul 29, 2020
Insert cell
md`# Animation`
Insert cell
Insert cell
lineLength = svg`<path d="${line(data)}">`.getTotalLength()
Insert cell
viewof replay = html`<button>Replay`
Insert cell
Insert cell
Insert cell
Insert cell
ramp(d3.interpolateRgb("steelblue", "orange"))
Insert cell
ramp(d3.interpolateRgb.gamma(2.2)("steelblue", "orange"))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
timeframe
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const zx = x.copy(); // x, but with a new domain.

const line = d3.line()
.x(d => zx(d.date))
.y(d => y(d.close));

const path = svg.append("path")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-miterlimit", 1)
.attr("d", line(data));

const gx = svg.append("g")
.call(xAxis, zx);

const gy = svg.append("g")
.call(yAxis, y);

return Object.assign(svg.node(), {
update(domain) {
const t = svg.transition().duration(750);
zx.domain(domain);
gx.transition(t).call(xAxis, zx);
path.transition(t).attr("d", line(data));
}
});
}
Insert cell
update = chart.update(timeframe)
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.upper)])
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = (g, scale = x) => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(scale).ticks(width / 80).tickSizeOuter(0))
Insert cell
yAxis = (g, scale = y) => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(scale).ticks(height / 40))
.call(g => g.select(".domain").remove())
Insert cell
data = d3.csvParse(await FileAttachment("aapl-bollinger.csv").text(), d3.autoType)
Insert cell
line = d3.line().x(d => x(d.date)).y(d => y(d.close))
Insert cell
Insert cell
Insert cell
Insert cell
import {Scrubber} from "@mbostock/scrubber"
Insert cell
import {ramp} from "@mbostock/color-ramp"
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