-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.js
428 lines (399 loc) · 16.1 KB
/
nodes.js
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
var width = 960,
height = 750;
var projection = d3.geoMercator()
.scale(4000000)
.translate([width / 2, height / 2])
var svg = d3.select("body").select("#svgContainer").append("svg")
.attr("width", width)
.attr("height", height)
const toolTipDiv = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0)
let keepHeatMapOn = true
document.getElementById('overlayPeople').checked = true
document.getElementById('overlayPeople').onclick = function () {
svg.selectAll(".studentDots")
.style("opacity", this.checked ? .75 : 0)
}
document.getElementById("colorPicker").value = "#00FF00"
document.getElementById('overlayHeat').checked = true
document.getElementById('overlayHeat').onclick = function () {
keepHeatMapOn = !keepHeatMapOn
}
document.getElementById("colorPicker").addEventListener("change", function () {
svg.selectAll(".studentDots")
.style("fill", event.target.value)
}, false)
let center = []
let g = d3.json("map.geojson", function (error, NYC_MapInfo) {
// after loading geojson, use d3.geo.centroid to find out
// where you need to center your map
center = d3.geo.centroid(NYC_MapInfo);
projection.center(center);
console.log("let g = ", center)
// now you can create new path function with
// correctly centered projection
var path = d3.geo.path().projection(projection);
// and finally draw the actual polygons
svg.selectAll("path")
.data(NYC_MapInfo.features)
.enter()
.append("path")
.attr("d", path);
});
let h = d3.json("map2.geojson", function (error, map) {
var path = d3.geo.path().projection(projection)
console.log("let h = [center]", center)
});
Promise.all([g, h]).then(function (values) {
let result = values[0]
let resultb = values[1]
center = d3.geoCentroid(result);
console.log("promises all center", center)
projection.center(center)
// now you can create new path function with
// correctly centered projection
var path = d3.geoPath().projection(projection);
// and finally draw the actual polygons
svg.selectAll("path")
.data(result.features)
.enter()
.append("path")
.attr("d", path)
.attr("id", function (d) { return d.properties['name']; })
.style("fill", function (d) { if (d.properties['name'] === 'Worcester Polytechnic Institute') { return "none"; } else { return "#00001f4f"; } })
.style("stroke", function (d) { if (d.properties['name'] === 'Worcester Polytechnic Institute') { return "red"; } else { return "grey"; } })
.on("mouseover", function () {
const buildingName = this.id
if (buildingName !== "") {
toolTipDiv.transition()
.duration(200)
.style("opacity", .9)
toolTipDiv.html(`${buildingName === "West Street Lot" ? "Off Campus or at Home" : buildingName}`)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
}
})
.on("mouseout", () => {
toolTipDiv.transition()
.duration(500)
.style("opacity", 0)
})
d3.csv("output.csv").then((data) => {
var myColor = d3.scaleLinear()
.range(["white", "steelblue"])
.domain([1, 5])
let times = Object.keys(data[0])
times = times.filter((a) => a !== "Timestamp" && !a.includes("Other"))
times.forEach((d, i) => {
times[i] = d
})
let locationAndTimeMap = {}
for (let i = 0; i < data.length; i++) {
const currentResponse = data[i]
for (key in currentResponse) {
if (key.includes("Other") || key === "Timestamp") {
continue
}
const currentBuilding = currentResponse[key]
if (currentBuilding in locationAndTimeMap) {
if (key in locationAndTimeMap[currentBuilding]) {
locationAndTimeMap[currentBuilding][key] = locationAndTimeMap[currentBuilding][key] + 1
} else {
locationAndTimeMap[currentBuilding][key] = 1
}
} else {
const toMake = {}
toMake[key] = 1
locationAndTimeMap[currentBuilding] = toMake
}
}
}
buildings_info = []
for (key in locationAndTimeMap) {
time_quant_pair = []
for (v in times) {
if (locationAndTimeMap[key][times[v]] > 0) {
time_quant_pair.push([v, locationAndTimeMap[key][times[v]]]);
} else {
time_quant_pair.push([v, 0])
}
}
buildings_info.push([key, time_quant_pair])
}
function updateTheMap(buildings_info, i) {
for (building in buildings_info) {
try {
const color = keepHeatMapOn ? myColor(buildings_info[building][1][i][1]) : "#00001f4f"
d3.selectAll("[id='" + buildings_info[building][0] + "']")
.style("fill", color)
} catch {
}
}
}
function generateRandomXorYForDataTime(destinationName, isX) {
if (destinationName === "Off campus or at home") {
destinationName = "West Street Lot"
} else if (destinationName === "Fuller Apartments") {
// Close enough
destinationName = "Schussler Lot"
} else if (destinationName === "Stoddard Complex") {
destinationName = "Hackfield Lot"
} else if (destinationName === "Fountain") {
const box = d3.select("[id='" + "Higgins Laboratories" + "']").node().getBBox()
const startingVal = isX ? box.x : box.y
return startingVal + (isX ? 10 : -10)
} else if (destinationName === "Faraday") {
destinationName = "Farady"
} else if (destinationName === "Other or not listed") {
return 0
}
const box = d3.select("[id='" + destinationName + "']").node().getBBox()
const startingVal = isX ? box.x : box.y
let randVal = (Math.random() * box.width) + startingVal
return randVal
}
function getTimeKeyFromIndex(index) {
const timeKeys = ["Where are you from 8am to 9am on Monday's?", "9am to 10am?", "10am to 11am?", "11am to 12pm?", "12pm to 1pm?", "1pm to 2pm?"
, "2pm to 3pm?", "3pm to 4pm?", "4pm to 5pm?", "5pm to 6pm?", "6pm to 7pm?", "7pm to 8pm?"]
return timeKeys[index]
}
let previousI = 0
function updateDots(i) {
if (i === previousI + 1) {
previousI = i
const timeKey = getTimeKeyFromIndex(i)
d3.selectAll(".studentDots")
.transition()
.duration(2000)
.attr("cx", (d, index) => {
const currentLocation = d[getTimeKeyFromIndex(i - 1)]
const destinationName = d[timeKey]
// Don't move if we're in the same building
if (currentLocation !== destinationName) {
return generateRandomXorYForDataTime(destinationName, true)
} else {
return document.getElementById(index).getAttribute("cx")
}
})
.attr("cy", (d, index) => {
const currentLocation = d[getTimeKeyFromIndex(i - 1)]
const destinationName = d[timeKey]
if (currentLocation !== destinationName) {
return generateRandomXorYForDataTime(destinationName, false)
} else {
return document.getElementById(index).getAttribute("cy")
}
})
} else if (i === 0) {
document.getElementById("currentTimeText").innerHTML = "Current Time: 8am"
//Initial case where we draw the starting dots
svg.selectAll(".studentDots")
.data(data)
.enter()
.append("circle")
.attr("cx", (d) => {
const destinationName = d[getTimeKeyFromIndex(0)]
return generateRandomXorYForDataTime(destinationName, true)
})
.attr("cy", (d) => {
const destinationName = d[getTimeKeyFromIndex(0)]
return generateRandomXorYForDataTime(destinationName, false)
})
.attr("r", 5)
.attr("data-currentLocation", (d) => {
return d["1pm to 2pm?"]
})
.attr("class", "studentDots")
.attr("id", (data, index) => index)
.style("fill", document.getElementById("colorPicker").value)
.style("opacity", ".75")
.style("stroke", "black")
.on("mouseover", function () {
this.style.cursor = "pointer"
})
.on("click", function () {
const currentFill = this.style.fill
if (currentFill === "red")
this.style.fill = document.getElementById("colorPicker").value
else
this.style.fill = "red"
})
}
}
let last_paused = 0;
let interruptPlay = false
function startDrawingMap() {
document.getElementById("playButton").innerHTML = "❚❚"
document.getElementById("playButton").onclick = stopDrawingMap
interruptPlay = false
const duration = 24000;
let i = 0;
timer = d3.timer((elapsed) => {
// I think this should be 11?
let paused_value = last_paused + elapsed
i = Math.floor(paused_value * 11 / duration)
const timeKey = getTimeKeyFromIndex(i)
const timeFormatted = `${timeKey.substring(0, 4)}`
document.getElementById("currentTimeText").innerHTML = `Current Time: ${timeFormatted}`
updateTheMap(buildings_info, i)
updateDots(i)
if (paused_value > duration || interruptPlay) {
last_paused = paused_value
timer.stop();
}
});
}
function stopDrawingMap() {
interruptPlay = true
document.getElementById("playButton").onclick = startDrawingMap
document.getElementById("playButton").innerHTML = "►"
}
document.getElementById("playButton").onclick = startDrawingMap
})
roads(resultb, center)
})
/*
* BEGIN HEATMAP CODE
*/
d3.csv("responses.csv").then((data) => {
let locationAndTimeMap = {}
for (let i = 0; i < data.length; i++) {
const currentResponse = data[i]
for (key in currentResponse) {
if (key.includes("Other") || key === "Timestamp") {
continue
}
const currentBuilding = currentResponse[key]
if (currentBuilding in locationAndTimeMap) {
if (key in locationAndTimeMap[currentBuilding]) {
locationAndTimeMap[currentBuilding][key] = locationAndTimeMap[currentBuilding][key] + 1
} else {
locationAndTimeMap[currentBuilding][key] = 1
}
} else {
const toMake = {}
toMake[key] = 1
locationAndTimeMap[currentBuilding] = toMake
}
}
}
// set the dimensions and margins of the graph
const margin = { top: 30, right: 30, bottom: 30, left: 140 },
width = 480 - margin.left - margin.right,
height = 712.5 - margin.top - margin.bottom;
// append the svg object to the body of the page
const svg = d3.select("#my_dataviz")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Labels of row and columns
let times = Object.keys(data[0])
times = times.filter((a) => a !== "Timestamp" && !a.includes("Other"))
times.forEach((d, i) => {
let formattedString = d.replace("?", "")
formattedString = formattedString.split("to")[0]
if (i === 0) {
formattedString = "8am"
}
times[i] = formattedString
})
const locations = Object.keys(locationAndTimeMap)
// Build X scales and axis:
var x = d3.scaleBand()
.range([0, width])
.domain(times)
.padding(0.01);
svg.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(x))
svg.selectAll("text")
.attr("transform", "rotate(-90)")
.attr("dx", "-2em")
.attr("dy", "-.5em")
// Build X scales and axis:
var y = d3.scaleBand()
.range([height, 0])
.domain(locations)
.padding(0.01);
svg.append("g")
.call(d3.axisLeft(y));
//Tooltip
var Tooltip = d3.select("#tooltipHeatmap")
.append("div")
.style("opacity", 0)
.attr("class", "tooltipHeatmap")
.style("background-color", "white")
.style("border", "solid")
.style("border-width", "2px")
.style("border-radius", "5px")
.style("padding", "5px")
.style("position", "absolute")
.style("width", width)
// Build color scale
var myColor = d3.scaleLinear()
.range(["WhiteSmoke", "steelblue"])
.domain([1, 5])
let heat = d3.entries(locationAndTimeMap)
let heatmap = [];
for (h in heat) {
for (v in heat[h].value) {
heatmap.push([heat[h].key, v, heat[h].value[v]])
}
}
for (h in heatmap) {
let formatTime = heatmap[h][1].replace("?", "")
formatTime = formatTime.split("to")[0]
if (formatTime[0] === 'W') {
formatTime = "8am"
}
heatmap[h][1] = formatTime;
}
svg.selectAll("g")
.data(heatmap, function (d) { return d; })
.enter()
.append("rect")
.attr("x", function (d) { return x(d[1]) })
.attr("y", function (d) { return y(d[0]) })
.attr("width", x.bandwidth())
.attr("height", y.bandwidth())
.style("fill", d => myColor(d[2]))
.on("mouseover", function (d) {
var g = d3.select(this);
g.style("stroke", "black")
Tooltip
.classed('info', true)
.style("opacity", 1)
.attr('x', 20)
.attr('y', 500)
.text(d[2] + (d[2] === 1 ? " Respondent " : " Respondents ") + (d[2] === 1 ? "was " : "were ") + "in " + d[0] + " at " + d[1]);
})
.on("mouseout", function () {
// Remove the info text on mouse out.
let g = d3.select(this)
g.style("stroke", "none")
Tooltip.text("");
Tooltip.style("opacity", 0)
})
.on("mousemove", function (d) {
Tooltip
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 50) + "px")
})
})
function roads(result, center) {
console.log("center", center)
var path = d3.geoPath().projection(projection)
svg.selectAll("g")
.append("g")
.data(result.features)
.enter()
.append("path")
.attr("d", function (d) { return path(d) })
.attr("stroke", "black")
.attr("fill", "none")
}