Public
Edited
Apr 6, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
distChart = {
const spec = {
height: height,
y: {
grid: true,
tickFormat: "~s"
},
x: {
label: "Distance (mi.) →"
},
marks: [
Plot.rectY(
data,
Plot.binX(
{ y: "count" },
{
x: "distance",
filter: (d) =>
inDomain(delay(d), delayDomain) &&
inDomain(hour(d), hourDomain) &&
inDomain(d.date, dateDomain)
}
)
),
Plot.ruleY([0])
]
};

let chart = Plot.plot(spec);

const x = chart.scale("x"),
[x1, x2] = x.range;
const y = chart.scale("y"),
[y1, y2] = y.range;

let domain = x.domain;

const wrapper = svg`<svg viewBox="${chart.getAttribute("viewBox")}">${chart}`;

const brush = d3
.brushX()
.extent([
[x1, y2],
[x2, y1]
])
.on("brush end", (event) => {
const { selection, sourceEvent } = event;
domain = selection && selection.map(x.invert);
mutable distDomain = domain;
});

d3.select(wrapper).call(brush);

return wrapper;
}
Insert cell
hourDayChart = {
const spec = {
height: height,
y: {
grid: true,
tickFormat: "~s"
},
x: {
label: "Hour of the Day →",
domain: hourScaleDomain
},
marks: [
Plot.rectY(
data,
Plot.binX(
{ y: "count" },
{
x: (d) => hour(d),
filter: (d) =>
inDomain(d.distance, distDomain) &&
inDomain(delay(d), delayDomain) &&
inDomain(d.date, dateDomain)
}
)
),
Plot.ruleY([0])
]
};

let chart = Plot.plot(spec);
const x = chart.scale("x"),
[x1, x2] = x.range;
const y = chart.scale("y"),
[y1, y2] = y.range;

let domain = x.domain;

const wrapper = svg`<svg viewBox="${chart.getAttribute("viewBox")}">${chart}`;

const brush = d3
.brushX()
.extent([
[x1, y2],
[x2, y1]
])
.on("brush end", (event) => {
const { selection, sourceEvent } = event;
domain = selection && selection.map(x.invert);
mutable hourDomain = domain;
});

d3.select(wrapper).call(brush);

return wrapper;
}
Insert cell
delayChart = {
const spec = {
height: height,
y: {
grid: true,
tickFormat: "~s"
},
x: {
label: "Arrival delay (min) →"
},
marks: [
Plot.rectY(
data,
Plot.binX(
{ y: "count" },
{
x: (d) => delay(d),
thresholds: 80,
filter: (d) =>
inDomain(d.distance, distDomain) &&
inDomain(hour(d), hourDomain) &&
inDomain(d.date, dateDomain)
}
)
),
Plot.ruleY([0])
]
};

let chart = Plot.plot(spec);

const x = chart.scale("x"),
[x1, x2] = x.range;
const y = chart.scale("y"),
[y1, y2] = y.range;

let domain = x.domain;

const wrapper = svg`<svg viewBox="${chart.getAttribute("viewBox")}">${chart}`;

const brush = d3
.brushX()
.extent([
[x1, y2],
[x2, y1]
])
.on("brush end", (event) => {
const { selection, sourceEvent } = event;
domain = selection && selection.map(x.invert);
mutable delayDomain = domain;
});

d3.select(wrapper).call(brush);

return wrapper;
}
Insert cell
dateChart = {
const spec = {
height: height,
y: {
grid: true,
tickFormat: "~s"
},
x: {
label: "Date →",
type: "time"
},
marks: [
Plot.rectY(
data,
Plot.binX(
{ y: "count" },
{
x: "date",
thresholds: 80,
filter: (d) =>
inDomain(d.distance, distDomain) &&
inDomain(delay(d), delayDomain) &&
inDomain(hour(d), hourDomain)
}
)
),
Plot.ruleY([0])
]
};

let chart = Plot.plot(spec);

const x = chart.scale("x"),
[x1, x2] = x.range;
const y = chart.scale("y"),
[y1, y2] = y.range;

let domain = x.domain;

const wrapper = svg`<svg viewBox="${chart.getAttribute("viewBox")}">${chart}`;

const brush = d3
.brushX()
.extent([
[x1, y2],
[x2, y1]
])
.on("brush end", (event) => {
const { selection, sourceEvent } = event;
domain = selection && selection.map(x.invert);
mutable dateDomain = domain;
});

d3.select(wrapper).call(brush);

return wrapper;
}
Insert cell
Insert cell
height = 140
Insert cell
hourScaleDomain = d3.extent(data, (d) => hour(d))
Insert cell
Insert cell
delay = function (d) {
return Math.max(-60, Math.min(149, d.delay));
}
Insert cell
hour = function (d) {
return d.date.getHours() + d.date.getMinutes() / 60;
}
Insert cell
parseDate = function (d) {
return new Date(
2001,
d.substring(0, 2) - 1,
d.substring(2, 4),
d.substring(4, 6),
d.substring(6, 8)
);
}
Insert cell
Insert cell
inDomain = function (d, domain) {
return domain === null ? true : d >= domain[0] && d <= domain[1];
}
Insert cell
Insert cell
mutable distDomain = null
Insert cell
mutable delayDomain = null
Insert cell
mutable hourDomain = null
Insert cell
mutable dateDomain = null
Insert cell
Insert cell
data = flights.map((d, i) => ({
...d,
date: parseDate(d.date)
}))
Insert cell
flights.map((d) => parseDate(d.date))
Insert cell
flights-3m.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

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