Published
Edited
May 16, 2020
1 star
Insert cell
md`# United States COVID-19 Cases in 2020`
Insert cell
d3 = require("d3@5")
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
simple = require("simple-statistics@7.0.7/dist/simple-statistics.min.js")
Insert cell
format = d => `${d}%`
Insert cell
topojson = require("topojson-client@3")
Insert cell
FINALCASES = FileAttachment("FINALCASES.json").json()
Insert cell
counties = topojson.feature(FINALCASES, FINALCASES.objects.FINALCASES)
Insert cell
csv_data = d3.csvParse(await FileAttachment("FINALCASES.csv").text(),({FIPS, USETotalCa, POP2010}) => [+FIPS, [+USETotalCa, +USETotalCa/+POP2010]])
Insert cell
data = Object.assign(new Map(csv_data), {title: "Total US Cases"})
Insert cell
USETotalCa = Array.from(csv_data.values(), d => d[1][1])
Insert cell
Purples = [d3.color("#f2f0f7"), d3.color("#cbc9e2"), d3.color("#9e9ac8"), d3.color("#756bb1"),d3.color("#54278f")]
Insert cell
naturalbreaks = simple.ckmeans(USETotalCa, Purples.length).map(v => v.pop())
Insert cell
//more information on sequential scales: https://observablehq.com/@d3/sequential-scales
// color = d3.scaleSequentialQuantile([...data.values()], d3.interpolateBlues)

// color = d3.scaleQuantile()
// .domain(med_age)
// .range()

color = d3.scaleThreshold()
.domain(naturalbreaks)
.range(Purples)
Insert cell
width = 1200
Insert cell
height = 1000

Insert cell
//Rotate the map sets the longitude of origin for our Albers projection.
// projection = d3.geoTransverseMercator()
// .rotate([98,0])
// .fitExtent([[80, 80], [width, height]], counties);
//d3 reference for projections: https://github.com/d3/d3-geo/blob/master/README.md
projection = d3.geoAlbers().fitExtent([[margin, margin], [width - margin, height - margin]], counties)
//use the following url for specific projection settings: https://github.com/veltman/d3-stateplane
//Use this code to set up the map projection (if different than geographic projection)
//margin = 100
//projection = d3.geoMercator().fitExtent([[margin, margin], [width - margin, height - margin]], counties)
Insert cell
margin = 100
Insert cell
//Using a path generator to project geometry onto the map
path = d3.geoPath().projection(projection);
Insert cell
choropleth = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.attr("transform", "translate(300,20)")
.append(() =>
legend({
color: color,
title: data.title,
width: 260,
tickFormat: ".1%"
})
);

svg.append("g")
.selectAll("path")
.data(counties.features)
.join("path")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("fill", d => color(data.get(+d.properties.FIPS)[1]))
.attr("d", path)
.append("title")
.text(d => " Percent of Total Cases " + data.get(+d.properties.FIPS)[1]);

return svg.node();
}
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