Published
Edited
Dec 28, 2021
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = population
.map(a => ({
ons_code: a.ons_code,
name: a.name,
petition_freq: signatures.find(b => a.ons_code === b.ons_code).signature_count / a.population,
brexit_freq: brexit.find(b => a.ons_code === b.ons_code).brexit_freq
}))
Insert cell
petitions = fetch("https://petition.parliament.uk/petitions.json?page=1&state=all&topic=covid-19")
.then(r => r.json())
.then(d => d.data.map(p => ({
id: p.id,
"Title": p.attributes.action,
"Date closed": p.attributes.closed_at ? d3.timeFormat("%Y-%m-%d")(new Date(p.attributes.closed_at)) : null,
"Signatures": p.attributes.signature_count
})))
Insert cell
population = FileAttachment("sape23dt7mid2020parliconsyoaestimatesunformatted.xlsx")
.xlsx()
.then(w => w.sheet("Mid-2020 Persons", { range: "A6:C578" }))
.then(s => s
.map(d => ({
ons_code: d["A"],
name: d["B"],
population: d["C"]
}))
)
Insert cell
brexit = fetch("https://docs.google.com/spreadsheets/d/1wTK5dV2_YjCMsUYlwg0l48uWWf44sKgG8uFVMv5OWlA/export?format=csv")
.then(r => r.text())
.then(d => d3.csvParse(d)
.map(d => ({
ons_code: d.PCON11CD,
brexit_freq: parseFloat(d["Figure to use"])
}))
)
Insert cell
signatures = Promise.all(
selected_petitions
.map(p =>
fetch(`https://petition.parliament.uk/petitions/${p.id}.json`)
.then(r => r.json())
.then(d => d.data.attributes.signatures_by_constituency)))
.then(d => d.flat())
.then(d => d3.rollup(d, v => d3.sum(v, d => d.signature_count), k => k.ons_code))
.then(d => Array.from(d.entries()))
.then(d => d.map(d => ({ ons_code: d[0], signature_count: d[1] })))
Insert cell
import { Plot } from "@fil/plot-regression"
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