Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csv(url, (crash) => ({
// Combine "date" et "time" pour créer un objet Date
datetime: d3.timeParse('%m/%d/%Y %H:%M')(crash.date + ' ' + crash.time),
borough: crash.borough,
zip: crash.zip,
injured: +crash.injured, // + converti les chaines de caractères en nombre
killed: +crash.killed,
cause: crash.cause
}))
Insert cell
Insert cell
Insert cell
Insert cell
d3.sum(data, d => d.killed)
Insert cell
Insert cell
d3.mean(data, d => d.killed + d.injured)
Insert cell
Insert cell
data.filter(d => d.zip === '').length
Insert cell
Insert cell
d3.sum(data, d => d.zip === '')
Insert cell
Insert cell
new Set(data.filter(d => d.zip !== '').map(d => d.zip)).size
Insert cell
Insert cell
d3.max(data, d => d.injured)
Insert cell
Insert cell
data.filter(d => d.zip === '11201')
Insert cell
Insert cell
data.filter(d => d.borough !== '')
Insert cell
Insert cell
d3.rollup(
data,
v => v.length,
d => d.datetime.getDate()
)
Insert cell
Insert cell
d3.rollup(
data,
v => ({ compteAccident: v.length, moyenneGensBlesses: d3.mean(v, s => s.injured)}),
d => d.datetime.getDate(),
)
Insert cell
Insert cell
data.slice().sort((a, b) => b.injured - a.injured).slice(0, 5)
Insert cell
Insert cell
{
// On fait le calcul avec rollup comme dans la question 8
const r = d3.rollup(
data,
v => v.length,
d => d.datetime.getMonth()
);

// On converti l'objet Map retourné en Object
const o = Object.fromEntries(r);
// On récupère la valeur maximale (des valeurs, pas des clés)
const max = d3.max(Object.values(o));
// On chercher quelle clée correspond à cette valeur :
const result = Object.keys(o).find(v => o[v] === max);

// Un tableau avec les mois, pour retourner le nom du mois plutôt que l'indice du mois
const months = ['Janv.', 'Fev.', 'Mars', 'Avr.', 'Mai', 'Juin', 'Jui.', 'Aout', 'Sept.', 'Oct.', 'Nov.', 'Dec.'];
return months[result];
}
Insert cell
Insert cell
dataSummary = {
// Le résultat à créer
const tableau = [];
// On récupère une liste (sans doublon) des noms de boroughs
const b = [...new Set(data.map(d => d.borough))];

// On trouve l'indice de l'entré vide ("") et on remplace par "Inconnu"
const ixVide = b.findIndex(d => d === "");
b[ixVide] = "Borough inconnu"

// Pour chaque Borough, on calcule les valeurs souhaitées (somme des blessés et somme des tués)
b.forEach((boroActuel) => {
const totalInjured = d3.sum(data.filter(d => d.borough === boroActuel), d => d.injured);
const totalKilled = d3.sum(data.filter(d => d.borough === boroActuel), d => d.killed);

// On ajout l'entrée "borough actuel" - somme des blessés
tableau.push({
name: boroActuel,
value: totalInjured,
type: 'injured',
});
// On ajout l'entrée "borough actuel" - somme des tués
tableau.push({
name: boroActuel,
value: totalKilled,
type: 'killed',
});
});

// On retourne le tableau qu'on vient de créer puis d'enrichir
return tableau;
}
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
y: { nice: true, label: 'Nombre d\'accidents'},
x: { label: null },
title: 'Nombre d\'accidents par Borough',
marks: [
Plot.barY(
data,
Plot.groupX(
{y: "count"},
{x: d => d.borough === '' ? 'Unknown' : d.borough , tip: true, fill: 'steelblue', sort: {x: 'y', reverse: true}}
)
),
]
})
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