Published
Edited
Mar 13, 2020
6 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof currentCases = new View(2000)
Insert cell
viewof daysToDoubling = new View(6.0)
Insert cell
viewof percentageOfBedsOccupied = new View(65)
Insert cell
viewof percentageOfSevereCases = new View(10)
Insert cell
Insert cell
md`### Visualization Helpers

Much thanks to @mbostock, from https://observablehq.com/@d3/styled-axes`
Insert cell
resetFunction = () => {
viewof currentCases.value = 2000;
viewof daysToDoubling.value = 6;
viewof percentageOfBedsOccupied.value = 65;
viewof percentageOfSevereCases.value = 10;
}
Insert cell
casesTimeSeriesBisector = d3.bisector(d => d.cases).right
Insert cell
areaGenerator = d3.area()
.x(function(d) { return dateScale(d.date); })
.y1(function(d) { return casesScale(d.cases); })
.y0(casesScale(0));
Insert cell
xAxis = svg => svg
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(dateScale)
.ticks(d3.timeWeek.every(2))
.tickFormat(dateFormat))
.call(g => g.select(".domain")
.remove())
Insert cell
yAxis = svg => svg
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisRight(casesScale)
.tickSize(width - margin.left - margin.right))
.call(g => g.select(".domain")
.remove())
.call(g => g.selectAll(".tick:not(:first-of-type) line")
.attr("stroke-opacity", 0.5)
.attr("stroke-dasharray", "2,2"))
.call(g => g.selectAll(".tick text")
.attr("x", 4)
.attr("dy", -4))
Insert cell
height = 450
Insert cell
margin = ({top: 20, right: 0, bottom: 30, left: 0})
Insert cell
numberFormat = d3.format(",.4r")
Insert cell
largeNumberFormat = d3.formatPrefix(",.1", 1e6)
Insert cell
dateFormat = d3.timeFormat("%Y-%m-%d");
Insert cell
dateFormatInProse = d3.timeFormat("%b %e");
Insert cell
casesScale = d3.scaleLinear()
.domain(casesDomain)
.range([height - margin.bottom, margin.top])
Insert cell
dateScale = d3.scaleTime()
.domain(dateDomain)
.range([margin.left, width - margin.right])
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
Insert cell
Insert cell
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