Skip to content

Commit 89db4ac

Browse files
committed
election map example
1 parent 9c3abb3 commit 89db4ac

File tree

3 files changed

+3162
-0
lines changed

3 files changed

+3162
-0
lines changed

test/output/usPresidentialElectionMap2020.svg

Lines changed: 3132 additions & 0 deletions
Loading

test/plots/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ export {default as usPopulationStateAgeDots} from "./us-population-state-age-dot
244244
export {default as usPresidentFavorabilityDots} from "./us-president-favorability-dots.js";
245245
export {default as usPresidentGallery} from "./us-president-gallery.js";
246246
export {default as usPresidentialElection2020} from "./us-presidential-election-2020.js";
247+
export {default as usPresidentialElectionMap2020} from "./us-presidential-election-map-2020.js";
247248
export {default as usPresidentialForecast2016} from "./us-presidential-forecast-2016.js";
248249
export {default as usRetailSales} from "./us-retail-sales.js";
249250
export {default as usStateCapitals} from "./us-state-capitals.js";
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
import {feature, mesh} from "topojson-client";
4+
5+
export default async function () {
6+
const [[counties, statemesh], elections] = await Promise.all([
7+
d3.json("data/us-counties-10m.json").then((us) => [feature(us, us.objects.counties), mesh(us, us.objects.states)]),
8+
d3.csv("data/us-presidential-election-2020.csv")
9+
]);
10+
const centroids = new Map(counties.features.map((d) => [d.id, d3.geoCentroid(d)]));
11+
return Plot.plot({
12+
width: 960,
13+
height: 600,
14+
projection: "albers-usa",
15+
marks: [
16+
Plot.geo(statemesh),
17+
Plot.vector(elections, {
18+
filter: (d) => d.votes > 0,
19+
anchor: "start",
20+
x: (d) => centroids.get(d.fips)?.[0],
21+
y: (d) => centroids.get(d.fips)?.[1],
22+
sort: (d) => Math.abs(+d.results_trumpd - +d.results_bidenj),
23+
stroke: (d) => (+d.results_trumpd > +d.results_bidenj ? "red" : "blue"),
24+
length: (d) => Math.sqrt(Math.abs(+d.margin2020 * +d.votes)),
25+
rotate: (d) => (+d.results_bidenj < +d.results_trumpd ? 60 : -60)
26+
})
27+
]
28+
});
29+
}

0 commit comments

Comments
 (0)