Public
Edited
Oct 26, 2023
16 forks
Importers
47 stars
Insert cell
Insert cell
Insert cell
Insert cell
data = FileAttachment("aapl-bollinger.csv").csv({typed: true})
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
Insert cell
{
let path = `M${x(data[0].date)},${y(data[0].close)}`;
for (let i = 1; i < data.length; ++i) {
path += `L${x(data[i].date)},${y(data[i].close)}`;
}
return path;
}
Insert cell
Insert cell
line = d3.line()
.x(d => x(d.date))
.y(d => y(d.close))
Insert cell
Insert cell
line(data)
Insert cell
htl.html`<svg viewBox="0 0 ${width} ${height}">
<path d="${line(data)}" fill="none" stroke="steelblue" stroke-width="1.5" stroke-miterlimit="1"></path>
${d3.select(htl.svg`<g>`).call(xAxis).node()}
${d3.select(htl.svg`<g>`).call(yAxis).node()}
</svg>`
Insert cell
Insert cell
Insert cell
Insert cell
area = d3.area()
.x(d => x(d.date))
.y0(y(0))
.y1(d => y(d.close))
Insert cell
htl.html`<svg viewBox="0 0 ${width} ${height}">
<path fill="steelblue" d="${area(data)}"></path>
${d3.select(htl.svg`<g>`).call(xAxis).node()}
${d3.select(htl.svg`<g>`).call(yAxis).node()}
</svg>`
Insert cell
Insert cell
areaBand = d3.area()
.x(d => x(d.date))
.y0(d => y(d.lower))
.y1(d => y(d.upper))
Insert cell
htl.html`<svg viewBox="0 0 ${width} ${height}">
<path d="${areaBand(data)}" fill="steelblue"></path>
${d3.select(htl.svg`<g>`).call(xAxis).node()}
${d3.select(htl.svg`<g>`).call(yAxis).node()}
</svg>`
Insert cell
Insert cell
lineMiddle = d3.line()
.x(d => x(d.date))
.y(d => y(d.middle))
Insert cell
htl.html`<svg viewBox="0 0 ${width} ${height}">
<path d="${areaBand(data)}" fill="#ddd"></path>
<g fill="none" stroke-width="1.5" stroke-miterlimit="1">
<path d="${lineMiddle(data)}" stroke="#00f"></path>
<path d="${line(data)}" stroke="#000"></path>
</g>
${d3.select(htl.svg`<g>`).call(xAxis).node()}
${d3.select(htl.svg`<g>`).call(yAxis).node()}
</svg>`
Insert cell
Insert cell
htl.html`<svg viewBox="0 0 ${width} ${height}">
<path d="${areaBand(data)}" fill="#ddd"></path>
<g fill="none" stroke-width="1.5" stroke-miterlimit="1">
<path d="${areaBand.lineY0()(data)}" stroke="#00f"></path>
<path d="${areaBand.lineY1()(data)}" stroke="#f00"></path>
</g>
${d3.select(htl.svg`<g>`).call(xAxis).node()}
${d3.select(htl.svg`<g>`).call(yAxis).node()}
</svg>`
Insert cell
Insert cell
Insert cell
arc = d3.arc()
.innerRadius(210)
.outerRadius(310)
.startAngle(([startAngle, endAngle]) => startAngle)
.endAngle(([startAngle, endAngle]) => endAngle)
Insert cell
Insert cell
arc([Math.PI / 2, Math.PI]) // from 90° to 180°
Insert cell
Insert cell
htl.html`<svg viewBox="-320 -320 640 640" style="max-width: 640px;">
${Array.from({length: n}, (_, i) => htl.svg`<path stroke="black" fill="${d3.interpolateRainbow(i / n)}" d="${arc([i / n * 2 * Math.PI, (i + 1) / n * 2 * Math.PI])}"></path>`)}
</svg>`
Insert cell
Insert cell
fruits = [
{name: "🍊", count: 21},
{name: "🍇", count: 13},
{name: "🍏", count: 8},
{name: "🍌", count: 5},
{name: "🍐", count: 3},
{name: "🍋", count: 2},
{name: "🍎", count: 1},
{name: "🍉", count: 1}
]
Insert cell
Insert cell
pieArcData = d3.pie()
.value(d => d.count)
(fruits)
Insert cell
Insert cell
arcPie = d3.arc()
.innerRadius(210)
.outerRadius(310)
.padRadius(300)
.padAngle(2 / 300)
.cornerRadius(8)
Insert cell
htl.html`<svg viewBox="-320 -320 640 640" style="max-width: 640px;" text-anchor="middle" font-family="sans-serif">
${pieArcData.map(d => htl.svg`
<path fill="steelblue" d="${arcPie(d)}"></path>
<text fill="white" transform="translate(${arcPie.centroid(d).join(",")})">
<tspan x="0" font-size="24">${d.data.name}</tspan>
<tspan x="0" font-size="12" dy="1.3em">${d.value.toLocaleString("en")}</tspan>
</text>
`)}
</svg>`
Insert cell
Insert cell
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(height / 40))
.call(g => g.select(".domain").remove())
Insert cell
height = 240
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more