-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmovie-network.js
509 lines (425 loc) · 17.8 KB
/
movie-network.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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/* ---------------------------------------------------------------------------
(c) Telefónica I+D, 2013
Author: Paulo Villegas
This script is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------- */
// For MSIE < 9, forget it
function D3notok() {
document.getElementById('sidepanel').style.visibility = 'hidden';
var nocontent = document.getElementById('nocontent');
nocontent.style.visibility = 'visible';
nocontent.style.pointerEvents = 'all';
var t = document.getElementsByTagName('body');
var body = document.getElementsByTagName('body')[0];
body.style.backgroundImage = "url('img/movie-network-screenshot-d.png')";
body.style.backgroundRepeat = "no-repeat";
}
// -------------------------------------------------------------------
// A number of forward declarations. These variables need to be defined since
// they are attached to static code in HTML. But we cannot define them yet
// since they need D3.js stuff. So we put placeholders.
// Highlight a movie in the graph. It is a closure within the d3.json() call.
var selectMovie = undefined;
// Change status of a panel from visible to hidden or viceversa
var toggleDiv = undefined;
// Clear all help boxes and select a movie in network and in movie details panel
var clearAndSelect = undefined;
// The call to set a zoom value -- currently unused
// (zoom is set via standard mouse-based zooming)
var zoomCall = undefined;
// -------------------------------------------------------------------
// Do the stuff -- to be called after D3.js has loaded
function D3ok() {
DEBUG = false;
// In debug mode, ensure there is a console object (MSIE does not have it by
// default). In non-debug mode, ensure the console log does nothing
if( !window.console || !DEBUG ) {
window.console = {};
window.console.log = function () {};
}
// Some constants
var WIDTH = 960,
HEIGHT = 600,
SHOW_THRESHOLD = 2.5;
// Variables keeping graph state
var activeMovie = undefined;
var currentOffset = { x : 0, y : 0 };
var currentZoom = 1.0;
// The D3.js scales
var xScale = d3.scale.linear()
.domain([0, WIDTH])
.range([0, WIDTH]);
var yScale = d3.scale.linear()
.domain([0, HEIGHT])
.range([0, HEIGHT]);
var zoomScale = d3.scale.linear()
.domain([1,6])
.range([1,6])
.clamp(true);
/* .......................................................................... */
// The D3.js force-directed layout
var force = d3.layout.force()
.charge(-320)
.size( [WIDTH, HEIGHT] )
.linkStrength( function(d,idx) { return d.weight; } );
// Add to the page the SVG element that will contain the movie network
var svg = d3.select("#movieNetwork").append("svg:svg")
.attr('xmlns','http://www.w3.org/2000/svg')
.attr("width", WIDTH)
.attr("height", HEIGHT)
.attr("id","graph")
.attr("viewBox", "0 0 " + WIDTH + " " + HEIGHT )
.attr("preserveAspectRatio", "xMidYMid meet");
// Movie panel: the div into which the movie details info will be written
movieInfoDiv = d3.select("#movieInfo");
/* ....................................................................... */
// Get the current size & offset of the browser's viewport window
function getViewportSize( w ) {
var w = w || window;
console.log(w);
if( w.innerWidth != null )
return { w: w.innerWidth,
h: w.innerHeight,
x : w.pageXOffset,
y : w.pageYOffset };
var d = w.document;
if( document.compatMode == "CSS1Compat" )
return { w: d.documentElement.clientWidth,
h: d.documentElement.clientHeight,
x: d.documentElement.scrollLeft,
y: d.documentElement.scrollTop };
else
return { w: d.body.clientWidth,
h: d.body.clientHeight,
x: d.body.scrollLeft,
y: d.body.scrollTop};
}
function getQStringParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
/* Change status of a panel from visible to hidden or viceversa
id: identifier of the div to change
status: 'on' or 'off'. If not specified, the panel will toggle status
*/
toggleDiv = function( id, status ) {
d = d3.select('div#'+id);
console.log( 'TOGGLE', id, d.attr('class'), '->', status );
if( status === undefined )
status = d.attr('class') == 'panel_on' ? 'off' : 'on';
d.attr( 'class', 'panel_' + status );
return false;
}
/* Clear all help boxes and select a movie in the network and in the
movie details panel
*/
clearAndSelect = function (id) {
toggleDiv('faq','off');
toggleDiv('help','off');
selectMovie(id,true); // we use here the selectMovie() closure
}
/* Compose the content for the panel with movie details.
Parameters: the node data, and the array containing all nodes
*/
function getMovieInfo( n, nodeArray ) {
console.log( "INFO", n );
info = '<div id="cover">';
if( n.cover )
info += '<img class="cover" height="300" src="' + n.cover + '" title="' + n.label + '"/>';
else
info += '<div class=t style="float: right">' + n.title + '</div>';
info +=
'<img src="img/close.png" class="action" style="top: 0px;" title="close panel" onClick="toggleDiv(\'movieInfo\');"/>' +
'<img src="img/target-32.png" class="action" style="top: 280px;" title="center graph on movie" onclick="selectMovie('+n.index+',true);"/>';
info += '<br/></div><div style="clear: both;">'
if( n.genre )
info += '<div class=f><span class=l>Genre</span>: <span class=g>'
+ n.genre + '</span></div>';
if( n.director )
info += '<div class=f><span class=l>Directed by</span>: <span class=d>'
+ n.director + '</span></div>';
if( n.cast )
info += '<div class=f><span class=l>Cast</span>: <span class=c>'
+ n.cast + '</span></div>';
if( n.duration )
info += '<div class=f><span class=l>Year</span>: ' + n.year
+ '<span class=l style="margin-left:1em;">Duration</span>: '
+ n.duration + '</div>';
if( n.links ) {
info += '<div class=f><span class=l>Related to</span>: ';
n.links.forEach( function(idx) {
info += '[<a href="javascript:void(0);" onclick="selectMovie('
+ idx + ',true);">' + nodeArray[idx].label + '</a>]'
});
info += '</div>';
}
return info;
}
// *************************************************************************
d3.json(
'data/movie-network-25-7-3.json',
function(data) {
// Declare the variables pointing to the node & link arrays
var nodeArray = data.nodes;
var linkArray = data.links;
console.log("NODES:",nodeArray);
console.log("LINKS:",linkArray);
minLinkWeight =
Math.min.apply( null, linkArray.map( function(n) {return n.weight;} ) );
maxLinkWeight =
Math.max.apply( null, linkArray.map( function(n) {return n.weight;} ) );
console.log( "link weight = ["+minLinkWeight+","+maxLinkWeight+"]" );
// Add the node & link arrays to the layout, and start it
force
.nodes(nodeArray)
.links(linkArray)
.start();
// A couple of scales for node radius & edge width
var node_size = d3.scale.linear()
.domain([5,10]) // we know score is in this domain
.range([1,16])
.clamp(true);
var edge_width = d3.scale.pow().exponent(8)
.domain( [minLinkWeight,maxLinkWeight] )
.range([1,3])
.clamp(true);
/* Add drag & zoom behaviours */
svg.call( d3.behavior.drag()
.on("drag",dragmove) );
svg.call( d3.behavior.zoom()
.x(xScale)
.y(yScale)
.scaleExtent([1, 6])
.on("zoom", doZoom) );
// ------- Create the elements of the layout (links and nodes) ------
var networkGraph = svg.append('svg:g').attr('class','grpParent');
// links: simple lines
var graphLinks = networkGraph.append('svg:g').attr('class','grp gLinks')
.selectAll("line")
.data(linkArray, function(d) {return d.source.id+'-'+d.target.id;} )
.enter().append("line")
.style('stroke-width', function(d) { return edge_width(d.weight);} )
.attr("class", "link");
// nodes: an SVG circle
var graphNodes = networkGraph.append('svg:g').attr('class','grp gNodes')
.selectAll("circle")
.data( nodeArray, function(d){ return d.id; } )
.enter().append("svg:circle")
.attr('id', function(d) { return "c" + d.index; } )
.attr('class', function(d) { return 'node level'+d.level;} )
.attr('r', function(d) { return node_size(d.score || 3); } )
.attr('pointer-events', 'all')
//.on("click", function(d) { highlightGraphNode(d,true,this); } )
.on("click", function(d) { showMoviePanel(d); } )
.on("mouseover", function(d) { highlightGraphNode(d,true,this); } )
.on("mouseout", function(d) { highlightGraphNode(d,false,this); } );
// labels: a group with two SVG text: a title and a shadow (as background)
var graphLabels = networkGraph.append('svg:g').attr('class','grp gLabel')
.selectAll("g.label")
.data( nodeArray, function(d){return d.label} )
.enter().append("svg:g")
.attr('id', function(d) { return "l" + d.index; } )
.attr('class','label');
shadows = graphLabels.append('svg:text')
.attr('x','-2em')
.attr('y','-.3em')
.attr('pointer-events', 'none') // they go to the circle beneath
.attr('id', function(d) { return "lb" + d.index; } )
.attr('class','nshadow')
.text( function(d) { return d.label; } );
labels = graphLabels.append('svg:text')
.attr('x','-2em')
.attr('y','-.3em')
.attr('pointer-events', 'none') // they go to the circle beneath
.attr('id', function(d) { return "lf" + d.index; } )
.attr('class','nlabel')
.text( function(d) { return d.label; } );
/* --------------------------------------------------------------------- */
/* Select/unselect a node in the network graph.
Parameters are:
- node: data for the node to be changed,
- on: true/false to show/hide the node
*/
function highlightGraphNode( node, on )
{
//if( d3.event.shiftKey ) on = false; // for debugging
// If we are to activate a movie, and there's already one active,
// first switch that one off
if( on && activeMovie !== undefined ) {
console.log("..clear: ",activeMovie);
highlightGraphNode( nodeArray[activeMovie], false );
console.log("..cleared: ",activeMovie);
}
console.log("SHOWNODE "+node.index+" ["+node.label + "]: " + on);
console.log(" ..object ["+node + "]: " + on);
// locate the SVG nodes: circle & label group
circle = d3.select( '#c' + node.index );
label = d3.select( '#l' + node.index );
console.log(" ..DOM: ",label);
// activate/deactivate the node itself
console.log(" ..box CLASS BEFORE:", label.attr("class"));
console.log(" ..circle",circle.attr('id'),"BEFORE:",circle.attr("class"));
circle
.classed( 'main', on );
label
.classed( 'on', on || currentZoom >= SHOW_THRESHOLD );
label.selectAll('text')
.classed( 'main', on );
console.log(" ..circle",circle.attr('id'),"AFTER:",circle.attr("class"));
console.log(" ..box AFTER:",label.attr("class"));
console.log(" ..label=",label);
// activate all siblings
console.log(" ..SIBLINGS ["+on+"]: "+node.links);
Object(node.links).forEach( function(id) {
d3.select("#c"+id).classed( 'sibling', on );
label = d3.select('#l'+id);
label.classed( 'on', on || currentZoom >= SHOW_THRESHOLD );
label.selectAll('text.nlabel')
.classed( 'sibling', on );
} );
// set the value for the current active movie
activeMovie = on ? node.index : undefined;
console.log("SHOWNODE finished: "+node.index+" = "+on );
}
/* --------------------------------------------------------------------- */
/* Show the details panel for a movie AND highlight its node in
the graph. Also called from outside the d3.json context.
Parameters:
- new_idx: index of the movie to show
- doMoveTo: boolean to indicate if the graph should be centered
on the movie
*/
selectMovie = function( new_idx, doMoveTo ) {
console.log("SELECT", new_idx, doMoveTo );
// do we want to center the graph on the node?
doMoveTo = doMoveTo || false;
if( doMoveTo ) {
console.log("..POS: ", currentOffset.x, currentOffset.y, '->',
nodeArray[new_idx].x, nodeArray[new_idx].y );
s = getViewportSize();
width = s.w<WIDTH ? s.w : WIDTH;
height = s.h<HEIGHT ? s.h : HEIGHT;
offset = { x : s.x + width/2 - nodeArray[new_idx].x*currentZoom,
y : s.y + height/2 - nodeArray[new_idx].y*currentZoom };
repositionGraph( offset, undefined, 'move' );
}
// Now highlight the graph node and show its movie panel
highlightGraphNode( nodeArray[new_idx], true );
showMoviePanel( nodeArray[new_idx] );
}
/* --------------------------------------------------------------------- */
/* Show the movie details panel for a given node
*/
function showMoviePanel( node ) {
// Fill it and display the panel
movieInfoDiv
.html( getMovieInfo(node,nodeArray) )
.attr("class","panel_on");
}
/* --------------------------------------------------------------------- */
/* Move all graph elements to its new positions. Triggered:
- on node repositioning (as result of a force-directed iteration)
- on translations (user is panning)
- on zoom changes (user is zooming)
- on explicit node highlight (user clicks in a movie panel link)
Set also the values keeping track of current offset & zoom values
*/
function repositionGraph( off, z, mode ) {
console.log( "REPOS: off="+off, "zoom="+z, "mode="+mode );
// do we want to do a transition?
var doTr = (mode == 'move');
// drag: translate to new offset
if( off !== undefined &&
(off.x != currentOffset.x || off.y != currentOffset.y ) ) {
g = d3.select('g.grpParent')
if( doTr )
g = g.transition().duration(500);
g.attr("transform", function(d) { return "translate("+
off.x+","+off.y+")" } );
currentOffset.x = off.x;
currentOffset.y = off.y;
}
// zoom: get new value of zoom
if( z === undefined ) {
if( mode != 'tick' )
return; // no zoom, no tick, we don't need to go further
z = currentZoom;
}
else
currentZoom = z;
// move edges
e = doTr ? graphLinks.transition().duration(500) : graphLinks;
e
.attr("x1", function(d) { return z*(d.source.x); })
.attr("y1", function(d) { return z*(d.source.y); })
.attr("x2", function(d) { return z*(d.target.x); })
.attr("y2", function(d) { return z*(d.target.y); });
// move nodes
n = doTr ? graphNodes.transition().duration(500) : graphNodes;
n
.attr("transform", function(d) { return "translate("
+z*d.x+","+z*d.y+")" } );
// move labels
l = doTr ? graphLabels.transition().duration(500) : graphLabels;
l
.attr("transform", function(d) { return "translate("
+z*d.x+","+z*d.y+")" } );
}
/* --------------------------------------------------------------------- */
/* Perform drag
*/
function dragmove(d) {
console.log("DRAG",d3.event);
offset = { x : currentOffset.x + d3.event.dx,
y : currentOffset.y + d3.event.dy };
repositionGraph( offset, undefined, 'drag' );
}
/* --------------------------------------------------------------------- */
/* Perform zoom. We do "semantic zoom", not geometric zoom
* (i.e. nodes do not change size, but get spread out or stretched
* together as zoom changes)
*/
function doZoom( increment ) {
newZoom = increment === undefined ? d3.event.scale
: zoomScale(currentZoom+increment);
console.log("ZOOM",currentZoom,"->",newZoom,increment);
if( currentZoom == newZoom )
return; // no zoom change
// See if we cross the 'show' threshold in either direction
if( currentZoom<SHOW_THRESHOLD && newZoom>=SHOW_THRESHOLD )
svg.selectAll("g.label").classed('on',true);
else if( currentZoom>=SHOW_THRESHOLD && newZoom<SHOW_THRESHOLD )
svg.selectAll("g.label").classed('on',false);
// See what is the current graph window size
s = getViewportSize();
width = s.w<WIDTH ? s.w : WIDTH;
height = s.h<HEIGHT ? s.h : HEIGHT;
// Compute the new offset, so that the graph center does not move
zoomRatio = newZoom/currentZoom;
newOffset = { x : currentOffset.x*zoomRatio + width/2*(1-zoomRatio),
y : currentOffset.y*zoomRatio + height/2*(1-zoomRatio) };
console.log("offset",currentOffset,"->",newOffset);
// Reposition the graph
repositionGraph( newOffset, newZoom, "zoom" );
}
zoomCall = doZoom; // unused, so far
/* --------------------------------------------------------------------- */
/* process events from the force-directed graph */
force.on("tick", function() {
repositionGraph(undefined,undefined,'tick');
});
/* A small hack to start the graph with a movie pre-selected */
mid = getQStringParameterByName('id')
if( mid != null )
clearAndSelect( mid );
});
} // end of D3ok()