Published
Edited
Jul 15, 2020
Importers
42 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const c = `rgb(255, 255, 255, 0.5)`;
return html`<style>
form output {
font-weight: bold;
font-size: 14px;
}
.wrapper {
text-align: center;
}
.map {
text-anchor: middle;
font-family: sans-serif;
font-size: 10px;
margin: 0 auto;
}
.subunit {
fill: #f4f4f4;
stroke: #999;
stroke-width: 0.5;
}
.place {
fill: rgba(0,0,0,0.8);
stroke: none;
pointer-events: none;
}
.place-label {
pointer-events: none;
text-shadow: ${c} 1px 0px 0px, ${c} 0.540302px 0.841471px 0px, ${c} -0.416147px 0.909297px 0px, ${c} -0.989992px 0.14112px 0px, ${c} -0.653644px -0.756802px 0px, ${c} 0.283662px -0.958924px 0px, ${c} 0.96017px -0.279415px 0px;
}
.place-label, .legend-title {
font-weight: bold;
font-size: 12px;
fill: rgba(0,0,0,0.8);
}
.bubble, .legend-bubble {
stroke-width: 0.8;
stroke: rgba(0,0,0,0.3)
}
.bubble:hover {
stroke: rgba(0,0,0,0.6);
stroke-width: 1.2;
cursor: crosshair;
}
.legend text {
fill: #000;
}
.legend-bubble {
stroke: rgba(0,0,0,0.4);
}
.legend-title {
text-anchor: start;
}
</style>`;
}
Insert cell
legendRadii = [10, 1000, 10000, 50000]
Insert cell
function placeName(d) {
if (d.county === "New York City") return "New York City";
const loc = getLocation(d);
return `${loc.properties.name} ${
d.state === "Louisiana" ? "Parish" : "County"
}`;
}
Insert cell
function getLocation(d) {
let location = countyMap.get(d.fips);
if (!location && d.county === "New York City")
location = countyMap.get("36061");
if (!location) location = stateMap.get(d.state);
if (!location) console.warn("No location found for: " + JSON.stringify(d));
return location;
}
Insert cell
map.update(index)
Insert cell
w = 975
Insert cell
h = 610
Insert cell
albersProjection = {
// XXX super hacky
const proj = d3.geoAlbersUsa().scale(1300);
return function(coords) {
const [x, y] = proj(coords);
return [x + 6, y + 54];
};
}
Insert cell
magnitude = toMagnitude(maxCases)
Insert cell
countyMap = new Map(
topojson.feature(us, us.objects.counties).features.map(d => [d.id, d])
)
Insert cell
stateMap = new Map(
topojson
.feature(us, us.objects.states)
.features.map(d => [d.properties.name, d])
)
Insert cell
function toMagnitude(n) {
var order = Math.floor(Math.log(n) / Math.LN10 + 0.000000001);
return Math.pow(10, order);
}
Insert cell
mutable index = 0
Insert cell
{
mutable index = dates.indexOf(day);
}
Insert cell
radius = d3
.scaleSqrt()
.domain([0, maxCases])
.range([0, maxRadius])
Insert cell
colorScale = d3
.scaleSqrt()
.domain([0, maxCases])
.range([`hsla(57, 100%, 50%, 0.36)`, `hsla(7, 100%, 50%, 0.57)`])
Insert cell
delay = 250
Insert cell
maxCases = d3.max(recentData.map(d => +d.cases))
Insert cell
excludeTerritories = new Set(["Guam", "Puerto Rico", "Virgin Islands"])
Insert cell
data = d3
.nest()
.key(d => d.date)
.entries(rawData.filter(d => !excludeTerritories.has(d.state)))
.map(d => d.values)
.filter(d => d[0].date >= startDate)
Insert cell
rawData = d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv")
Insert cell
path = d3.geoPath()
Insert cell
maxRadius = 50
Insert cell
recentData = data[data.length - 1]
Insert cell
startDate = "2020-03-01"
Insert cell
dates = data.map(d => d[0].date).map(d => new Date(`${d}T20:00Z`))
Insert cell
places = d3.csvParse(await FileAttachment("us-places.csv").text())
Insert cell
sFormat = d3.format(".1s")
Insert cell
numFormat = d3.format("~s")
Insert cell
us = FileAttachment("counties-albers-10m.json").json()
Insert cell
import { Scrubber } from "@mbostock/scrubber"
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@5")
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