-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
282 lines (216 loc) · 7.77 KB
/
Copy pathscript.js
File metadata and controls
282 lines (216 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
let svg;
let backgroundImage = null;
let routes = null;
let routeData = null;
let selectedRoute = null;
let routeEtymology = {};
// Load the external SVG file
Promise.all([
// d3.xml("images/map_new.svg"),
d3.xml("images/map-routes.svg"),
d3.csv("data/routes_03012026.csv"),
// Wrap fetch in a fail-safe
fetch("data/etymology.json")
.then(res => {
if (!res.ok) throw new Error("Failed to load");
return res.json();
})
.catch(err => {
console.warn("Etymology JSON failed to load:", err);
return {}; // return empty object instead of rejecting
})
]).then(([svgData, csvData, jsonData]) => {
routeEtymology = jsonData;
console.log("Route etymology loaded:", routeEtymology.acela);
// const container = d3.select("#map-container");
const container = d3.select("#map-overlay");
// Append the SVG element to the container
container.node().append(svgData.documentElement);
// Select the loaded SVG
// svg = d3.select("#map-container svg")
// .attr("id", "amtrak-svg")
// .attr("preserveAspectRatio", "xMidYMid meet");
svg = d3.select("#map-overlay svg")
.attr("id", "amtrak-svg")
.attr("preserveAspectRatio", "xMidYMid meet")
.attr("viewBox", "0 0 4447 3473");
console.log("SVG loaded:", svg);
// Slider: Select image and chnage opacity of underlying image
// backgroundImage = svg.select("image");
backgroundImage = d3.select("#map-base");
// ... set initial opacity
const initialOpacity = d3.select("#opacity-slider").property("value");
if (backgroundImage) {
// backgroundImage.attr("opacity", initialOpacity);
backgroundImage.style("opacity", initialOpacity);
}
console.log("Found image:", backgroundImage.node());
// Toggle: Show/hide routes
routes = svg.selectAll("g[id^='route']");
console.log("Routes found:", routes.size());
let selectedRoute = null;
// Load routes csv for tooltips
// Convert numeric columns if needed
csvData.forEach(d => {
d.n_stations_approx = +d.n_stations_approx;
d.time = +d.time;
d.miles_approx = +d.miles_approx;
});
// Index by route for fast access
routeData = {};
csvData.forEach(d => {
routeData[d.route] = d;
});
console.log("Routes loaded:", routeData);
setupSlider();
setupToggle();
setupRouteClick();
setupResetClick();
// End
}).catch(err => console.error("Error loading SVG or CSV:", err));
// ----------------- FUNCTIONS -----------------
function setupSlider() {
d3.select("#opacity-slider").on("input", function () {
const val = this.value;
if (backgroundImage) {
// backgroundImage.attr("opacity", val);
backgroundImage.style("opacity", val);
}
});
}
function setupToggle() {
d3.select("#toggle-routes").on("change", function () {
const checked = this.checked;
if (routes) {
routes.attr("display", checked ? null : "none");
}
});
}
function setupRouteClick() {
routes.style("cursor", "pointer");
routes.on("click", function (event) {
event.stopPropagation(); // prevent reset click
const clicked = d3.select(this);
selectedRoute = this.id;
// // fade all
routes.attr("opacity", 0.1);
// routes.attr("stroke", "#b0b0b0");
// // highlight clicked
clicked.attr("opacity", 1);
// clicked.attr("stroke", null);
// show tooltip
selectedRouteName = selectedRoute.replace(/^route_/,""); // remove prefix
showTooltip(routeData[selectedRouteName], event.pageX, event.pageY);
})
};
function setupResetClick() {
svg.on("click", function () {
selectedRoute = null;
routes.attr("opacity", 1);
routes.attr("stroke", null);
hideTooltip();
});
}
// Show tooltip at mouse position
function showTooltip(info, x, y) {
// Remove any existing tooltip
d3.select(".tooltip").remove();
// Create tooltip container
const tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("left", `${x + 20}px`)
.style("top", `${y + 10}px`);
// =========================
// HEADER BUTTONS
// =========================
const header = tooltip.append("div")
.attr("class", "tooltip-header")
.style("margin-bottom", "5px");
// Radio input: Route Info
header.append("input")
.attr("type", "radio")
.attr("name", "tooltip-panel")
.attr("id", "panel-info")
.attr("checked", true)
.on("change", () => showPanel("info"));
header.append("label")
.attr("for", "panel-info")
.text("Route info")
.attr("class", "tooltip-radio-label");
// Radio input: Etymology
header.append("input")
.attr("type", "radio")
.attr("name", "tooltip-panel")
.attr("id", "panel-etymology")
.on("change", () => showPanel("etymology"));
header.append("label")
.attr("for", "panel-etymology")
.text("Etymology")
.attr("class", "tooltip-radio-label");
// =========================
// PANELS
// =========================
const panelInfo = tooltip.append("div").attr("class", "tooltip-panel info");
const panelEtym = tooltip.append("div").attr("class", "tooltip-panel etymology");
// --- Route Info Table ---
const fields = [
{key: "route", label: "Route"},
{key:"origin", label:"Origin"},
{key:"destination", label:"Destination"},
{key:"frequency", label:"Frequency"},
{key:"miles_approx", label:"# miles", prefix: "~", suffix:" mi"},
{key:"n_stations_approx", label:"Stations"},
{key:"stop_summary", label:"Stop summary"},
{key:"time_label", label:"Time"},
{key:"link", label:"More info"}
];
// Build table
const tbody = panelInfo.append("table").append("tbody");
// Only the fields we want
fields.forEach(f => {
if (info[f.key] !== undefined && info[f.key] !== "") {
let value = (f.prefix || "") + info[f.key] + (f.suffix || "");
// Capitalize the route name
if (f.key === "route" || f.key === "route_name") {
value = value.replace(/_/g, " ");
value = value.toUpperCase(); // all uppercase
// Or to capitalize just first letters of words:
// value = value.replace(/\b\w/g, c => c.toUpperCase());
}
// If it's a link, wrap in <a>
// if (f.key === "link") {
// value = `<a href="${info[f.key]}" target="_blank">${info[f.key]}</a>`;
// }
if (f.key === "link" && info[f.key]) {
value = `<a href="${info[f.key]}" target="_blank">Link</a>`;
}
tbody.append("tr").html(`
<td>${f.label}</td>
<td>${value}</td>
`);
}
});
// --- Etymology ---
const routeName = info.route;
const etymText = routeEtymology[routeName] || "No etymology available.";
panelEtym.append("p").html(etymText);
// --- Panel Toggle ---
function showPanel(name) {
if (name === "info") {
panelInfo.style("display", "block");
panelEtym.style("display", "none");
} else {
panelInfo.style("display", "none");
panelEtym.style("display", "block");
}
}
// Default to showing Route Info
showPanel("info");
// infoBtn.on("click", () => showPanel("info"));
// etymBtn.on("click", () => showPanel("etymology"));
}
// Hide tooltip
function hideTooltip() {
d3.select(".tooltip").remove();
}