Public
Edited
Nov 2, 2022
1 star
Also listed in…
Political Issues
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
bar_data = {
let bar_data =
chamber == "House" ? map_data.house_districts : map_data.senate_districts;
bar_data = bar_data.features.map((o) => o.properties);
bar_data.forEach(
(o) =>
(o.competitive_status =
o.dem_vote_share < 0.5 - threshold
? "Republican"
: o.dem_vote_share > 0.5 + threshold
? "Democratic"
: "Competitive")
);
bar_data = d3.sort(bar_data, (o) => o.dem_vote_share);
bar_data.forEach((o, i) => (o.rank = i));
// d3.sort(
// bar_data.filter((o) => o.cometitive_status == "Democratic"),
// (o) => o.dem_vote_share
// ).forEach(function (o, i) {
// let oo = bar_data.filter((ooo) => ooo.DISTRICT == o.DISTRICT)[0];
// oo.rank = i;
// });
return d3.sort(bar_data, (o) => o.DISTRICT);
}
Insert cell
Insert cell
house_counts = {
let total = map_data.house_districts.features.length;
let rep_count = map_data.house_districts.features.filter(
(o) => o.properties.dem_vote_share < 0.5 - threshold
).length;
let dem_count = map_data.house_districts.features.filter(
(o) => o.properties.dem_vote_share > 0.5 + threshold
).length;

let toss_ups = map_data.house_districts.features.filter(
(o) =>
0.5 - threshold < o.properties.dem_vote_share &&
o.properties.dem_vote_share < 0.5 + threshold
);
let dem_vote_share_extent = d3.extent(
map_data.house_districts.features.map((o) => o.properties.dem_vote_share)
);

return {
total,
rep_count,
dem_count,
dem_vote_share_extent,
toss_ups,
toss_up_count: toss_ups.length
};
}
Insert cell
senate_counts = {
let total = map_data.senate_districts.features.length;
let rep_count = map_data.senate_districts.features.filter(
(o) => o.properties.dem_vote_share < 0.5 - threshold
).length;
let dem_count = map_data.senate_districts.features.filter(
(o) => o.properties.dem_vote_share > 0.5 + threshold
).length;

let toss_ups = map_data.senate_districts.features.filter(
(o) =>
0.5 - threshold < o.properties.dem_vote_share &&
o.properties.dem_vote_share < 0.5 + threshold
);
let dem_vote_share_extent = d3.extent(
map_data.senate_districts.features.map((o) => o.properties.dem_vote_share)
);

return {
total,
rep_count,
dem_count,
dem_vote_share_extent,
toss_ups,
toss_up_count: toss_ups.length
};
}
Insert cell
map_data = {
let map_file = await FileAttachment("nc_leg_2022.json").json();
let senate_districts = topojson.feature(
map_file,
map_file.objects["NC-S22-Dist1"]
);
let house_districts = topojson.feature(
map_file,
map_file.objects["NC-H22-Dist1"]
);
return {
senate_districts,
house_districts
};
}
Insert cell
Insert cell
import { Legend, Swatches } from "@d3/color-legend"
Insert cell
require.resolve(`tippy.js/themes/light-border.css`)
Insert cell
tippy_style = html`<link rel="stylesheet" href="${await require.resolve(
`tippy.js/themes/light-border.css`
)}">`
Insert cell
tippy = require("tippy.js@6")
Insert cell
// Princeton Gerrymandering project indicates the competive range should be +/- 3.5%,
// though my logistic regression analysis indicates more like +/- 1%.
// threshold = 0.035
Insert cell
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