-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_subwayMap_simple.html
298 lines (259 loc) · 10.4 KB
/
test_subwayMap_simple.html
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<html>
<head>
<title>Test of the Subway Map SVG</title>
<style>
g{
pointer-events: all;
}
path{
pointer-events: all;
}
}
</style>
</head>
<body bgcolor=white>
<br>
<br>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var diameter = 960;
var stationText = d3.select("body").append("p")
.classed("selectionName", true)
.style("padding-left", "5em")
.style("font-weight", "bold")
.style("font-size", "2em");
stationText.text("(click to select)"); //   (non-breaking space) = \u00A0
var svg = d3.select("body").append("svg")
.attr("width", diameter)
.attr("height", diameter)
.attr("class", "map");
// a rect to eat the selection clicks inside the svg so that they nullify
var clickEater = svg.append("rect")
.attr("width", diameter)
.attr("height", diameter)
.attr("class", "selectionClickEater")
.style("fill", "white");
var mapScale = 0.5; // max x is 1147 and max y is 1039
var stationSize = 8;
var undergroundOpacity = 0.4;
var aboveGroundOpacity = 0.4;
var aboveGroundColor = "gray"
var blueLine = svg.append("g").attr("class", "line blue").data(["Blue Line"]);
drawSubwayLine("processing/line_blue.json", blueLine, mapScale, "blue", undergroundOpacity);
var orangeLine = svg.append("g").attr("class", "line orange").data(["Orange Line"]);
drawSubwayLine("processing/line_orange.json", orangeLine, mapScale, "orange", undergroundOpacity);
var greenLine = svg.append("g").attr("class", "line green").data(["Green Line"]);
drawSubwayLine("processing/line_green.json", greenLine, mapScale, "green", undergroundOpacity);
drawSubwayLine("processing/line_greenB.json", greenLine, mapScale, aboveGroundColor, aboveGroundOpacity);
drawSubwayLine("processing/line_greenC.json", greenLine, mapScale, aboveGroundColor, aboveGroundOpacity);
drawSubwayLine("processing/line_greenD.json", greenLine, mapScale, aboveGroundColor, aboveGroundOpacity);
drawSubwayLine("processing/line_greenE_underground.json", greenLine, mapScale, "green", undergroundOpacity);
drawSubwayLine("processing/line_greenE.json", greenLine, mapScale, aboveGroundColor, aboveGroundOpacity);
var redLine = svg.append("g").attr("class", "line red").data(["Red Line"]);
drawSubwayLine("processing/line_red.json", redLine, mapScale, "red", undergroundOpacity);
drawSubwayLine("processing/line_redB.json", redLine, mapScale, "red", undergroundOpacity);
drawSubwayLine("processing/line_redM.json", redLine, mapScale, aboveGroundColor, aboveGroundOpacity);
redLine.on("mouseover", function(d){
var xPos = 100 * mapScale;
var yPos = 1000 * mapScale
//d3.select(this).append("text").classed("mouseName", true).attr("font-weight", "bold").attr("font-size", "1em")
svg.append("text").classed("mouseName", true).attr("font-weight", "bold").attr("font-size", "1em")
.attr("text-anchor", "left")
.attr("x", xPos)
.attr("y", yPos)
.text(d);
var lin = d3.selectAll(".line.red").selectAll(".subwayPath")
.style("stroke-width", 4);
});
redLine.on("mouseout", function(d){
d3.selectAll(".mouseName").remove();
var lin = d3.selectAll(".line.red").selectAll(".subwayPath")
.style("stroke-width", 3);
});
greenLine.on("mouseover", function(d){
var xPos = 100 * mapScale;
var yPos = 1000 * mapScale
//d3.select(this).append("text").classed("mouseName", true).attr("font-weight", "bold").attr("font-size", "1em")
svg.append("text").classed("mouseName", true).attr("font-weight", "bold").attr("font-size", "1em")
.attr("text-anchor", "left")
.attr("x", xPos)
.attr("y", yPos)
.text(d);
var lin = d3.selectAll(".line.green").selectAll(".subwayPath")
.style("stroke-width", 4);
});
greenLine.on("mouseout", function(d){
d3.selectAll(".mouseName").remove();
var lin = d3.selectAll(".line.green").selectAll(".subwayPath")
.style("stroke-width", 3);
});
orangeLine.on("mouseover", function(d){
var xPos = 100 * mapScale;
var yPos = 1000 * mapScale
//d3.select(this).append("text").classed("mouseName", true).attr("font-weight", "bold").attr("font-size", "1em")
svg.append("text").classed("mouseName", true).attr("font-weight", "bold").attr("font-size", "1em")
.attr("text-anchor", "left")
.attr("x", xPos)
.attr("y", yPos)
.text(d);
var lin = d3.selectAll(".line.orange").selectAll(".subwayPath")
.style("stroke-width", 4);
});
orangeLine.on("mouseout", function(d){
d3.selectAll(".mouseName").remove();
var lin = d3.selectAll(".line.orange").selectAll(".subwayPath")
.style("stroke-width", 3);
});
blueLine.on("mouseover", function(d){
var xPos = 100 * mapScale;
var yPos = 1000 * mapScale
//d3.select(this).append("text").classed("mouseName", true).attr("font-weight", "bold").attr("font-size", "1em")
svg.append("text").classed("mouseName", true).attr("font-weight", "bold").attr("font-size", "1em")
.attr("text-anchor", "left")
.attr("x", xPos)
.attr("y", yPos)
.text(d);
var lin = d3.selectAll(".line.blue").selectAll(".subwayPath")
.style("stroke-width", 4);
});
blueLine.on("mouseout", function(d){
d3.selectAll(".mouseName").remove();
var lin = d3.selectAll(".line.blue").selectAll(".subwayPath")
.style("stroke-width", 3);
});
d3.json("processing/station_map.json", function(error, d) {
var stationsData = d;
var newStations = svg.selectAll(".node")
.data(stationsData)
.enter()
.append("g")
.attr("class", "node")
.classed("aboveGround", function(d){ return parseInt(d.underground) == 0 })
.classed("underGround", function(d){ return parseInt(d.underground) != 0 })
.attr("transform", function(d) { return "translate(" + mapScale * d.x + "," + mapScale * d.y + ")"; });
var stationDots = newStations.append("circle")
.attr("r", function(d){
// make above ground stations a smaller radius
if(parseInt(d.underground)){
return mapScale * stationSize;
}else{
return 0.25 * mapScale * stationSize;
}
})
.attr("class", function(d){return (d.name).replace(/[\s/.]/g, '')}) // the station names include some annoying characters for setting the class, remove white space, forward slahses, and periods
.style("stroke", "black")
.style("stroke-width", 1)
.style("fill", "gray");
var hitBoxSize = (mapScale * stationSize) * 4;
var undergrounds = newStations.filter(".underground");
var stationHitBoxes = undergrounds.append("rect")
.attr("width", hitBoxSize)
.attr("height", hitBoxSize)
.attr("x", -(hitBoxSize)/2)
.attr("y", -(hitBoxSize)/2)
.attr("class", "stationHitBox")
.style("visibility", "hidden");
undergrounds.on("mouseover", function(d){
//var xPos = d3.mouse(this)[0]; // this often looks pretty poor on top of the subway lines
//var yPos = d3.mouse(this)[1];
var xPos = 100 * mapScale;
var yPos = 1000 * mapScale
//d3.select(this).append("text").classed("mouseName", true).attr("font-weight", "bold").attr("font-size", "1em")
svg.append("text").classed("mouseName", true).attr("font-weight", "bold").attr("font-size", "1em")
.attr("text-anchor", "left")
.attr("x", xPos)
.attr("y", yPos)
.text(d.name);
var className = (d.name).replace(/[\s/.]/g, '');
var sel = d3.selectAll("."+className)
sel.style("fill", "LightSkyBlue");
})
.on("mouseout", function(d){
d3.selectAll(".mouseName").remove();
d3.selectAll("."+(d.name).replace(/[\s/.]/g, '')).style("fill", "gray");;
})
clickEater.on("click", function(d){
// zero out selection
d3.selectAll('.selected')
.classed('selected', false);
d3.selectAll(".selectionName").text("(click to select)");
updateSelection();
});
d3.selectAll(".line").on("click", function(d){
alreadySelected = this.classList.contains("selected");
// zero out selection
d3.selectAll('.selected')
.classed('selected', false);
d3.selectAll(".selectionName").text("(click to select)");
if(!alreadySelected) {
this.classList.add('selected');
var myData = d3.select(this).data();
d3.selectAll(".selectionName").text("Selected: " + myData);
}
updateSelection();
});
undergrounds.on("click", function(d){
alreadySelected = this.classList.contains("selected");
// zero out selection
d3.selectAll('.selected')
.classed('selected', false);
d3.selectAll(".selectionName").text("(click to select)");
if(!alreadySelected) {
this.classList.add('selected');
var myData = d3.select(this).select("circle").data();
d3.selectAll(".selectionName").text("Selected: " + myData[0].name);
}
updateSelection();
});
});
function updateSelection(){
lines = d3.selectAll('.line');
selectedLine = lines.filter('.selected');
lines.selectAll(".subwayPath")
.style("stroke-width", 3)
.style("stroke-opacity", 0.4);
selectedLine.selectAll(".subwayPath")
.style("stroke-width", 4)
.style("stroke-opacity", 0.8);
stations = d3.selectAll('.node').filter('.underground');
selectedStation = stations.filter('.selected');
stations.selectAll('circle')
.attr("r", mapScale * stationSize);
selectedStation.selectAll('circle')
.attr("r", 2 * mapScale * stationSize);
}
function drawSubwayLine(jsonFile, svgContainer, mapScale, color, opacity){
// line generator for nice, smooth lines
// var line = d3.svg.line()
// .interpolate("bundle")
// .tension(.85)
// .x(function(d) { return mapScale * d.x; })
// .y(function(d) { return mapScale * d.y; });
var line = d3.svg.line()
.interpolate("cardinal")
.x(function(d) { return mapScale * d.x; })
.y(function(d) { return mapScale * d.y; });
var lineGroup = svgContainer.append('g');
d3.json(jsonFile, function(error, lineData) {
// hidden path to increase hitbox size for selection
lineGroup.append("path")
.attr("d", line(lineData))
.style("stroke-width", 11)
.style("fill", "none")
.style('visiblity', 'hidden')
.style("pointer-events", "stroke");
// visible path
var links = lineGroup.append("path")
.attr("class", "subwayPath")
.attr("d", line(lineData))
.style("stroke", color)
.style("stroke-width", 3)
.style("fill", "none")
.style("stroke-opacity", opacity)
.style("pointer-events", "stroke");
});
//function select
}
</script>
</body>
</html>