Published
Edited
Aug 9, 2020
22 stars
Also listed in…
Season 1
County Maps
Insert cell
Insert cell
Insert cell
Insert cell
showCountyLines = false
Insert cell
Insert cell
Insert cell
map = makeMap(nationShape)
Insert cell
ny = makeMap(getState("New York"))
Insert cell
fl = makeMap(getState("Florida"))
Insert cell
ca = makeMap(getState("California"))
Insert cell
tx = makeMap(getState("Texas"))
Insert cell
function makeMap(zoomShape) {
let ctx = DOM.context2d(mapWidth, mapHeight);
// ctx.canvas.width = mapWidth; // * window.devicePixelRatio;
// ctx.canvas.height = mapHeight; // * window.devicePixelRatio;
ctx.canvas.width = mapWidth;
ctx.canvas.height = mapHeight;
ctx.canvas.style.width = mapWidth + "px";
ctx.canvas.style.height = mapHeight + "px";

let projection = d3
.geoAlbersUsa()
.fitSize([mapWidth - 50, mapHeight - 50], zoomShape);
let path = d3.geoPath(projection).context(ctx);

ctx.fillStyle = "white";
ctx.fillRect(0, 0, mapWidth, mapHeight);

// let r = 0.5;
let r = 1;

ctx.strokeStyle = "#111";
ctx.lineWidth = 2;
ctx.globalAlpha = 1;

ctx.beginPath();
path(zoomShape);
ctx.stroke();

shapesWithData.forEach(d => {
ctx.strokeStyle = "#ddd";
ctx.lineWidth = .5;
ctx.globalAlpha = 0;
if (d.nyt) ctx.globalAlpha = 1;

if (showCountyLines) {
ctx.beginPath();
path(d);
ctx.stroke();
}

const fillColor = d3.color(colorScale(colorScale.domain()[1]));
fillColor.opacity = 0.25;
ctx.fillStyle = fillColor.toString();

if (d.points && d.points.features) {
d.points.features.forEach(feature => {
// const {
// properties: { color }
// } = feature;

const {
geometry: {
coordinates: [x, y]
}
} = feature;

let c = projection([x, y]);
if (!c) return;
// console.log({ x, y, feature });
// ctx.fillStyle = color;
ctx.fillRect(c[0], c[1], r, r);
});
}
});

ctx.fillStyle = "#111";
ctx.beginPath();
ctx.font = '24px Helvetica';
ctx.fillText(normalDate(date), mapWidth / 2 - 40, 35);
ctx.fill();

return ctx.canvas;
}
Insert cell
nytDataWithRollingAverages = {
const byCounty = Array.from(d3.group(rawData, d => d.county));
const byCountyCoerced = byCounty.map(d => {
const [name, values] = [d[0], d[1]];
return { name, values };
});

return byCountyCoerced;
}
Insert cell
nytDots = d3.group(rawData, d => d.date)
Insert cell
nytDots.get(normalDate(date))
Insert cell
nytCounties = d3.group(nytDots.get(normalDate(date)), d => d.fips)
Insert cell
Insert cell
Insert cell
// projection = d3.geoAlbersUsa().fitSize([mapWidth, mapHeight], nationShape)
// .translate([mapWidth / 2, mapHeight / 2])
// .scale([mapWidth])
Insert cell
import { rawData, data as nytdata } with {
nytStart as startDate
} from "@jashkenas/united-states-coronavirus-daily-cases-map-covid-19"
Insert cell
nytStart = normalDate(start_date)
Insert cell
nytdata
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
legend({
color: colorScale,
title: "Total Cumulative Cases",
width: width
})
Insert cell
Insert cell
extent = d3.extent(rawData, d => +d.cases)
Insert cell
Insert cell
Insert cell
// data = fetchData(signalMap[signal], date, fips)
Insert cell
// metrics = new Map(data.epidata.map(d => [d.geo_value, d.value]))
Insert cell
// reduceMetrics = data.epidata.reduce((m, d) => {
// m[d.geo_value] = d.value;
// return m;
// }, {})
Insert cell
Insert cell
// metricExtent = d3.extent(Array.from(metrics), d => d[1])
Insert cell
dotScale = d3
.scaleLinear()
// .domain(metricExtent)
.domain([0, 5000])
.range([0, 500])
Insert cell
// rScale = d3
// .scaleSqrt()
// .domain(metricExtent)
// .range([0.1, 12])
Insert cell
// popExtent = d3.extent(shapesWithData, s => {
// return s.population;
// })
Insert cell
// rPopScale = d3
// .scaleSqrt()
// .domain(popExtent)
// .range([0.1, 12])
Insert cell
// rPopScale(762)
Insert cell
Insert cell
Insert cell
Insert cell
usShapes = d3.json("https://cdn.jsdelivr.net/npm/us-atlas@3/counties-10m.json")
Insert cell
countyShapes = topojson
.feature(usShapes, usShapes.objects.counties)
.features.sort((a, b) => +a.id - +b.id)
Insert cell
nationShape = topojson.feature(usShapes, usShapes.objects.nation).features[0]
Insert cell
stateShapes = topojson
.feature(usShapes, usShapes.objects.states)
.features.sort((a, b) => +a.id - +b.id)
Insert cell
getState("New York")
Insert cell
getState = function(state) {
return stateShapes.filter(
d => d.properties.name.toLowerCase() == state.toLowerCase()
)[0];
}
Insert cell
shapesWithData = countyShapes.map(s => {
let nyt = nytCounties.get(s.id) || [];
const nextData = {
...s,
population: pops.get(s.id),
// metric: metrics.get(s.id),
centroid: turf.centroid(s),
nyt: nyt[0]
};

nextData.points = generateDensity(nextData);
return nextData;
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
moment = require('moment')
Insert cell
Insert cell
import { slider, checkbox, radio, color } from "@jashkenas/inputs"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more