-
Notifications
You must be signed in to change notification settings - Fork 94
/
index.html
474 lines (408 loc) · 13.1 KB
/
index.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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<!DOCTYPE html>
<html>
<head>
<title>Game of Thrones: Character Time Per Episode - Scatter</title>
<meta charset="UTF-8">
<meta name="description" content="Game of Thrones: Character Time Per Episode - Scatter">
<meta name="author" content="Jeffrey Lancaster">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../style.css">
<style>
text.select{
display: block!important;
font-size: 12px!important;
fill: #000!important;
}
.overlay text{
font-family: serif;
font-size: 20px;
text-anchor: middle;
}
span{
font-family: sans-serif;
font-size: 12px;
margin-left: 20px;
}
</style>
</head>
<body>
<p>
<select id="characters" style="display: none;"></select>
<span>
Show Overlay: <input type="checkbox" style="display: inline-block;" id="overlay" name="overlay">
</span>
</p>
<svg></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
/* HELPFUL FUNCTIONS */
// to convert scene start/end times into seconds
function sec(timeString){
var sec = 0;
if (timeString.length == 0) return sec;
var splitArray = timeString.split(":");
sec = 3600*parseFloat(splitArray[0])+60*parseFloat(splitArray[1])+parseFloat(splitArray[2]);
return sec;
}
// to convert seconds into hh:mm:ss
function secondsToHMS(d) {
var date = new Date(null);
date.setSeconds(d); // specify value for SECONDS here
return date.toISOString().substr(11, 8);
}
// to dedpulicate an array
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
// from: http://bl.ocks.org/eesur/4e0a69d57d3bfc8a82c2
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
d3.selection.prototype.moveToBack = function() {
return this.each(function() {
var firstChild = this.parentNode.firstChild;
if(firstChild) {
this.parentNode.insertBefore(this, firstChild);
}
});
};
var config = {
"title":"Character Time Per Episode - Scatter",
"width":960,
"height":700,
"margin":{
"top":20,
"right":100,
"bottom":200,
"left":40
},
"r": 10
}
/* IMPORT DATA */
d3.queue()
.defer(d3.json, '../data/episodes.json')
.defer(d3.json, '../data/characters-include.json')
.await(ready);
function ready(error, e, inc) {
if (error) throw error;
console.log("now that the files are loaded... do magic.");
/* CONFIG SETUP */
d3.select("svg")
.attr("width", config.width)
.attr("height", config.height)
/* END CONFIG SETUP */
// put all scenes in one array
var scenes = e.episodes.reduce(function(acc, val, ind){
var scene = val.scenes.reduce(function(a, v, i){
var sceneLength = Math.abs(sec(v.sceneEnd) - sec(v.sceneStart));
var s = Object.assign({}, v, {"seasonNum":val.seasonNum, "episodeNum":val.episodeNum, "sceneLength":sceneLength, "episodeIndex":ind});
return a.concat(s);
}, []);
return [...acc, ...scene];
}, []);
// put all episodes in an array
var episodes = e.episodes.filter(function(val, ind){
return val.scenes.length > 0
})
.map(function(val, ind){
return {"title":val.episodeTitle, "seasonNum":val.seasonNum, "episodeNum":val.episodeNum, "episodeIndex":ind};
})
// then make an array of objects {x, y, ybar} with 0 count for values
var episodesObj = episodes.map(function(val, ind){
return Object.assign({}, {"x":ind, "y":0});
});
// put all characters in one array, make object to count character time
var characters = scenes.reduce(function(acc, val, ind){
var c = val.characters.reduce(function(a, v, i){
return a.concat(v.name);
}, []);
return [...acc, ...c];
}, [])
.filter(onlyUnique)
.map(function(cur, ind){
// have to do parse/stringify because of deep clone of episodesObj
return Object.assign({}, {
"name":cur,
"last":0,
"dead":false,
"data":JSON.parse(JSON.stringify(episodesObj))
});
});
// go through scenes and add sceneLength values to characters' counts
scenes.forEach(function(val, ind){
val.characters.forEach(function(v, i){
var index = characters.findIndex(function(element){
return element.name == v.name;
});
characters[index].data[val.episodeIndex].y += val.sceneLength;
characters[index].last = val.seasonNum;
if(val.flashback || val.greensight || val.warg){
// ignore the past
} else {
characters[index].dead = (v.alive == false) ? true : false;
}
})
})
// add slope and intercept from calcLinear (below)
characters.forEach(function(val, ind){
val.slope = calcLinear(val.data).slope;
val.intercept = calcLinear(val.data).intercept;
})
// extract the data for the scatter plot
var characterScatter = characters.map(function(val, ind){
return Object.assign({}, {
'name':val.name,
'last':val.last,
'dead':val.dead,
'x':val.slope,
'y':val.intercept
})
})
// build the select
var include = inc.include.filter(function(val, ind){
return val.include
})
.map(function(val, ind){
return val.name
})
.sort();
include.forEach(function(val, ind){
d3.select("#characters")
.append("option")
.text(val);
})
// select behavior
d3.select("#characters")
.style("display", "inline-block")
.style("margin-top", config.margin.top)
.on("change", function(){
// update pattern from: https://bl.ocks.org/ctufts/674ece47de093f6e0cd5af22d7ee9b9b
var value = d3.select(this).property('value').toLowerCase().replace(/([^A-Z0-9])/gi,"");
svg.selectAll(".point").select("path")
.classed("select", false)
.transition()
.duration(500)
.attr("transform", function(d){return `translate(${x(d.x)}, ${y(d.y)}) scale(1) rotate(45)`})
svg.selectAll(".point").select("text")
.classed("select", false)
svg.select("."+value).moveToFront();
svg.select("."+value).select("path")
.classed("select", true)
.transition()
.duration(500)
.attr("transform", function(d){return `translate(${x(d.x)}, ${y(d.y)}) scale(2) rotate(45)`})
svg.select("."+value).select("text")
.classed("select", true)
})
data = characterScatter;
// build the visualization
// based on: https://bl.ocks.org/HarryStevens/be559bed98d662f69e68fc8a7e0ad097
var width = config.width-config.margin.left-config.margin.right,
height = config.height-config.margin.top-config.margin.bottom;
var svg = d3.select("svg")
.attr("width", config.width)
.attr("height", config.height)
.append("g")
.attr("transform", `translate(${config.margin.left}, ${config.margin.top})`);
var x = d3.scaleLinear()
.range([0,width])
var y = d3.scaleLinear()
.range([height,0]);
var z = d3.scaleOrdinal() // or d3.schemeCategory20c between () and no .range
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00", "#FEC574"]);
var xAxis = d3.axisBottom()
.scale(x)
var yAxis = d3.axisLeft()
.scale(y)
y.domain(d3.extent(data, function(d){ return d.y}));
x.domain(d3.extent(data, function(d){ return d.x}));
console.log(height, x(0), y(0))
svg.append("g")
.attr("class", "x axis-show")
.attr("transform", "translate(0," + y(0) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis-show")
.attr("transform", "translate(" + x(0) + ",0)")
.call(yAxis);
svg.append("text")
.attr("transform", "translate(" + x(0) + ",-5)")
.attr("text-anchor", "middle")
.style("font-size", "12px")
.text("y-intercept (sec)")
svg.append("text")
.attr("transform", "translate(" + width + "," + (y(0)-5) + ")")
.attr("text-anchor", "middle")
.style("font-size", "12px")
.text("slope (sec/episode)")
var symbolGenerator = d3.symbol();
var points = svg.selectAll(".point")
.data(data)
.enter()
.append("g")
.attr("class", function(d){return `point ${d.name.toLowerCase().replace(/([^A-Z0-9])/gi,"")}`})
.on("mouseover", function(d){
d3.select(this).moveToFront();
d3.select(this).select("path")
.classed("select", true)
.transition()
.duration(500)
.attr("transform", function(d){return `translate(${x(d.x)}, ${y(d.y)}) scale(2) rotate(45)`})
d3.select(this).select("text")
.classed("select", true)
})
.on("mouseout", function(d){
d3.select(this).moveToBack();
d3.select(this).select("path")
.classed("select", false)
.transition()
.duration(500)
.attr("transform", function(d){return `translate(${x(d.x)}, ${y(d.y)}) scale(1) rotate(45)`})
d3.select(this).select("text")
.classed("select", false)
})
.append("path")
.attr('transform', function(d, i) {
return 'translate(' + x(d.x) + ', ' + y(d.y) + ') rotate(45)';
})
.attr('d', function(d){
symbolGenerator.type(function(){
return (!d.dead) ? d3['symbolCircle'] : d3['symbolCross']
});
return symbolGenerator();
})
.style("fill", function(d){
return z(d.last);
})
.append("svg:title")
.text(function(d,i){
return d.name;
});
svg.selectAll(".point")
.append("text")
.attr("x", function(d){return x(d.x)})
.attr("y", function(d){return y(d.y)})
.attr("dx", `${config.r}px`)
.style("fill", "#ddd")
.style("font-size", "9px")
.style("font-weight", 400)
.style("font-family", "sans-serif")
.text(function(d){
return d.name
})
.style("display", "none");
// make the unscientific overlay
var overlay = svg.append("g")
.attr("class", "overlay")
.style("display", "none");
overlay.append("line")
.attr("class", "regression")
.attr("x1", x(0.35))
.attr("y1", y(-75))
.attr("x2", x(-4))
.attr("y2", y(450));
overlay.append("line")
.attr("class", "regression")
.attr("x1", x(0.35))
.attr("y1", y(-75))
.attr("x2", x(4))
.attr("y2", y(450));
overlay.append("circle")
.attr("class", "regression")
.attr("cx", x(-0.25))
.attr("cy", y(0))
.attr("r", 100)
.style("fill", "none");
overlay.append("text")
.text("Dead")
.attr("x", x(-6))
.attr("y", y(100))
overlay.append("text")
.text("Contenders")
.attr("x", x(0))
.attr("y", y(275))
overlay.append("text")
.text("Dutiful Servants")
.attr("x", x(6))
.attr("y", y(100))
overlay.append("text")
.text("Minor")
.attr("x", x(-1))
.attr("y", y(-50))
// change overlay display based on checkbox
d3.select("#overlay")
.on("change", function(){
var value = d3.select(this).property("checked");
d3.select(".overlay")
.style("display", function(){
return (value) ? "block" : "none";
});
})
// draw the first dot and select the character
document.getElementById("characters").value = "Jon Snow";
d3.select(".jonsnow").select("path")
.classed("select", true)
.attr("transform", function(d){return `translate(${x(d.x)}, ${y(d.y)}) scale(2) rotate(45)`});
d3.select(".jonsnow").select("text")
.classed("select", true);
// Calculate a linear regression from the data, returns an object with two points, where each point is an object with an x and y coordinate
function calcLinear(data){
var minX = d3.min(data, function(d){ return d.x});
var maxX = d3.max(data, function(d){ return d.x});
var minY = d3.min(data, function(d){ return d.y});
var maxY = d3.max(data, function(d){ return d.y});
// SLOPE
// Let n = the number of data points
var n = data.length;
// Get just the points
var pts = [];
data.forEach(function(d,i){
var obj = {};
obj.x = d.x;
obj.y = d.y;
obj.mult = obj.x*obj.y;
pts.push(obj);
});
// Let a equal n times the summation of all x-values multiplied by their corresponding y-values
// Let b equal the sum of all x-values times the sum of all y-values
// Let c equal n times the sum of all squared x-values
// Let d equal the squared sum of all x-values
var sum = 0;
var xSum = 0;
var ySum = 0;
var sumSq = 0;
pts.forEach(function(pt){
sum = sum + pt.mult;
xSum = xSum + pt.x;
ySum = ySum + pt.y;
sumSq = sumSq + (pt.x * pt.x);
});
var a = sum * n;
var b = xSum * ySum;
var c = sumSq * n;
var d = xSum * xSum;
// slope: m = (a - b) / (c - d)
var m = (a - b) / (c - d);
// INTERCEPT
// Let e equal the sum of all y-values
var e = ySum;
// Let f equal the slope times the sum of all x-values
var f = m * xSum;
// y-intercept: g = (e - f) / n
var g = (e - f) / n;
// the equation
// console.log("y = " + m + "x + " + g);
// return an object of two points
return {
ptA : {x: minX, y: m * minX + g},
ptB : {x: maxX, y: m * maxX + g},
slope: m,
intercept: g
}
}
};
</script>
</body>
</html>