localRequire = require.alias({
"d3@7.8.5/dist/d3.min.js": "/resources/ojs/d3-7.9.2.min.js",
"htl@0.3.1/dist/htl.min.js": "/resources/ojs/htl-0.3.1.min.js"
})
d3 = localRequire("/resources/ojs/d3-7.9.2.min.js")
htl = localRequire("/resources/ojs/htl-0.3.1.min.js")
Plot = localRequire("/resources/ojs/plot-0.6.11.umd.min.js")
Inputs = localRequire("/resources/ojs/inputs-0.10.6.min.js")
md = {
const { marked } = await localRequire("/resources/ojs/marked-4.3.0.min.js");
return (strings, ...values) => {
const div = document.createElement("div");
div.innerHTML = marked.parse(String.raw(strings, ...values)).trim();
// Losgemaakt teruggeven: de OJS-inspector toont alleen elementen zonder ouder als HTML.
return div.childNodes.length === 1 ? div.removeChild(div.firstChild) : div;
};
}
pwLang = (document.documentElement.lang || "nl").slice(0, 2) === "en" ? "en" : "nl"
T = ({
nl: {
choose: "Kies partijen voor coalitie:",
party: "Partij",
avgSeats: "Gemiddeld aantal zetels",
selectOne: "**Selecteer ten minste één partij om de grafiek te tonen.**",
seatsFor: "Aantal zetels voor coalitie",
pctSims: "Percentage simulaties",
barTitle: (s, p) => `${s} zetels: ${p.toFixed(1)}% van de simulaties`,
threshold: "Gewenst zetelaantal",
summary: (m, lo, hi) => `Deze coalitie haalt nu gemiddeld **${m}** zetels in de Peilingwijzer, maar we hebben te maken met een onzekerheidsmarge, dus het kunnen het er ook ${lo} of ${hi} zijn (95% credible interval). Let op: dit is de situatie op het moment dat de laatste peiling werd uitgevoerd, **geen voorspelling** van de verkiezingsuitslag.`,
thresholdPct: (t, p) => `Percentage van simulaties met ${t} of meer zetels: **${p}%**`
},
en: {
choose: "Choose parties for the coalition:",
party: "Party",
avgSeats: "Average number of seats",
selectOne: "**Select at least one party to show the graph.**",
seatsFor: "Number of seats for the coalition",
pctSims: "Percentage of simulations",
barTitle: (s, p) => `${s} seats: ${p.toFixed(1)}% of simulations`,
threshold: "Desired number of seats",
summary: (m, lo, hi) => `This coalition currently has an average of **${m}** seats in the Peilingwijzer. Because of the margin of error, it could also be ${lo} or ${hi} (95% credible interval). Note: this is the situation at the time of the most recent poll, **not a prediction** of the election result.`,
thresholdPct: (t, p) => `Percentage of simulations with ${t} or more seats: **${p}%**`
}
})[pwLang]
seats = await FileAttachment("/resources/coa_seats.csv").csv({typed: true})
seats_av = await FileAttachment("/resources/coa_seats_av.csv").csv({typed: true})
// Get party names from coa_seats_av.csv
partyNames = seats_av.map(d => d.party)
// Controls: party selection
viewof selectedParties = Inputs.checkbox(partyNames, {label: T.choose, value: ["D66", "VVD", "CDA"]})
// Compute coalition seats for each simulation
coalitionSeats = selectedParties.length > 0
? seats.map(row => d3.sum(selectedParties.map(party => row[party])))
: []
// Filter average seats for selected parties
averageSeats = seats_av.filter(d => selectedParties.includes(d.party))
// Load party colors
partycolours = await FileAttachment("/resources/partycolours.csv").csv({typed: true})
// Create color domain and range from the CSV
partyColorDomain = partycolours.map(d => d.party)
partyColorRange = partycolours.map(d => d.colour)
// Only show legend for selected parties
selectedPartyColors = partycolours.filter(d => selectedParties.includes(d.party))
selectedPartyColorDomain = selectedPartyColors.map(d => d.party)
selectedPartyColorRange = selectedPartyColors.map(d => d.colour)
ciLower = d3.quantile(coalitionSeats, 0.025)
ciUpper = d3.quantile(coalitionSeats, 0.975)
meanAndCI = [{
mean: averageSeats,
ciLower: ciLower,
ciUpper: ciUpper
}]
Average number of seats for this coalition
// Top stacked horizontal bar: average seats per selected party, all in one bar
selectedParties.length > 0 && seatCounts.length > 0
? Plot.plot({
marginLeft: 10,
color: {
legend: true,
label: T.party,
domain: selectedPartyColorDomain,
range: selectedPartyColorRange
},
x: {label: T.avgSeats, domain: [0, 150], grid: true, ticks: d3.range(0, 151, 15)},
y: {axis: null},
height: 70,
width: 800,
marks: [
Plot.barX(
averageSeats,
Plot.groupZ(
{x: "sum"}, // sum the zetels for each partij
{y: "", x: "zetels", fill: "party"}
)
),
Plot.ruleY(meanAndCI, {
y: "",
x1: "ciLower",
x2: "ciUpper",
stroke: "lightgrey",
marker: "none"
}),
Plot.ruleX([75], {stroke: "grey", strokeDasharray: "4,4", strokeWidth: 1})
]
})
: md`${T.selectOne}`
selectedParties.length > 0
? md`${T.summary(d3.sum(averageSeats, d => d.zetels).toFixed(0), ciLower.toFixed(0), ciUpper.toFixed(0))}`
: md``
Number of seats for this coalition per simulation
seatCounts = Array.from(
d3.rollup(
coalitionSeats,
v => v.length,
d => d
),
([seats, count]) => ({
seats,
count,
percentage: 100 * count / coalitionSeats.length
})
).sort((a, b) => a.seats - b.seats)
// Barplot: percentage simulaties per exact zetelaantal
selectedParties.length > 0 && seatCounts.length > 0
? Plot.plot({
marks: [
Plot.barY(seatCounts, {
x: "seats",
y: "percentage",
title: d => T.barTitle(d.seats, d.percentage)
})
],
x: {
label: T.seatsFor,
ticks: seatCounts.length,
tickFormat: d => Number.isInteger(d) ? d : ""
},
y: {label: T.pctSims},
height: 200
})
: md`${T.selectOne}`
percentage76plus = coalitionSeats.length > 0
? 100 * coalitionSeats.filter(d => d >= 76).length / coalitionSeats.length
: 0
// Zoek de modale categorie (de waarde met de hoogste count)
modaal = seatCounts.length > 0
? seatCounts.reduce((a, b) => (a.count > b.count ? a : b))
: {seats: null, percentage: null}
// Inline input for threshold
viewof seatThreshold = Inputs.range([0, 150], {step: 1, value: 76, label: html`<span style = "width:60px">${T.threshold}</span>`})
percentageThresholdPlus = coalitionSeats.length > 0
? 100 * coalitionSeats.filter(d => d >= seatThreshold).length / coalitionSeats.length
: 0
md`${T.thresholdPct(seatThreshold, percentageThresholdPlus.toFixed(1))}`
Each Peilingwijzer consists of a large number of simulations. The support for a party, and therefore for a coalition, can differ slightly from one simulation to the next.
The graph above shows how many seats the coalition gets across all simulations. For example, the coalition above gets seats in % of the simulations.
From this you can also calculate in what percentage of cases the coalition reaches a certain minimum number of seats (for example 76, a majority). Note: this applies to the current situation and is not a prediction of the election result.