Published
Edited
Jun 18, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof chartTour1 = hypotheseChart(t1Hypothese, "Premier tour")
// d3.selectAll('svg rect').attr('width', 10)
Insert cell
viewof chartTour1_styled = html`<div class="custom">${hypotheseChart(t1Hypothese, "Premier tour")}<style>
svg { font-family: georgia !important; font-size:11px !important}
svg.plot g:first-child text { font-size: 13px; font-weight:bold}
.observablehq>style:only-child:before {display:none !important}</style>`
Insert cell
t1Hypothese.tetes_liste
Insert cell
styleIt = html`<style>
svg { font-family: georgia !important; font-size:11px !important}
svg.plot g:first-child text { font-size: 13px; font-weight:bold}
.observablehq>style:only-child:before {display:none !important}
</style>`
Insert cell
tParser = d3.timeParse("%Y-%m-%d")
Insert cell
niceDates = (s) => {
let l = s.split('-')
return l[2] + '/' + l[1]
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
hypotheseChart(t2Hypothese, "Deuxième tour")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { sondages, regions, desc } from '@taniki/regionales2021-donnees-nsppolls'
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
regions
Insert cell
Insert cell
regionSondages = sondages.filter(i => i.region == regionSelect)
Insert cell
Insert cell
Insert cell
premierTour = tourSondages(regionSondages, 'Premier tour')
Insert cell
Insert cell
Inputs.table(secondTour)
Insert cell
secondTour = tourSondages(regionSondages, 'Deuxième tour')
Insert cell
tourSondages = (sondages, tour) => {
return sondages
.map(s => [s, s.tours.find(t => t.tour == tour)])
.map(([s, x]) => x?.hypotheses.map(h => ({sondage: s.id, date: s.fin_enquete, n: h.sous_echantillon, ...h})))
.filter(r => r != undefined)
.flat()
}
Insert cell
Insert cell
Insert cell
t1TetesListe = tetesListeRegion('Premier tour')
Insert cell
tetesListeRegion = (tour) => {
let data = [ ...new Set(
tourSondages(regionSondages, tour)
.map(x => x.tetes_liste)
.flat()
.map(t => t.tete_liste)
.filter(t => t != null)
)]
.map(t => [t, candidates.find(c => t.toLowerCase() == `${c['Prénom candidat']} ${c['Nom candidat']}`.toLowerCase() && c['Tête de Liste'] == 'Oui')])
.filter(t => t[1] != undefined )
.map(t => ({ 'name': t[0], info: t[1] }))

return data
}
Insert cell
Insert cell
t2TetesListe = tetesListeRegion('Deuxième tour')
Insert cell
Insert cell
// intentions = (sondages) =>
Insert cell
Insert cell
t1Intentions = premierTour
.map(t => t.tetes_liste.map(tl => ({sondage: t.sondage, date: t.date, n: t.sous_echantillon, hypothese: t.hypothese, ...tl})))
.flat()
.filter(t => t1TetesListe.map(t => t.name).includes(t.tete_liste))
.sort((a,b) => b.date.localeCompare(a.date))
Insert cell
Insert cell
t1Moyennes = [ ...d3.group(t1Intentions, i => i['tete_liste'])]
.map(t => {
let n = d3.sum(t[1], i => i.n)
let p = d3.sum(t[1], i => i.intentions * i.n) / n
let Z = jStat.normal.inv((1+0.95)/2, 0, 1)
let e = Z * Math.sqrt(((p/100) * (1 - (p/100))) / n)

return {
name: t[0],
mean: p,
n,
inf: d3.sum(t[1], i => i.erreur_inf * i.n) / d3.sum(t[1], i => i.n),
sup: d3.sum(t[1], i => i.erreur_sup * i.n) / d3.sum(t[1], i => i.n),
inf2: p - e*100,
sup2: p + e*100,
}
})
Insert cell
Inputs.table(t2Intentions)
Insert cell
t2Intentions = secondTour
.map(t => t.tetes_liste.map(tl => ({sondage: t.sondage, date: t.date, n: t.sous_echantillon, hypothese: t.hypothese, ...tl})))
.flat()
.filter(t => t2TetesListe.map(t => t.name).includes(t.tete_liste))
.sort((a,b) => b.date.localeCompare(a.date))
Insert cell
Inputs.table(t2Moyennes)
Insert cell
t2Moyennes = [ ...d3.group(t2Intentions, i => i['tete_liste'])]
.map(t => {
let n = d3.sum(t[1], i => i.n)
let p = d3.sum(t[1], i => i.intentions * i.n) / n
let Z = jStat.normal.inv((1+0.95)/2, 0, 1)
let e = Z * Math.sqrt(((p/100) * (1 - (p/100))) / n)

return {
name: t[0],
mean: p,
n,
inf: d3.sum(t[1], i => i.erreur_inf * i.n) / d3.sum(t[1], i => i.n),
sup: d3.sum(t[1], i => i.erreur_sup * i.n) / d3.sum(t[1], i => i.n),
inf2: p - e*100,
sup2: p + e*100,
}
})
Insert cell
import { raw as candidates, desc as descMinInt } from '@taniki/regionales2021-donnees-minint'
Insert cell
nuance = (c, name = 'tete_liste') => {
let found = candidates.find(x => `${x['Prénom candidat']} ${x['Nom candidat']}`.toLowerCase() == c[name].toLowerCase())
if (found) return found['Nuance candidat']
else if (c[name] == 'Fabienne Grébert') return 'ECO'
else if (c[name] == 'Aleksandar Nikolik') return 'RN'
else if (c[name] == 'Eric Pecqueur') return 'EXG'
else if (c[name] == 'Jean-Pierre Marchaud') return 'ECO'
else if (c[name] == 'Joseph Rivière') return 'RN'
else if (c[name] == 'Jean-Yves Payet') return 'EXG'
else if (c[name] == 'Jérémy Clément et Christelle de Crémiers') return 'DIV'

else return ''
}
Insert cell
candidates.find(x => `${x['Prénom candidat']} ${x['Nom candidat']}`.toLowerCase() == 'Éric PECQUEUR'.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, ""))
Insert cell
candidates.filter(x => `${x['Prénom candidat']} ${x['Nom candidat']}`.toLowerCase() == 'Jean-Pierre Marchau'.toLowerCase())
Insert cell
candidates.filter(x => `${x['Nom candidat']}`.toLowerCase() == 'Marchand'.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, ""))
Insert cell
// import {desc as descColors } from '@taniki/regionales2021-colors'
import {candidateColors, desc as descColors } from '@taniki/regionales2021-colors'
Insert cell
jStat = require('jstat')
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