-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinteraction.js
505 lines (468 loc) · 18.7 KB
/
interaction.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
///////////////////////////////////////////////////////////////////////////////
//
// Topographic Attribute Maps Demo
// Copyright 2020 Reinhold Preiner, Johanna Schmidt, Gabriel Mistelbauer
//
// This code is licensed under an MIT License.
// See the accompanying LICENSE file for details.
//
///////////////////////////////////////////////////////////////////////////////
function initInteractions()
{
// Add pattern for single heightfield selection and highlighting
renderer.CANVAS.append("defs").append("pattern")
.attr("id","myPattern")
.attr("width", 40)
.attr("height", 40)
.attr("patternUnits","userSpaceOnUse")
.append("path")
.attr("fill","none")
.attr("stroke","#111")
.attr("stroke-width","2")
//.attr("d","M-1,1 l2,-2 M0,20 l20,-20 M19,21 l2,-2");
.attr("d","M0,40 l40,-40 M0,0 l40,40");
// initialize Menubar
initMenubar();
// initialize zoom and pan capabilities
d3.select("#tam").call(
d3.zoom()
.scaleExtent([.01, 100])
.on("zoom", function() { renderer.CANVAS.attr("transform", d3.event.transform); })
);
// initialize tooltips for hovering over nodes
initTooltip();
// define interaction possibilities for graph svg
setTAMInteractions();
// define interaction possibilities for menu bar
setMenubarInteractions();
}
/////////////////////////////////////////////////////////////////////////////
/// SVG INTERACTIONS
function setTAMInteractions()
{
// events
d3.select("body")
.on("keydown", function() {
if (d3.event.keyCode == "S".charCodeAt(0)) {
toggleShading();
d3.select("#settings_shading").property('checked', PARAM_SHADING);
}
else if (d3.event.keyCode == "R".charCodeAt(0)) {
toggleReverseColormap();
d3.select("#settings_reversecolor").property('checked', PARAM_REVERSE_COLORMAP);
}
else if (d3.event.keyCode == "H".charCodeAt(0)) {
toggleSelectTime();
d3.select("#settings_select_time").property('checked', PARAM_USE_MOUSEOVER);
}
else if (d3.event.keyCode == "I".charCodeAt(0)) {
toggleShowTooltips();
d3.select("#settings_show_tooltips").property('checked', PARAM_SHOW_TOOLTIPS);
}
else if (d3.event.keyCode == "F".charCodeAt(0)) {
toggleEnergizeSimulation();
d3.select("#settings_freeze").property('checked', !PARAM_ENERGIZE);
}
else if (d3.event.keyCode == "N".charCodeAt(0)) {
toggleNames();
d3.select("#settings_show_names").property('checked', PARAM_SHOW_NAMES);
}
else if(d3.event.keyCode == "G".charCodeAt(0)) {
toggleShowGraph();
d3.select("#settings_show_graph").property('checked', PARAM_SHOW_GRAPH);
}
else if (d3.event.keyCode == "M".charCodeAt(0)) {
toggleShowContours();
d3.select("#settings_show_contours").property('checked', PARAM_SHOW_CONTOURS);
}
else if (d3.event.keyCode == "L".charCodeAt(0)) {
toggleLinks();
d3.select("#settings_show_links").property('checked', PARAM_SHOW_LINKS);
}
else if (d3.event.keyCode == "T".charCodeAt(0)) {
toggleShowTunnels();
d3.select("#settings_show_tunnels").property('checked', PARAM_SHOW_TUNNELS);
}
else if (d3.event.keyCode == "E".charCodeAt(0)) {
document.getElementById("btnSvgExport").click();
}
});
// make nodes draggable
renderer.SVG_DRAGABLE_ELEMENTS.call(d3.drag()
.on("start", dragStartNode)
.on("drag", dragNode)
.on("end", dragEndNode)
);
renderer.SVG_DRAGABLE_ELEMENTS
.on("click", onMouseClick)
}
//---------------------------------------------------------------------------
function onMouseClick(d)
{
d.fx = d.fy = null;
}
//---------------------------------------------------------------------------
function mouseoverContour(c)
{
renderer.SVG_CONTOURS
.attr("fill",
function(d)
{
// Currently selected one will be always at 0.5
if (c.value === d.value)
{
return "url(#myPattern) #000";//chromadepth(0.5);
}
return renderer.SVG_COLORMAP(d.value);
}
);
}
//---------------------------------------------------------------------------
function dragStartNode(d)
{
d3.event.sourceEvent.stopPropagation();
if (!d3.event.active)
{
renderer.resetScalarField();
if (!PARAM_ENERGIZE)
renderer.FORCE_SIMULATION.velocityDecay(1); // don't move anything than the selected node!
renderer.FORCE_SIMULATION.alpha(PARAM_ALPHA).restart();
}
d.fx = d.x;
d.fy = d.y;
if (PARAM_SHOW_TOOLTIPS)
d3.select("#tooltip").style("opacity", PARAM_TOOLTIP_DRAG_OPACITY);
}
//---------------------------------------------------------------------------
function dragNode(d)
{
d.fx = d3.event.x;
d.fy = d3.event.y;
if (PARAM_SHOW_TOOLTIPS)
d3.select("#tooltip")
.style("top", (d3.event.sourceEvent.pageY - 10) + "px")
.style("left", (d3.event.sourceEvent.pageX + 15) + "px");
}
//---------------------------------------------------------------------------
function toggleEnergizeSimulation()
{
PARAM_ENERGIZE = !PARAM_ENERGIZE;
if (PARAM_ENERGIZE)
{
renderer.resetScalarField();
renderer.FORCE_SIMULATION.alpha(PARAM_ALPHA).restart();
}
else
renderer.FORCE_SIMULATION.alpha(0);
}
//---------------------------------------------------------------------------
function dragEndNode(d)
{
if (!d3.event.active && !PARAM_ENERGIZE)
renderer.FORCE_SIMULATION.velocityDecay(PARAM_FRICTION).alpha(0); // reset friction
//d.fx = d.fy = null;
if (PARAM_SHOW_TOOLTIPS)
d3.select("#tooltip").style("opacity", 1.0);
}
//---------------------------------------------------------------------------
function toggleShading()
{
PARAM_SHADING = !PARAM_SHADING;
renderer.SHADING_LAYER.attr("visibility", PARAM_SHOW_CONTOURS && PARAM_SHADING ? "visible" : "hidden");
}
//---------------------------------------------------------------------------
function toggleLinks()
{
PARAM_SHOW_LINKS = !PARAM_SHOW_LINKS;
renderer.toggleLinks(PARAM_SHOW_LINKS);
}
//---------------------------------------------------------------------------
function toggleShowGraph()
{
PARAM_SHOW_GRAPH = !PARAM_SHOW_GRAPH;
renderer.GRAPH_LAYER.attr("visibility", PARAM_SHOW_GRAPH ? "visible" : "hidden");
}
//---------------------------------------------------------------------------
function toggleLifelines()
{
if (renderer && renderer instanceof TFMRenderer)
{
// toggle state
PARAM_SHOW_LIFELINES = !PARAM_SHOW_LIFELINES;
let transform = renderer.CANVAS.attr("transform"); // save current view on canvas
// save graph data
graph = renderer.GRAPH;
// stop any running simulation and reset SVG layers
if (renderer.FORCE_SIMULATION)
renderer.FORCE_SIMULATION.stop();
resetSVGLayers();
// create new renderer with the saved data
delete renderer;
renderer = new TFMRenderer();
// NOTE: graph already contains all the enriched data created during the createFamilyForceGraph procedure,
// as well as all the simulation (p.x/y) and layout (p.vis.x/y) position data. We can still pass it as is
// to the createFamilyForceGraph() function, we only need to make sure that any lifeline info is deleted
// in case it is toggled off.
if (!PARAM_SHOW_LIFELINES)
graph.persons.forEach( p => delete p.lifeline );
renderer.createFamilyForceGraph(graph);
// creating a new renderer created a new SVG CANVAS -> now restore view transform of this canvas
renderer.CANVAS.attr("transform", transform);
}
}
//---------------------------------------------------------------------------
function toggleShowContours()
{
PARAM_SHOW_CONTOURS = !PARAM_SHOW_CONTOURS;
renderer.TOPO_LAYER.attr("visibility", PARAM_SHOW_CONTOURS ? "visible" : "hidden");
renderer.SHADING_LAYER.attr("visibility", PARAM_SHOW_CONTOURS && PARAM_SHADING ? "visible" : "hidden");
}
//---------------------------------------------------------------------------
function toggleNames()
{
PARAM_SHOW_NAMES = !PARAM_SHOW_NAMES;
if (PARAM_SHOW_NAMES)
renderer.showNames();
else
renderer.hideNames();
}
//---------------------------------------------------------------------------
function toggleShowTunnels()
{
PARAM_SHOW_TUNNELS = !PARAM_SHOW_TUNNELS;
if (!PARAM_ENERGIZE) renderer.updateScalarField();
}
//---------------------------------------------------------------------------
function toggleReverseColormap()
{
PARAM_REVERSE_COLORMAP = !PARAM_REVERSE_COLORMAP;
renderer.setColorMap();
if (!PARAM_ENERGIZE) renderer.updateScalarField();
}
//---------------------------------------------------------------------------
function toggleSelectTime()
{
PARAM_USE_MOUSEOVER = !PARAM_USE_MOUSEOVER;
if (PARAM_USE_MOUSEOVER) {
renderer.TOPO_LAYER.selectAll("path.contours").on("mouseover", mouseoverContour);
} else {
renderer.resetColormap();
renderer.TOPO_LAYER.selectAll("path.contours").on("mouseover", null);
}
}
//---------------------------------------------------------------------------
function toggleShowTooltips()
{
PARAM_SHOW_TOOLTIPS = !PARAM_SHOW_TOOLTIPS;
registerTooltipEventhandler();
}
/////////////////////////////////////////////////////////////////////////////
/// MENUBAR INTERACTIONS
function initMenubar()
{
d3.select("#settings_shading").property('checked', PARAM_SHADING);
d3.select("#settings_reversecolor").property('checked', PARAM_REVERSE_COLORMAP);
d3.select("#settings_dataset").property("value", PARAM_FILENAME);
d3.select("#settings_noderadius").property("value", PARAM_NODE_RADIUS);
d3.select("#settings_linkwidth").property("value", PARAM_LINK_WIDTH);
d3.select("#settings_pnodeopacity").property("value", PARAM_PERSON_LABEL_OPACITY);
// Interaction
d3.select("#settings_freeze").property('checked', !PARAM_ENERGIZE);
d3.select("#settings_select_time").property('checked', PARAM_USE_MOUSEOVER);
d3.select("#settings_show_tooltips").property('checked', PARAM_SHOW_TOOLTIPS);
// Force Simulation
d3.select("#settings_repulsion_strength").property("value", PARAM_REPULSION_STRENGTH);
d3.select("#settings_gravity_x").property('value', PARAM_GRAVITY_X);
d3.select("#settings_gravity_y").property('value', PARAM_GRAVITY_Y);
d3.select("#settings_link_strength").property("value", PARAM_LINK_STRENGTH);
d3.select("#settings_friction").property("value", PARAM_FRICTION);
d3.select("#settings_simforce_strength").property("value", PARAM_SF_STRENGTH);
// Appearance
d3.select("#settings_interpolation_type").property("checked", PARAM_INTERPOLATE_NN);
d3.select("#settings_embed_links").property("checked", PARAM_EMBED_LINKS);
d3.select("#settings_show_contours").property("checked", PARAM_SHOW_CONTOURS);
d3.select("#settings_show_graph").property("checked", PARAM_SHOW_GRAPH);
d3.select("#settings_show_lifelines").property("checked", PARAM_SHOW_LIFELINES);
d3.select("#settings_show_links").property("checked", PARAM_SHOW_LINKS);
d3.select("#settings_show_names").property("checked", PARAM_SHOW_NAMES);
d3.select("#settings_show_tunnels").property("checked", PARAM_SHOW_TUNNELS);
d3.select("#settings_dilation_degree").property("value", PARAM_SCALARFIELD_DILATION_ITERS);
d3.select("#settings_contour_step").property("value", PARAM_CONTOUR_STEP);
d3.select("#settings_contour_big_step").property("value", PARAM_CONTOUR_BIG_STEP);
d3.select("#settings_indicator_size").property("value", PARAM_INDICATOR_FONTSIZE);
d3.select("#settings_range_min").property("value", PARAM_RANGE_MIN);
d3.select("#settings_range_max").property("value", PARAM_RANGE_MAX);
d3.select("#settings_height_scale").property("value", PARAM_HEIGHT_SCALE);
d3.select("#settings_resolution").property("value", PARAM_SCALARFIELD_RESOLUTION);
d3.select("#settings_link_sample_step").property("value", PARAM_LINK_SAMPLE_STEPSIZE);
d3.select("#settings_underground_threshold").property("value", PARAM_UNDERGROUND_THRESHOLD);
// Interactive features
d3.select("#settings_show_tooltips").property("checked", PARAM_SHOW_TOOLTIPS);
}
//---------------------------------------------------------------------------
function setMenubarInteractions()
{
d3.select("#settings_interpolation_type").on("input", function() {
PARAM_INTERPOLATE_NN = this.checked;
if (!PARAM_ENERGIZE) renderer.updateScalarField();
});
d3.select("#settings_contour_step").property("value", PARAM_CONTOUR_STEP).on("input", function() {
PARAM_CONTOUR_STEP = parseFloat(this.value);
if (!PARAM_ENERGIZE) renderer.updateScalarField();
});
d3.select("#settings_contour_big_step").on("input", function() {
PARAM_CONTOUR_BIG_STEP = parseFloat(this.value);
if (!PARAM_ENERGIZE) renderer.updateScalarField();
});
d3.select("#settings_noderadius").on("input", function() {
PARAM_NODE_RADIUS = parseInt(this.value);
if (renderer.SVG_NODE_CIRCLES) renderer.SVG_NODE_CIRCLES.attr("r", PARAM_NODE_RADIUS)
});
d3.select("#settings_linkdist").on("input", function() {
PARAM_LINK_DISTANCE = parseInt(this.value);
});
d3.select("#settings_linkwidth").on("input", function() {
PARAM_LINK_WIDTH = parseInt(this.value);
if (renderer.SVG_LINKS) renderer.SVG_LINKS.attr("stroke-width", PARAM_LINK_WIDTH + "px");
if (renderer.SVG_LINKS_STREETS) renderer.SVG_LINKS_STREETS.attr("stroke-width", PARAM_LINK_WIDTH + "px");
if (renderer.SVG_LINKS_TUNNELS) renderer.SVG_LINKS_TUNNELS.attr("stroke-width", PARAM_LINK_WIDTH + "px");
if (renderer.SVG_TUNNEL_ENTRIES_1) renderer.SVG_TUNNEL_ENTRIES_1.attr("stroke-width", PARAM_LINK_WIDTH + "px");
if (renderer.SVG_TUNNEL_ENTRIES_2) renderer.SVG_TUNNEL_ENTRIES_2.attr("stroke-width", PARAM_LINK_WIDTH + "px");
});
d3.select("#settings_pnodeopacity").on("input", function() {
PARAM_PERSON_LABEL_OPACITY = parseFloat(this.value);
if (renderer.SVG_NODE_LABELS) renderer.SVG_NODE_LABELS.style("opacity", PARAM_PERSON_LABEL_OPACITY);
});
d3.select("#settings_simforce_strength").on("input", function() {
PARAM_SF_STRENGTH = parseFloat(this.value);
});
d3.select("#settings_repulsion_strength").on("input", function() {
PARAM_REPULSION_STRENGTH = this.value;
renderer.REPULSION_FORCE.strength(-PARAM_REPULSION_STRENGTH);
});
d3.select("#settings_link_strength").on("input", function() {
PARAM_LINK_STRENGTH = this.value;
renderer.LINK_FORCE.strength(PARAM_LINK_STRENGTH);
});
d3.select("#settings_friction").on("input", function() {
PARAM_FRICTION = parseFloat(this.value);
renderer.FORCE_SIMULATION.velocityDecay(PARAM_FRICTION);
});
d3.select("#settings_gravity_x").on("input", function() {
PARAM_GRAVITY_X = parseFloat(this.value);
renderer.FORCE_SIMULATION.force("x", d3.forceX(0).strength(PARAM_GRAVITY_X))
});
d3.select("#settings_gravity_y").on("input", function() {
PARAM_GRAVITY_Y = parseFloat(this.value);
renderer.FORCE_SIMULATION.force("y", d3.forceY(0).strength(PARAM_GRAVITY_Y))
});
d3.select("#settings_embed_links").on("input", function() {
PARAM_EMBED_LINKS = !PARAM_EMBED_LINKS;
if (!PARAM_ENERGIZE) renderer.updateScalarField();
});
d3.select("#settings_dilation_degree").on("input", function() {
PARAM_SCALARFIELD_DILATION_ITERS = parseFloat(this.value);
if (!PARAM_ENERGIZE) renderer.updateScalarField();
});
d3.select("#settings_show_names").on("input", function() {
toggleNames();
});
d3.select("#settings_indicator_size").on("input", function() {
PARAM_INDICATOR_FONTSIZE = this.value;
if (renderer.SVG_INDICATOR_LABELS) renderer.SVG_INDICATOR_LABELS.style("font-size", PARAM_INDICATOR_FONTSIZE);
});
d3.select("#settings_show_tunnels").on("input", function () {
toggleShowTunnels();
});
d3.select("#settings_show_contours").on("input", function() {
toggleShowContours();
});
d3.select("#settings_show_graph").on("input", function() {
toggleShowGraph();
});
d3.select("#settings_show_lifelines").on("input", function() {
toggleLifelines();
});
d3.select("#settings_show_links").on("input", function() {
toggleLinks();
});
d3.select("#settings_shading").on("click", function(e){
toggleShading();
});
d3.select("#settings_reversecolor").on("click", function(e){
toggleReverseColormap();
});
d3.select("#settings_select_time").on("click", function(e){
toggleSelectTime();
});
d3.select("#settings_freeze").on("click", function (e) {
toggleEnergizeSimulation();
});
d3.select("#settings_show_tooltips").on("click", function (e) {
toggleShowTooltips();
});
d3.select("#settings_range_min").on("input", function() {
PARAM_RANGE_MIN = parseFloat(this.value);
renderer.setColorMap();
renderer.updateRange();
if (!PARAM_ENERGIZE) renderer.updateScalarField();
});
d3.select("#settings_range_max").on("input", function() {
PARAM_RANGE_MAX = parseFloat(this.value);
renderer.setColorMap();
renderer.updateRange();
if (!PARAM_ENERGIZE) renderer.updateScalarField();
});
d3.select("#settings_height_scale").on("input", function() {
PARAM_HEIGHT_SCALE = parseFloat(this.value);
if (!PARAM_ENERGIZE) renderer.updateScalarField();
});
d3.select("#settings_resolution").on("input", function() {
PARAM_SCALARFIELD_RESOLUTION = parseInt(this.value);
if (!PARAM_ENERGIZE) renderer.updateScalarField();
});
d3.select("#settings_link_sample_step").on("input", function() {
PARAM_LINK_SAMPLE_STEPSIZE = parseInt(this.value);
if (!PARAM_ENERGIZE) renderer.updateScalarField();
});
d3.select("#settings_underground_threshold").on("input", function() {
PARAM_UNDERGROUND_THRESHOLD = parseFloat(this.value);
if (!PARAM_ENERGIZE) renderer.updateScalarField();
});
}
//---------------------------------------------------------------------------
function initTooltip()
{
d3.select("#tooltip").remove(); // remove any previous elements
d3.select("body").append("div").attr("id", "tooltip");
registerTooltipEventhandler();
}
function registerTooltipEventhandler()
{
if (PARAM_SHOW_TOOLTIPS) {
let tooltip = d3.select("#tooltip");
renderer.SVG_DRAGABLE_ELEMENTS
.on("mouseover", function (node) {
return tooltip.style("visibility", "visible");
})
.on("mouseenter", function (node) { // insert tooltip content
let tooltipString = renderer.getNodeAttributesAsString(node);
return tooltip.text(tooltipString);
})
.on("mousemove", function () { // adjust tooltip position
return tooltip
.style("top", (d3.event.pageY - 10) + "px")
.style("left", (d3.event.pageX + 15) + "px");
})
.on("mouseout", function () {
return tooltip.style("visibility", "hidden");
});
} else {
renderer.SVG_DRAGABLE_ELEMENTS
.on("mouseover", null)
.on("mouseenter", null)
.on("mousemove", null)
.on("mouseout", null)
d3.select("#tooltip").style("visibility", "hidden");
}
}