Published
Edited
Oct 6, 2020
4 stars
Also listed in…
Election Explorations
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
countySVG
Insert cell
projection = d3.geoAlbersUsa().fitSize([width - 20, height - 30], countyShapes)
Insert cell
rPopScale = d3
.scaleSqrt()
.domain(populationExtent)
.range([1, 22])
Insert cell
diffScale = {
let max = Math.max(Math.abs(diffExtent[0]), diffExtent[1]);
return d3
.scaleDiverging()
.domain([-max, 0, max])
.range([-500, 0, 500]);
}
Insert cell
diffOpacity = {
let max = Math.max(Math.abs(diffExtent[0]), diffExtent[1]);
return d =>
d3
.scaleDiverging()
.domain([-max, 0, max])
.range([.6, 0, .6])(d) + 0.25;
}
Insert cell
diffColor = {
return d3
.scaleDiverging()
.domain([diffExtent[0], 0, diffExtent[1]])
.range(["orange", "white", "teal"]);
}
Insert cell
// diff = diffRegister
diff = diffPopulation
Insert cell
diffRegister = d => {
let c = eavsByCounty.get(d.id)
if(!c) return 0;
return +c["A1a|2018"] - +c["A1a|2016"];
}
Insert cell
diffPopulation = d => {
let d2018 = demographicsData[2018].get(d.id);
let d2016 = demographicsData[2016].get(d.id);
if (!d2018 || !d2018) return 0;
return +d2018["B01003_001E"] - +d2016["B01003_001E"];
}
Insert cell
turf.centroid(countyShapes.features[0])
Insert cell
center = d => {
return projection(turf.centroid(d).geometry.coordinates) || [0, 0];
}
Insert cell
allYearsFlat = FileAttachment("allYearsFlat@1.csv").csv({ typed: false })
Insert cell
allYearsFlat.filter(d => d.State_Full == "WISCONSIN")
Insert cell
allYearsFlat.filter(d => diff(d) < -1000000)
Insert cell
populationExtent = d3.extent(countyShapes.features, d => {
let c = demographicsData[2018].get(d.id);
if (c) return c["B01003_001E"];
return 0;
})
Insert cell
circles = {
let localNodes = countyShapes.features
.map(d => {
let e = eavsByCounty.get(d.id);
let c = center(d);
let demo = demographicsData[2018].get(d.id);
let pop = demo ? demo["B01003_001E"] : 0;
let o = {
...d,
cx: c[0],
cy: c[1],
x: c[0],
y: c[1]
};
if (e) {
o.p = rPopScale(pop);
o.value = diff(e);
}
return o;
})
.filter(d => !isNaN(d.p));
// console.log(localNodes);

let simulation = d3
.forceSimulation(localNodes)
.force(
"collide",
d3
.forceCollide()
// .radius(d => d.p) // for spacing by population
.radius(d => d.p + 1) // for spacing by metric
.strength(1)
)
.force(
"x",
d3.forceX(d => {
return d.cx;
})
)
.force(
"y",
d3.forceY(d => {
return d.cy;
})
);

simulation.tick(100);

localNodes.sort((a, b) => a.metric - b.metric);
return localNodes;
}
Insert cell
eavsByCounty = new Map(
allYearsFlat.map(d => {
let s = String(d.FIPSCode);
return [s.slice(0, s.length - 5).padStart(5, "0"), d];
})
)
Insert cell
diffExtent = d3.extent(countyShapes.features, diff)
Insert cell
d3.min(countyShapes.features, diff)
Insert cell
import { legend } from "@d3/color-legend"
Insert cell
fips = countySVG ? countySVG.id : ""
Insert cell
eav = eavsByCounty.get(fips)
Insert cell
eav ? diff(countySVG) : ""
Insert cell
height = 700
Insert cell
pop2016 = demographicsData[2016].get(fips)["B01003_001E"]
Insert cell
pop2018 = demographicsData[2018].get(fips)["B01003_001E"]
Insert cell
format = d3.format(",d")
Insert cell
import {
demographicsData,
selectedFields,
niceLabel,
countyShapes
} from "@enjalot/us-county-datasets"
Insert cell
d3 = require("d3@6")
Insert cell
(d3.selection.prototype.moveToFront = function() {
return this.each(function() {
this.parentNode.appendChild(this);
});
})
Insert cell
FileAttachment("keyLables.json").json()
Insert cell
turf = require('https://bundle.run/turf@3.0.14')
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