Public
Edited
Jul 1, 2024
Importers
4 stars
Insert cell
Insert cell
Inputs.table(table)
Insert cell
d3.group(table, (d) => d.circo) // 577 circos
Insert cell
table = d3
.hierarchy(
{
elements: resultats
.flat()
.flatMap((d) => d.elements)
.filter((d) => d.name === "EnsembleGeo")
},
(d) => (d.name === "Candidat" ? null : d.elements)
)
.leaves()
.filter((d) => d.data.name === "Candidat")
.map((d) => ({
...Object.fromEntries(
d.data.elements.map(({ name, elements }) => [
name,
name.startsWith("Rapport")
? +elements[0].text.replace(",", ".")
: elements[0].text
])
),
circo: d
.ancestors()
.find((d) => d.data.name === "Circonscription")
.data.elements.find((d) => d.name === "CodCirElec").elements[0].text
}))
Insert cell
resultats = {
const ENDPOINT =
"https://www.resultats-elections.interieur.gouv.fr/telechargements/LG2024/resultatsT1/";
const depts = await d3.html(ENDPOINT).then(
(d) =>
Array.from(d3.select(d).selectAll("a"), (d) =>
d.getAttribute("href")
).filter((d) => !d.match(/[.]/)) // remove .. and xxx.xml
);

const circos = (
await Promise.all(
depts.map((d) =>
d3.html(`${ENDPOINT}${d}`).then((h) =>
Array.from(d3.select(h).selectAll("a"), (d) => d.getAttribute("href"))
.filter((e) => e.match(`^R1${d.replace(/[/]$/, "..[.]xml$")}`))
.map((e) => `${d}${e}`)
)
)
)
).flat();

const allCircos = Promise.all(
circos.map((d) =>
d3.text(`${ENDPOINT}${d}`).then((d) => convert.xml2js(d).elements)
)
);

return allCircos;
}
Insert cell
convert = import("https://esm.sh/xml-js")
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