Published
Edited
Feb 13, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
formatDate= d3.timeFormat("%b");
Insert cell
formatDay= d3.timeFormat("%A");
Insert cell
data = d3.csv(url, crash => ({
// combine the date and time strings into one Date object
dateTime: d3.timeParse('%m/%d/%Y')(crash.date),
month: formatDate(d3.timeParse('%m/%d/%Y')(crash.date)),
day: formatDay(d3.timeParse('%m/%d/%Y')(crash.date)),
borough: crash.borough,
zip: crash.zip,
injured: +crash.injured, // + converts the string to an int
killed: +crash.killed,
cause: crash.cause
}))
Insert cell
{
const numCollisionsByMonth = d3.rollup(data, crashesForMonth => crashesForMonth.length, d => d.month);
const monthlyCount = Array.from(numCollisionsByMonth,
([month, numCollisions]) => ({month, numCollisions}));
return monthlyCount;
}
Insert cell
Insert cell
{
const numCollisionsByDay = d3.rollup(data,
crashesForDay => crashesForDay.length,
d => d.day);
const dailyCount = Array.from(numCollisionsByDay,
([day, numCollisions]) => ({day, numCollisions}));
return dailyCount;
}

Insert cell
Insert cell
Insert cell
data1 = d3.csv(url, crash => ({
// combine the date and time strings into one Date object
dateTime: d3.timeParse('%m/%d/%Y')(crash.date),
borough: crash.borough,
zip: crash.zip,
injured: +crash.injured, // + converts the string to an int
killed: +crash.killed,
cause: crash.cause
}))
Insert cell
{
const collisionsWithCause = data1.filter(d => d.cause !== "Unspecified");
const collisionsWithCauses = collisionsWithCause.filter(d => d.cause !== "");
const numCollisionsByCause = d3.rollup(collisionsWithCauses,
crashesForCause => crashesForCause.length,
d => d.cause);
const numCollisions = Array.from(numCollisionsByCause, ([cause, numCollisions]) => ({cause, numCollisions}));
return numCollisions.sort((a, b) => d3.descending(a.numCollisions, b.numCollisions))
.slice(0, 3);
}
Insert cell
Insert cell
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