2024-06-20 18:36:52 +02:00
|
|
|
export function getNomZone(typeResultats, zoneInfo) {
|
2024-06-20 23:29:18 +02:00
|
|
|
if (!zoneInfo.type)
|
2024-06-17 18:40:26 +02:00
|
|
|
return ""
|
|
|
|
else if (typeResultats === "france")
|
|
|
|
return "France"
|
|
|
|
else if (typeResultats === "region")
|
2024-06-20 18:36:52 +02:00
|
|
|
return `Région ${zoneInfo.nom}`
|
2024-06-17 18:40:26 +02:00
|
|
|
else if (typeResultats === "departement")
|
2024-06-20 18:36:52 +02:00
|
|
|
return `Département ${zoneInfo.nom}`
|
2024-06-17 18:40:26 +02:00
|
|
|
else if (typeResultats === "circonscription")
|
2024-06-20 18:36:52 +02:00
|
|
|
return `Circonscription ${zoneInfo.id}`
|
2024-06-17 18:40:26 +02:00
|
|
|
else if (typeResultats === "commune")
|
2024-06-20 18:36:52 +02:00
|
|
|
return `Commune ${zoneInfo.nom}`
|
2024-06-17 18:40:26 +02:00
|
|
|
else if (typeResultats === "bureau_vote")
|
2024-06-20 18:36:52 +02:00
|
|
|
return zoneInfo.libelle
|
2024-06-17 18:40:26 +02:00
|
|
|
}
|
|
|
|
|
2024-06-20 23:29:18 +02:00
|
|
|
export function trierCandidats(candidats, voix_par_candidat, key = "numero") {
|
2024-06-17 18:40:26 +02:00
|
|
|
return candidats.toSorted((l1, l2) => {
|
2024-06-20 23:29:18 +02:00
|
|
|
return (voix_par_candidat[l2[key]] || 0) - (voix_par_candidat[l1[key]] || 0)
|
2024-06-17 18:40:26 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-20 23:29:18 +02:00
|
|
|
export function regrouperVoix(voixCandidats, candidats, blocs, nuances, dejaGroupesParNuance = false) {
|
2024-06-17 18:40:26 +02:00
|
|
|
if (!candidats || !voixCandidats || !blocs || !nuances
|
|
|
|
|| candidats.length === 0 || blocs.length === 0 || nuances.length === 0)
|
|
|
|
return [{}, {}]
|
|
|
|
|
2024-06-20 23:29:18 +02:00
|
|
|
const key = dejaGroupesParNuance ? "code" : "numero"
|
|
|
|
|
2024-06-17 18:40:26 +02:00
|
|
|
const parBloc = {}
|
2024-06-20 23:29:18 +02:00
|
|
|
const parNuance = dejaGroupesParNuance ? voixCandidats : {}
|
2024-06-17 18:40:26 +02:00
|
|
|
|
|
|
|
for (let bloc of blocs) {
|
|
|
|
parBloc[bloc.nom] = 0
|
|
|
|
}
|
2024-06-20 23:29:18 +02:00
|
|
|
|
|
|
|
if (!dejaGroupesParNuance) {
|
|
|
|
for (let nuance of nuances) {
|
|
|
|
parNuance[nuance.code] = 0
|
|
|
|
}
|
2024-06-17 18:40:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let candidat of candidats) {
|
2024-06-20 23:29:18 +02:00
|
|
|
parBloc[candidat.bloc] += voixCandidats[candidat[key]] || 0
|
|
|
|
if (!dejaGroupesParNuance)
|
|
|
|
parNuance[candidat.nuance] += voixCandidats[candidat[key]] || 0
|
2024-06-17 18:40:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return [parBloc, parNuance]
|
|
|
|
}
|
|
|
|
|
|
|
|
export function calculerSieges(listes, resultats, seuil = 0.05) {
|
|
|
|
if (!resultats['voix'])
|
|
|
|
return {}
|
|
|
|
|
|
|
|
const MAX_SIEGES = 81
|
|
|
|
const sieges = {}
|
|
|
|
const listesElues = []
|
|
|
|
let siegesAffectes = 0
|
|
|
|
let totalVoix = resultats.exprimes
|
|
|
|
for (let liste of listes) {
|
|
|
|
const voix = resultats?.voix[liste.numero] ?? 0
|
|
|
|
if (voix / resultats.exprimes < seuil) {
|
|
|
|
// Barre des 5 % non franchie
|
|
|
|
totalVoix -= voix
|
|
|
|
sieges[liste.numero] = 0
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
listesElues.push(liste)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listesElues.length === 0)
|
|
|
|
return
|
|
|
|
|
|
|
|
for (let liste of listesElues) {
|
|
|
|
const voix = resultats?.voix[liste.numero] ?? 0
|
|
|
|
sieges[liste.numero] = Math.floor(MAX_SIEGES * voix / totalVoix)
|
|
|
|
siegesAffectes += sieges[liste.numero]
|
|
|
|
}
|
|
|
|
|
|
|
|
while (siegesAffectes < MAX_SIEGES) {
|
|
|
|
// Méthode de la plus forte moyenne pour affecter les sièges restants
|
|
|
|
let maxMoyenne = 0
|
|
|
|
let listeElue = null
|
|
|
|
for (let liste of listesElues) {
|
|
|
|
if (sieges[liste.numero] < MAX_SIEGES) {
|
|
|
|
const voix = resultats?.voix[liste.numero] ?? 0
|
|
|
|
const moyenne = voix / (sieges[liste.numero] + 1)
|
|
|
|
if (moyenne > maxMoyenne) {
|
|
|
|
maxMoyenne = moyenne
|
|
|
|
listeElue = liste
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sieges[listeElue.numero]++
|
|
|
|
siegesAffectes++
|
|
|
|
}
|
|
|
|
|
|
|
|
return sieges
|
|
|
|
}
|