Published
Edited
Jul 21, 2020
Insert cell
Insert cell
dataset = d3.csvParse(await FileAttachment("aapl-bollinger.csv").text(), d3.autoType)
Insert cell
horzScale = d3.scaleUtc()
.domain(d3.extent(dataset, (d)=>d.date))
.range([margin.left, width - margin.right])
Insert cell
vertScale = d3.scaleLinear()
.domain([0, d3.max(dataset, (d)=>d.upper)])
.range([height - margin.bottom, margin.top])
Insert cell
Area = d3.area()
.x((d)=>horzScale(d.date))
.y0(vertScale(0))
.y1((d)=>vertScale(d.close))
Insert cell
horzAxis = (g)=>g.attr("transform", `translate(0, ${height - margin.bottom})`)
.call(d3.axisBottom(horzScale))
Insert cell
vertAxis = (g)=>g.attr("transform", `translate(${margin.left}, 0)`)
.call(d3.axisLeft(vertScale).ticks(10))
.call((g)=>g.select(".domain").remove())
Insert cell
html`<svg width="${width}" height="${height}">
<g fill="steelblue">
<path d="${Area(dataset)}"/>
</g>
${d3.select(svg`<g>`).call(horzAxis).node()}
${d3.select(svg`<g>`).call(vertAxis).node()}
</svg>`
Insert cell
Area2 = d3.area()
.x((d)=>horzScale(d.date))
.y0((d)=>vertScale(d.lower))
.y1((d)=>vertScale(d.upper))
Insert cell
html`<svg width="${width}" height="${height}">
<g>
<path d="${Area2(dataset)}" fill="#eeee"/>
<path d="${Area2.lineY0()(dataset)}" stroke="green" stroke-width="1" fill="none"/>
<path d="${Area2.lineY1()(dataset)}" stroke="red" stroke-width="1" fill="none"/>
</g>
${d3.select(svg`<g>`).call(horzAxis).node()}
${d3.select(svg`<g>`).call(vertAxis).node()}
</svg>`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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