Public
Edited
Oct 6, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
district_winners = district_data
.map(
({
"Paul Vallas": vallas,
'Jesus "Chuy" Garcia': garcia,
"Lori Lightfoot": lightfoot,
"Brandon Johnson": johnson,
district
}) => ({
district,
...Object.entries({ garcia, vallas, lightfoot, johnson }).reduce((a, b) =>
b[1] > a[1] ? b : a
)
})
)
.map((d) => ({
district: d.district,
candidate: d[0],
votes: d[1]
}))
Insert cell
districts = {
blocks.features.map(
(d) =>
(d.properties.district = assignment_hash.get(d.properties.precinct_id)?.[
selected_map
])
);
return blocks;
}
Insert cell
district_borders = turfjs.dissolve(districts, { propertyName: "district" })
Insert cell
fields = Object.fromEntries(
[
"p1_001n",
"school_age_public",
"total_cvap",
"black_cvap",
"white_cvap",
"latino_cvap",
"paul vallas",
"lori e. lightfoot",
"brandon johnson",
'jesus "chuy" garcia'
].map((f) => [f, rollup(f)])
)
Insert cell
district_data = {
const ids = [...Array(20).keys()];
return ids.map((i) => ({
district: i,
popululation: fields.p1_001n.get(i),
"public school students": fields.school_age_public.get(i),
"total CVAP": fields.total_cvap.get(i),
"Black CVAP %": fields.black_cvap.get(i) / fields.total_cvap.get(i),
"Latino CVAP %": fields.latino_cvap.get(i) / fields.total_cvap.get(i),
"white CVAP %": fields.white_cvap.get(i) / fields.total_cvap.get(i),
'Jesus "Chuy" Garcia': fields['jesus "chuy" garcia'].get(i),
"Paul Vallas": fields["paul vallas"].get(i),
"Lori Lightfoot": fields["lori e. lightfoot"].get(i),
"Brandon Johnson": fields["brandon johnson"].get(i)
}));
}
Insert cell
district_data
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
rollup = (property) =>
d3.rollup(
districts.features,
(v) => d3.sum(v, (d) => d.properties[property]),
(d) => d.properties.district
)
Insert cell
blocks_topo = FileAttachment("blocks@9.topojson").json()
Insert cell
choice = (arr) => {
return arr[Math.floor(Math.random() * arr.length)];
}
Insert cell
mapIds = Array.from({ length: 1000 }, (_, i) => i * 10)
Insert cell
selected_map = {
updateButton;
return choice(mapIds);
}
Insert cell
blocks = topojson.feature(blocks_topo, blocks_topo.objects.blocks)
Insert cell
assignments = FileAttachment("assignment@1.csv").csv({ typed: true })
Insert cell
assignment_hash = new Map(assignments.map((d) => [d.block, d]))
Insert cell
may_5_draft_plan_data = FileAttachment("may_5_draft_plan@2.csv").csv({
typed: true
})
Insert cell
may_5_winners = may_5_draft_plan_data
.map(({ district_name, vallas, garcia, lightfoot, johnson }) => ({
district_name,
...Object.entries({ garcia, vallas, lightfoot, johnson }).reduce((a, b) =>
b[1] > a[1] ? b : a
)
}))
.map((d) => ({
district_name: d.district_name,
candidate: d[0],
votes: d[1]
}))
Insert cell
may_5_draft_plan = FileAttachment("may_5_draft_plan@2.geojson").json()
Insert cell
import { rewind } from "@fil/rewind"
Insert cell
turfjs = require("@turf/turf")
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