Published
Edited
Oct 12, 2020
Also listed in…
Election Explorations
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
eav
Insert cell
voteByCounty.get(fips)
Insert cell
fips
Insert cell
+eavsByCounty.get(fips)["A1a|2016"]
Insert cell
calculatePercent(countySVG.id)
Insert cell
eavsByCounty.get(countySVG.id)
Insert cell
projection = d3.geoAlbersUsa().fitSize([width - 10, height - 30], countyShapes)
Insert cell
minCounty = d3.min(countyShapes.features, d => calculatePercent(d.id))
Insert cell
diffScale = d3
.scaleSequential(d3.interpolateGreys)
// .domain(diffExtent)
.domain([minCounty, 1])
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
eavsByCounty = new Map(
allYearsFlat.map(d => {
let s = String(d.FIPSCode);
return [s.slice(0, s.length - 5).padStart(5, "0"), d];
})
)
Insert cell
statePctConfig = ({
min: simPct[0] * 100,
max: simPct[1] * 100
})
Insert cell
pctByState = {
let seed2 = "hi";
let chance = new Chance(seed2);
return new Map(
stateShapes.features.map(d => {
let s = String(d.FIPSCode);
return [d.id, chance.floating(statePctConfig)];
})
);
}
Insert cell
pctByCounty = {
let seed = "hi";
let chance = new Chance(seed);
return new Map(
allYearsFlat.map(d => {
let s = String(d.FIPSCode);
s = s.slice(0, s.length - 5).padStart(5, "0");
let statePct = pctByState.get(s.slice(0, 2));
return [
s,
chance.floating({
min: statePct - 10,
max: _.clamp(statePct + 10, 100)
})
];

return [s.slice(0, s.length - 5).padStart(5, "0"), statePct];
})
);
}
Insert cell
voteJitter = 5 // The amount added or subtracted from state percentage
Insert cell
Insert cell
voteByCounty = {
let seed = "hi";
let chance = new Chance(seed);
return new Map(
countyShapes.features.map(d => {
let fips = d.id;
let eave = eavsByCounty.get(fips);
if (!eave) return [fips, 0];

let countyActiveVoters = eave["A1a|2016"];
let statePct = pctByState.get(fips.slice(0, 2));

let countyPct = chance.floating({
min: statePct - voteJitter,
max: _.clamp(statePct + voteJitter, 100)
});

return [fips, Math.floor(countyActiveVoters * (countyPct / 100))];
})
);
}
Insert cell
import { legend } from "@d3/color-legend"
Insert cell
countySVG
Insert cell
fips = countySVG ? countySVG.id : ""
Insert cell
eav = eavsByCounty.get(fips)
Insert cell
eav ? eav : ""
Insert cell
height = 500
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,
stateShapes
} from "@enjalot/us-county-datasets"
Insert cell
d3 = require("d3@6")
Insert cell
FileAttachment("keyLables.json").json()
Insert cell
turf = require('https://bundle.run/turf@3.0.14')
Insert cell
import { twitch } from "@codingwithfire/twitch"
Insert cell
import { rangeSlider } from "@mootari/range-slider"
Insert cell
Chance = require("chance")
Insert cell
_ = require('lodash')
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