Published
Edited
Aug 27, 2020
2 forks
Importers
31 stars
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("stroke-miterlimit", 1);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

svg.append("g")
.call(grid);

svg.append("path")
.attr("fill", "steelblue")
.attr("fill-opacity", 0.2)
.attr("d", area(data));

svg.append("path")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("d", line(data.slice(0, observedIndex + 1)));

svg.append("path")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-dasharray", "3,3")
.attr("d", line(data.slice(observedIndex)));

svg.append("circle")
.attr("cx", x(observed.date))
.attr("cy", y(observed.mean))
.attr("r", 2.5);

svg.append("text")
.attr("x", x(observed.date))
.attr("y", y(observed.mean))
.attr("dx", 6)
.attr("dy", "0.35em")
.text(observed.mean.toLocaleString("en"));

svg.append("text")
.attr("x", x(observed.date))
.attr("y", y(observed.mean))
.attr("dx", 6)
.attr("dy", "1.35em")
.text(d3.utcFormat("%B %-d")(observed.date));

return svg.node();
}
Insert cell
data = Object.assign(d3.csvParse(await FileAttachment("covid-ihme-projected-deaths-2020-04-01.csv").text(), d3.autoType), {y: "↑ Deaths per day"})
Insert cell
observedIndex = data.findIndex(d => d.projected) - 1
Insert cell
observed = data[observedIndex]
Insert cell
line = d3.line()
.x(d => x(d.date))
.y(d => y(d.mean))
Insert cell
area = d3.area()
.x(d => x(d.date))
.y0(d => y(d.lower))
.y1(d => y(d.upper))
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data, d => d.date))
.rangeRound([margin.left, width - margin.right])
Insert cell
y = d3.scaleLog()
.domain([1, d3.max(data, d => d.upper)])
.rangeRound([height - margin.bottom, margin.top])
.clamp(true)
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80))
.call(g => g.select(".domain").remove())
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, ",d"))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(data.y))
Insert cell
grid = g => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right));
Insert cell
height = 600
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
d3 = require("d3@6")
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