Restructuration
This commit is contained in:
96
nupes-elections-front/src/utils.js
Normal file
96
nupes-elections-front/src/utils.js
Normal file
@@ -0,0 +1,96 @@
|
||||
export function getNomZone(typeResultats, resultats) {
|
||||
if (!resultats || resultats.length === 0)
|
||||
return ""
|
||||
else if (typeResultats === "france")
|
||||
return "France"
|
||||
else if (typeResultats === "region")
|
||||
return `Région ${resultats.region.nom}`
|
||||
else if (typeResultats === "departement")
|
||||
return `Département ${resultats.departement.nom}`
|
||||
else if (typeResultats === "circonscription")
|
||||
return `Circonscription ${resultats.circonscription.id}`
|
||||
else if (typeResultats === "commune")
|
||||
return `Commune ${resultats.commune.nom}`
|
||||
else if (typeResultats === "bureau_vote")
|
||||
return resultats.bureau_vote.libelle
|
||||
}
|
||||
|
||||
export function trierCandidats(candidats, voix_par_candidat) {
|
||||
return candidats.toSorted((l1, l2) => {
|
||||
return (voix_par_candidat[l2.numero] || 0) - (voix_par_candidat[l1.numero] || 0)
|
||||
})
|
||||
}
|
||||
|
||||
export function regrouperVoix(voixCandidats, candidats, blocs, nuances) {
|
||||
if (!candidats || !voixCandidats || !blocs || !nuances
|
||||
|| candidats.length === 0 || blocs.length === 0 || nuances.length === 0)
|
||||
return [{}, {}]
|
||||
|
||||
const parBloc = {}
|
||||
const parNuance = {}
|
||||
|
||||
for (let bloc of blocs) {
|
||||
parBloc[bloc.nom] = 0
|
||||
}
|
||||
for (let nuance of nuances) {
|
||||
parNuance[nuance.code] = 0
|
||||
}
|
||||
|
||||
for (let candidat of candidats) {
|
||||
parBloc[candidat.bloc] += voixCandidats[candidat.numero] || 0
|
||||
parNuance[candidat.nuance] += voixCandidats[candidat.numero] || 0
|
||||
}
|
||||
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user