Published
Edited
May 11, 2020
6 forks
Importers
29 stars
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, 975, 610]);

svg.append("g")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.join("path")
.attr("fill", d => color(data.get(d.id)))
.attr("d", path)
.append("title")
.text(d => `${d.properties.name}, ${states.get(d.id.slice(0, 2)).name}
${format(data.get(d.id))}`);

svg.append("path")
.datum(topojson.mesh(us, us.objects.states, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "#ccc")
.attr("stroke-linejoin", "round")
.attr("d", path);

return svg.node();
}
Insert cell
alldata = d3.csvParse(await FileAttachment("ACSDT5Y2018.B04006_data_with_overlays_2020-05-08T171725.csv").text())
Insert cell
total = "B04006_001E"
Insert cell
variables = new Map(Object.entries(alldata[0])
.filter(([GEO_ID]) => /^B\d+_\d+E$/.test(GEO_ID) && GEO_ID !== total)
.map(([GEO_ID, id]) => [GEO_ID, {id: GEO_ID, name: id.split(/!!/g).pop(), total: d3.sum(alldata.slice(1), d => d[GEO_ID])}])
.sort(([, {total: a}], [, {total: b}]) => d3.descending(a, b)))
Insert cell
data = new Map(alldata.slice(1).map(d => [d.GEO_ID.slice(-5), +d[variable.id] / +d[total]]))
Insert cell
color = d3.scaleSequential([0, d3.max(data.values())], d3.interpolateYlGnBu)
Insert cell
path = d3.geoPath()
Insert cell
format = color.tickFormat(5, "%")
Insert cell
states = new Map(us.objects.states.geometries.map(d => [d.id, d.properties]))
Insert cell
us = FileAttachment("counties-albers-10m.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@5", "d3-array@2")
Insert cell
import {legend} from "@d3/color-legend"
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