Published
Edited
Sep 9, 2020
3 stars
Also listed in…
Bitemporal
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart.update(line)
Insert cell
height = width / 2
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
data = Object.assign(covid, { y: "U.S. cumulative positive case count" })
Insert cell
color = d3
.scaleSequential()
.interpolator(d3.interpolateRainbow)
.domain(x.domain())
Insert cell
format = d => (d < 100 ? d : d3.format(".2s")(d))
Insert cell
yAxis = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(
d3
.axisLeft(y)
.tickFormat(format)
.ticks(5)
)
.call(g => g.select(".domain").remove())
.call(g =>
g
.select(".tick:last-of-type text")
.clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y)
)
Insert cell
xAxis = g =>
g.attr("transform", `translate(0,${height - margin.bottom})`).call(
d3
.axisBottom(x)
.ticks(width / 80)
.tickSizeOuter(0)
)
Insert cell
xAxisLeft = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(
d3
.axisLeft(
xOn
)
.ticks(width / 80)
.tickSizeOuter(0)
)
.call(g =>
g
.append("text")
.attr("x", 4)
.attr("y", x(d3.max(data.map(d => d.on))) - 4)
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.attr("fill", "black")
.text("On this day we thought")
.call(halo)
)
Insert cell
xAxisTop = g =>
g
.attr("transform", `translate(0,${margin.top})`)
.call(
d3
.axisTop(x)
.ticks(width / 80)
.tickSizeOuter(0)
)
.call(g =>
g
.append("text")
.attr("x", width - margin.right)
.attr("y", 12)
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.attr("fill", "black")
.text("There were this many cases on this day")
.call(halo)
)
Insert cell
y = d3
.scaleLog()
.domain([1, d3.max(data, d => d.value)])
// .nice()
.range([height - margin.bottom, margin.top])
Insert cell
x = d3
.scaleUtc()
.domain(d3.extent([...data.map(d => d.on), ...data.map(d => d.about)]))
.range([margin.left, width - margin.right])
Insert cell
xOn = d3
.scaleUtc()
.domain(d3.extent(data.map(d => d.on)))
.range([margin.left, x(d3.max(data.map(d => d.on)))])
.nice()
Insert cell
options = [
{
label: html`Lines connect case counts estimated on the same date; x-axis shows the date they’re about.`,
value: { name: "on-over-about", line: lineAbout }
},
{
label: html`Lines connect case count estimates about the same date; x-axis shows date they’re made on.`,
value: { name: "about-over-on", line: lineOn }
}
]
Insert cell
lineOn = d3
.line()
.defined(d => !isNaN(d.value))
.x(d => x(d.on))
.y(d => y(d.value))
Insert cell
lineAbout = d3
.line()
.defined(d => !isNaN(d.value))
.x(d => x(d.about))
.y(d => y(d.value))
Insert cell
line2D = d3
.line()
.defined(d => !isNaN(d.value))
.x(d => x(d.about))
.y(d => xOn(d.on))
Insert cell
covid = d3
.csvParse(
await FileAttachment("covid-combined@1.csv").text(),
({ on, about, positive }) => ({
on: new Date(on),
about: new Date(about),
value: +positive
})
)
.sort((a, b) => (+a.on === +b.on ? +a.about - b.about : +a.on - b.on))
Insert cell
d3 = require("d3@6")
Insert cell
dataByAbout = Array.from(d3.group(data, ({ about }) => +about).values())
Insert cell
dataByOn = Array.from(d3.group(data, ({ on }) => +on).values())
Insert cell
dataActual = data.filter(({ on, about }) => +on === +about)
Insert cell
function halo(text) {
text
.select(function() {
return this.parentNode.insertBefore(this.cloneNode(true), this);
})
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-width", 4)
.attr("stroke-linejoin", "round");
}
Insert cell
import { html, svg } from "@observablehq/htl"
Insert cell
import { resample } from "@tophtucker/scrapbook"
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