Public
Edited
Nov 6, 2024
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
population2radius = d3.scaleSqrt().domain([0, 2e9]).range([0, 680])
Insert cell
counties = origCounties.map((county) => ({
...county,
properties: {
...county.properties,
radius: population2radius(votes.get(county.id)?.count?.total ?? 0)
}
}))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
/*counties = topojson
.feature(us, us.objects.counties)
.features.map(county => {
const { population } = populations.find(p => p.id === county.id);

const state = us.objects.states.geometries.find(
state => state.id === county.properties.STATEFP
);

const name = `${county.properties.NAME} County, ${state.properties.name}`;

return {
...county,
properties: {
name,
state: state.properties.name,
population,
density: (population / county.properties.ALAND) * 1e6,
centroid: projection(
turf.centroid(county.geometry).geometry.coordinates
),
radius: radiusScale(population)
}
};
})
.filter(c => c.properties.centroid)
//.filter(c => c.geometry.type === "MultiPolygon")
.sort((a, b) =>
a.properties.centroid[0] < b.properties.centroid[0] ? -1 : 1
)
.map((d, i) => {
let geometry;
if (d.geometry.type !== "MultiPolygon") {
geometry = d.geometry;
} else {
geometry = {
type: d.geometry.type,
coordinates: d.geometry.coordinates
.sort((a, b) =>
turf.area(turf.polygon(a)) > turf.area(turf.polygon(b)) ? -1 : 1
)
.slice(0, 1)
};
}
return {
...d,
rank: i,
geometry
};
})*/
Insert cell

/*DOM.download(
new Blob([JSON.stringify(spreadCounties)], {
type: "application/json"
}),
"countiesPositioned.json"
)*/
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
votes = {
const nyTimesRes = await (
await fetch(
"https://static01.nyt.com/elections-assets/pages/data/feeds/96c3e55a-4c13-4585-b5f6-78f9de0ec96e/pres-county-data/2024-11-05.json"
)
).json();

return new Map(
_.flatten(
nyTimesRes["data"].map((state) =>
state["reporting_units"]
.filter((reportingUnit) => reportingUnit.fips_county)
.map((reportingUnit) => ({
id: `${reportingUnit.fips_state}${reportingUnit.fips_county}${
reportingUnit.fips_suffix ?? ""
}`,
count: {
total: reportingUnit.total_votes,
dem:
reportingUnit.candidates.find(
(candidate) => candidate.nyt_id == "harris-k"
)?.votes?.total ?? 0,
gop:
reportingUnit.candidates.find(
(candidate) => candidate.nyt_id == "trump-d"
)?.votes?.total ?? 0
}
}))
.map((result) => [
result.id,
{
...result,
percent: {
dem: result.count.dem / result.count.total,
gop: result.count.gop / result.count.total
},
two_party_ratio:
result.count.dem / (result.count.dem + result.count.gop)
}
])
)
)
);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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