Published
Edited
Jul 21, 2020
Insert cell
Insert cell
data = d3.csvParse(await FileAttachment("aapl-bollinger.csv").text(), d3.autoType)
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
line = d3.line().x((d)=>x(d.date)).y((d)=>y(d.close))
Insert cell
lineMiddle = d3.line().x((d)=>x(d.date)).y((d)=>y(d.middle))
Insert cell
line(data)
Insert cell
Insert cell
html`<svg width="${width}" height="${height}">
<g>
<path d="${line(data)}" stroke="steelblue" stroke-width="1.5" stroke-miterlimit="1" fill="none"/>
<path d="${lineMiddle(data)}" stroke="grey" stroke-width="1.5" stroke-miterlimit="1" fill="none"/>
</g>
${d3.select(svg`<g transform="translate(${margin.left}, 0)">`)
.call(d3.axisLeft(y))
.call((g)=>g.select(".domain").remove()).node()}
${d3.select(svg`<g transform="translate(0, ${height-margin.bottom})">`)
.call(d3.axisBottom(x)).node()}
</svg>`
Insert cell
Insert cell
show = (path)=>path
.transition()
.duration(5000)
.ease(d3.easeCubicInOut)
.attrTween("stroke-dasharray", function(){
let length = this.getTotalLength();
return d3.interpolate(`0,${length}`, `${length},0`);
})
Insert cell
ticks = d3.ticks(0, 1, 200)
Insert cell
match = d3.interpolate("one, two", "1, 2")
Insert cell
viewof replay = html`<button>Replay`
Insert cell
replay, html`<svg width="${width}" height="${height}">
<g>
${d3.select(svg`<path d="${line(data)}" stroke="steelblue" stroke-width="1.5" stroke-miterlimit="1" fill="none"/>`)
.call(show).node()}
</g>
${d3.select(svg`<g transform="translate(${margin.left}, 0)">`)
.call(d3.axisLeft(y))
.call((g)=>g.select(".domain").remove()).node()}
${d3.select(svg`<g transform="translate(0, ${height-margin.bottom})">`)
.call(d3.axisBottom(x)).node()}
</svg>`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
margin = ({top:10, right:10, bottom:40, left:40})
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