// The picker starts on the route the address names (routes.html#60). It is
// built once, so resizing the window never puts it back.
viewof routeId = {
const name = r => [routeTitle(r), routeSub(r)].filter(Boolean).join(" · ");
const select = h("select", {id: "route-select"},
h("option", {value: ""}, "All routes"),
...routeList.map(r => h("option", {value: r.route_id}, name(r))));
select.value = routeFromHash(routeList) ?? "";
const picker = h("div", {class: "route-picker"}, h("label", {for: "route-select"}, "Route"), select);
Object.defineProperty(picker, "value", {
get: () => select.value || null,
set: id => { select.value = id ?? ""; },
});
return picker;
}Routes
On time, route by route and stop by stop
On-time figures for each Island Transit route and each timetable stop, from saved bus locations.
Pick a route on the map or from the list to see how its buses did. We count departures only at the stops whose times are printed in Island Transit’s timetables, called timepoints, because those are the times riders are promised. A bus is on time if it leaves from 1 minute early to 5 minutes late. To watch every bus move over a whole day, see Replay a day.
import {json, pct, count, band, routeTitle, routeSub, routeOrder, routeSlug, routeFromHash, lateness, plural, longDate, routeMap, withDates, bandIcon, bandTag, bandLegend, emptyNote, guard, stats, h, MIN_OBSERVED, DATA_URL, DATA_RAW_URL} from "./lib.js"
import {drawRouteMap, routeSummary} from "./map.js"
import {makeCharts} from "./charts.js"
import {Plot} from "./vendor/plot-0.6.11/plot.js"
ch = makeCharts({Plot})
L = require(new URL("vendor/leaflet-1.9.4/leaflet.js", document.baseURI).href)
recent = json("data/ontime/recent.json")
mapData = routeMap(recent?.timepoints)
routeList = mapData == null || !Array.isArray(mapData.routes) ? [] : mapData.routes
.map(r => ({...r, stats: recent?.routes?.[r.route_id] ?? null}))
.sort(routeOrder)
byId = Object.fromEntries(routeList.map(r => [r.route_id, r]))
pick = id => {
const view = viewof routeId;
if (view.value === id) return;
view.value = id;
view.dispatchEvent(new Event("input", {bubbles: true}));
}linked = ({map: null, chart: null})
mapView = {
if (routeList.length === 0) return null;
const view = drawRouteMap(L, mapData, recent, {onSelect: pick, onStop: id => {
const s = recent?.routes?.[(viewof routeId).value]?.stops?.find(x => x.stop_id === id);
linked.chart?.highlight(s?.name ?? null);
}});
linked.map = view;
invalidation.then(() => view.destroy());
return view;
}bandLegend({title: recent == null ? "Departures on time" : `Departures on time${recent.days > 1 ? `, last ${recent.days} days` : recent.days === 1 ? ", latest day" : ""}`})// Follow the address: a link to another route on this page (routes.html#60),
// or the Back button.
{
const follow = () => pick(routeFromHash(routeList));
addEventListener("hashchange", follow);
addEventListener("popstate", follow);
invalidation.then(() => {
removeEventListener("hashchange", follow);
removeEventListener("popstate", follow);
});
}// Show the picked route on the map, say so under it, and put it in the
// address so the page can be shared and the Back button returns to all routes.
{
mapView?.setSelected(routeId);
const r = routeId == null ? null : byId[routeId];
const caption = document.getElementById("map-caption");
if (caption) {
if (r == null) caption.replaceChildren(routeList.length ? "All routes are shown. Pick one to see its stops." : "");
else caption.replaceChildren(
bandIcon(band(r.stats?.on_time_share_of_observed, r.stats?.observed)),
h("strong", {}, routeTitle(r)), `: ${routeSummary(r.stats)}. `,
h("a", {href: "#route-detail"}, "Stop-by-stop details"));
}
const slug = r == null ? "" : `#${routeSlug(r, routeId)}`;
if (location.hash !== slug) {
const address = `${location.pathname}${location.search}${slug}`;
// An address that names no route is tidied in place; a pick is a step
// the Back button can undo.
if (r == null && routeFromHash(routeList) == null) history.replaceState(null, "", address);
else history.pushState(null, "", address);
}
}guard(() => {
if (recent == null) return emptyNote("unavailable", "The route figures");
const span = recent.days === 1
? `These figures cover ${longDate(recent.last_service_date)}.`
: `These figures cover the ${recent.days} days from ${longDate(recent.first_service_date)} to ${longDate(recent.last_service_date)}.`;
const idle = recent.days_without_service?.length
? ` Days with no service are left out (${recent.days_without_service.map(d => longDate(d)).join(", ")}).` : "";
const change = recent.schedule_change
? ` Island Transit changed its schedule on ${longDate(recent.schedule_change.effective)}, so these figures start over from that day.` : "";
return h("p", {class: "note"}, span, idle, change);
}, "The route figures")overview = () => {
if (routeList.length === 0) return emptyNote("unavailable", "The list of routes");
const rows = routeList.map(r => {
const s = r.stats;
return {
route: r, name: routeTitle(r), where: routeSub(r),
on_time: s?.on_time_share_of_observed ?? null,
typical: s?.deviation_s_median ?? null,
seen: s?.observed ?? 0,
band: band(s?.on_time_share_of_observed, s?.observed),
};
});
return html`<h2>All routes</h2>
<p class="note">We don't rate a route until we've seen at least ${MIN_OBSERVED} of its departures.</p>
${ch.table(rows, {
caption: "On-time figures for every route",
sort: {key: "seen", descending: true}, pageSize: 40,
columns: [
{key: "name", label: "Route", sortValue: r => routeList.indexOf(r.route),
format: (v, r) => h("a", {href: `#${routeSlug(r.route, r.route.route_id)}`}, v)},
{key: "where", label: "Serves"},
{key: "on_time", label: "On time", align: "right", format: v => pct(v)},
{key: "typical", label: "Typical departure", align: "right", format: v => lateness(v)},
{key: "seen", label: "Departures seen", align: "right", format: count},
{key: "band", label: "Band", format: b => bandTag(b), sortValue: r => r.band.rank * 10 + (r.on_time ?? 0)},
],
})}`;
}
detail = (r, s) => {
const title = html`<h2 class="route-title">${routeTitle(r)} <span class="route-sub">${routeSub(r)}</span></h2>`;
if (s == null) return html`${title}
<p class="empty">None of this route's timepoint departures were scheduled during this period.</p>`;
const b = band(s.on_time_share_of_observed, s.observed);
return html`${title}
${stats([
{big: true, label: "Departures on time", value: pct(s.on_time_share_of_observed), note: bandTag(b)},
{label: "Typical departure", value: lateness(s.deviation_s_median),
note: s.deviation_s_p25 == null ? null : `Half of departures fell between ${lateness(s.deviation_s_p25)} and ${lateness(s.deviation_s_p75)}.`},
{label: "Departures seen", value: `${count(s.observed)} of ${count(s.scheduled)}`,
note: "We couldn't put a time on the others, most often because of a gap in the bus's tracking."},
])}
${stopChart(s)}
${trendChart(s)}`;
}
stopChart = (s) => {
const stops = (s.stops ?? []).filter(d => d.deviations_counted > 0 && d.deviation_s_median != null).map(d => ({
...d, med: d.deviation_s_median / 60, lo: (d.deviation_s_p25 ?? d.deviation_s_median) / 60,
hi: (d.deviation_s_p75 ?? d.deviation_s_median) / 60,
band: band(d.on_time_share_of_observed, d.observed)}));
if (stops.length === 0) return html`<h3>Stop by stop</h3><p class="empty">No stop on this route has enough departures yet.</p>`;
const chart = ch.dotRange(stops, {
label: "name", mid: "med", lo: "lo", hi: "hi", band: "band", window: [-1, 5],
text: d => d.band.key === "none" ? "–" : pct(d.on_time_share_of_observed),
axisLabel: "Minutes late (negative: early)",
ariaLabel: "Typical departure at each timepoint stop, in minutes late",
describe: stops.map(d => `${d.name}: ${lateness(d.deviation_s_median)}, ${pct(d.on_time_share_of_observed)} on time`).join("; "),
tip: d => `${d.name}\nTypically ${lateness(d.deviation_s_median)}\n${pct(d.on_time_share_of_observed)} on time (${plural(d.observed, "departure")} seen)`,
onPoint: d => linked.map?.highlightStop(d?.stop_id ?? null),
});
linked.chart = chart;
return html`<h3>Stop by stop</h3>
${ch.figure({
note: "Each mark shows the typical departure at that stop, and the gray bar covers the middle half of all departures there. Anything inside the shaded band counts as on time. The figure at the right is the share of departures that were on time.",
chart,
methods: "on-time-performance",
tableLabel: "Show the stops as a table",
table: ch.table(stops, {
caption: "On-time figures for each timepoint stop on this route",
columns: [
{key: "name", label: "Stop", sortValue: d => stops.indexOf(d)},
{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},
{key: "band", label: "Band", format: b => bandTag(b), sortValue: d => d.band.rank * 10 + (d.on_time_share_of_observed ?? 0)},
],
}),
})}`;
}
trendChart = (s) => {
const days = withDates(s.daily ?? []).filter(d => d.on_time_share_of_observed != null);
if (days.length < 2) return html`<h3>Day by day</h3>${emptyNote("one-day")}`;
return html`<h3>Day by day</h3>${ch.figure({
chart: ch.trend(days, {
y: "on_time_share_of_observed", format: v => pct(v), yLabel: "On time",
ariaLabel: "Share of this route's departures on time, by service day",
describe: `From ${longDate(days[0].day)} to ${longDate(days.at(-1).day)}. 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)} on time\n${pct(d.observed_share)} of departures seen`,
}),
tableLabel: "Show the days as a table",
table: ch.table([...days].reverse(), {
caption: "This route's on-time share 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)},
{key: "observed_share", label: "Departures seen", align: "right", format: v => pct(v)},
],
}),
})}`;
}html`<p class="note">We calculate these figures ourselves from Island Transit's public data, so
they aren't official Island Transit numbers. The <a href="methods.html">Methods</a> page
explains how, and you can download <a href="${DATA_RAW_URL}/ontime/recent.json">the data behind this page</a>.</p>`