Published
Edited
Apr 9, 2020
Insert cell
import {ramp} from "@mbostock/color-ramp"
Insert cell
import {Scrubber} from "@mbostock/scrubber"
Insert cell
d3 = require("d3@5",'d3-dsv')
Insert cell
// data = d3.csvParse(await FileAttachment("aapl-bollinger.csv").text(), d3.autoType)
Insert cell
data = d3.csv('https://gist.githubusercontent.com/KevinK88/5a487d25d23fdc7a34776ba74d71a361/raw/10a73861d1771bb086586ed3585e7a4daafe4d73/test.csv',d3.autoType)
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
height = 240

Insert cell
line = d3.line().x(d => x(d.date)).y(d => y(d.value))
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
xAxis = (g, scale = x) => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(scale).ticks(width / 80).tickSizeOuter(0))
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([height - margin.bottom, margin.top])
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
reveal = path => path.transition()
.duration(10000)
.ease(d3.easeLinear)
.attrTween("stroke-dasharray", function() {
const length = this.getTotalLength();
return d3.interpolate(`0,${length}`, `${length},${length}`);
})
Insert cell
replay2, html`<svg viewBox="0 0 ${width} ${height}">
${d3.select(svg`<path d="${line(data)}" fill="none" stroke="steelblue" stroke-width="1.5" stroke-miterlimit="1" stroke-dasharray="0,1"></path>`).call(reveal).node()}
${d3.select(svg`<g>`).call(xAxis).node()}
${d3.select(svg`<g>`).call(yAxis).node()}
</svg>`
Insert cell
{
replay2;
for (let i = 0, n = 300; i < n; ++i) {
yield i;
}
}
Insert cell
lineLength = svg`<path d="${line(data)}">`.getTotalLength()
Insert cell
viewof replay2 = html`<button>Replay`
Insert cell
{
replay2;

const path = svg`<path d="${line(data)}" fill="none" stroke="steelblue" stroke-width="1.5" stroke-miterlimit="1">`;

const chart = html`<svg viewBox="0 0 ${width} ${height}">
${path}
${d3.select(svg`<g>`).call(xAxis).node()}
${d3.select(svg`<g>`).call(yAxis).node()}
</svg>`;

for (let i = 0, n = 300; i < n; ++i) {
const t = (i + 1) / n;
path.setAttribute("stroke-dasharray", `${t * lineLength},${lineLength}`);
yield chart;
}
}
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