guard(() => {
if (period === "hour") {
if (hours.length === 0) return noDetail();
return ch.figure({
note: `Departures ${hourSpan}, grouped by the hour each was scheduled, in Pacific time. We leave out any hour with fewer than ${MIN_OBSERVED} departures seen.`,
legend: bandLegend(),
chart: ch.bars(hours, {
category: "label", value: "on_time_share_of_observed", upright: true, fill: d => d.band.color,
axisLabel: "On time", references: [0.6, 0.8],
ariaLabel: `Share of departures on time by scheduled hour, ${hourSpan}`,
tip: d => `${d.label}\n${pct(d.on_time_share_of_observed)} on time\nTypically ${lateness(d.deviation_s_median)}\n${count(d.observed)} of ${count(d.scheduled)} departures seen`,
}),
tableLabel: "Show the hours as a table",
table: ch.table(hours, {
caption: `On-time figures by scheduled hour, ${hourSpan}`,
columns: [
{key: "label", label: "Hour", sortValue: d => d.hour},
{key: "on_time_share_of_observed", label: "On time", align: "right", format: v => pct(v)},
{key: "deviation_s_median", label: "Typical departure", align: "right", format: v => lateness(v)},
{key: "observed", label: "Seen", align: "right", format: count},
{key: "scheduled", label: "Scheduled", align: "right", format: count},
],
}),
methods: "on-time-performance",
});
}
if (period === "week" || period === "month") {
const rows = (periods?.grains?.[period] ?? []).filter(r => r.observed > 0)
.map(r => ({...r, label: periodName(period, r.period) + (r.partial ? " (part)" : "")}));
if (rows.length === 0) return emptyNote("unavailable", `The figures by ${period}`);
const word = period === "week" ? "week (Monday to Sunday)" : "month";
return ch.figure({
note: `Every departure seen in each ${word}, early, on time and late. "Part" marks a ${period} the archive does not cover from start to end.`,
legend: splitLegend(),
chart: ch.stack(rows, {category: "label", ariaLabel: `Early, on time and late departures by ${period}`}),
tableLabel: `Show the ${period}s as a table`,
table: ch.table([...rows].reverse(), {
caption: `On-time figures by ${period}`,
columns: [
{key: "label", label: period === "week" ? "Week" : "Month", sortValue: d => d.period},
{key: "on_time_share_of_observed", label: "On time", align: "right", format: v => pct(v, 1)},
{key: "early_share_of_observed", label: "Early", align: "right", format: v => pct(v, 1)},
{key: "late_share_of_observed", label: "Late", align: "right", format: v => pct(v, 1)},
{key: "deviation_s_median", label: "Typical departure", align: "right", format: v => lateness(v)},
{key: "days", label: "Days", align: "right", format: count},
{key: "observed", label: "Departures seen", align: "right", format: count},
],
}),
methods: "on-time-performance",
});
}
if (days.length === 0) return emptyNote("no-days");
if (days.length < 2) return html`<p class="empty">So far there is one day of data, ${longDate(latestDate)}, and ${pct(days[0].on_time_share_of_observed)} of the departures we saw that day were on time.
A day-by-day trend line will appear once a second day comes in.</p>`;
const shown = recentDays(days, showAll);
return ch.figure({
chart: ch.trend(shown, {
y: "on_time_share_of_observed", format: v => pct(v, 1), yLabel: "On time", height: 240,
ariaLabel: "Share of departures on time, by service day",
describe: `From ${longDate(shown[0].day)} to ${longDate(latestDate)}. On the latest day, ${pct(days.at(-1).on_time_share_of_observed)} of the departures seen were on time.`,
tip: d => `${longDate(d.day)}\n${pct(d.on_time_share_of_observed, 1)} on time\n${pct(d.observed_share)} of departures seen`,
}),
tableLabel: "Show the days as a table",
table: ch.table([...shown].reverse(), {
caption: "Share of departures on time, by service day",
columns: [
{key: "day", label: "Service day", format: v => longDate(v)},
{key: "on_time_share_of_observed", label: "On time", align: "right", format: v => pct(v, 1)},
{key: "deviation_s_median", label: "Typical departure", align: "right", format: v => lateness(v)},
{key: "observed", label: "Departures seen", align: "right", format: count},
{key: "scheduled", label: "Scheduled", align: "right", format: count},
],
}),
methods: "on-time-performance",
});
}, "The chart over time")