| 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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592 |
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
| /*
* Grakn - A Distributed Semantic Database
* Copyright (C) 2016 Grakn Labs Limited
*
* Grakn 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.
*
* Grakn 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 Grakn. If not, see <http://www.gnu.org/licenses/gpl.txt>.
*/
import _ from 'underscore';
import vis from 'vis';
import Style from './Style';
import User from '../User';
import NodeSettings from '../NodeSettings';
import * as API from '../util/HALTerms';
/*
* Main class for creating a graph of nodes and edges. See Style class for asthetic customisation.
* Callbacks (for interactivity with the graph) must be registered before calling .render().
* Graph is drawn *only* after calling .render().
* Nodes and edges can be added at any time.
*/
export default class Visualiser {
constructor() {
this.nodes = new vis.DataSet([], {
queue: { delay: 800 },
});
this.edges = new vis.DataSet([]);
this.callbacks = {};
this.style = new Style();
// vis.js network, instantiated on render.
this.network = {};
// vis.js default config
this.networkConfig = {
autoResize: true,
nodes: {
font: {
size: 15,
face: 'Geogrotesque-Ultralight',
},
shadow: {
enabled: true,
size: 10,
x: 2,
y: 2,
},
},
physics: {
barnesHut: {
springLength: 140,
},
minVelocity: 0.75,
},
edges: {
hoverWidth: 2,
selectionWidth: 2,
arrowStrikethrough: false,
arrows: { to: { enabled: true, scaleFactor: 0.7 } },
smooth: {
enabled: false,
forceDirection: 'none',
},
},
interaction: {
hover: true,
multiselect: true,
},
layout: {
improvedLayout: false,
randomSeed: 10,
},
};
// Additional properties to show in node label by type.
this.displayProperties = {};
this.alreadyFittedToWindow = false;
// Structure to hold colour preferences on nodes
this.nodeColourProperies = {};
// working on stopping nodes from moving
this.lastFixTime = 0; // this is needed to stop a redraw loop due to the update of the vis dataset
this.draggingNode = false;
this.draggingRect = false;
this.rectangleCleared = false;
}
setCallbackOnEvent(eventName, callback) {
this.callbacks[eventName] = callback;
}
/**
* Start visualisation and render graph.
* This needs to be called only once, but all callbacks should be configured
* prior.
*/
render(container) {
this.network = new vis.Network(
container, {
nodes: this.nodes,
edges: this.edges,
},
this.networkConfig);
for (const eventName in this.callbacks) {
this.network.on(eventName, this.callbacks[eventName]);
}
this.network.on('stabilized', (params) => {
if (this.draggingNode === false && User.getFreezeNodes()) {
this.fixNodes();
}
});
this.network.on('dragEnd', (params) => {
this.draggingNode = false;
this.fixNodes(params.nodes);
});
this.network.on('afterDrawing', (params) => {
if (this.draggingRect) {
this.drawRectangle();
}
});
// Variables used to draw selection rectangle.
this.canvas = this.network.canvas.frame.canvas;
this.ctx = this.canvas.getContext('2d');
this.rect = {};
return this;
}
// Methods used to draw a selection rectangle on the canvas and select multiple nodes
resetRectangle() {
this.draggingRect = false;
this.selectNodesFromHighlight();
this.rect.w = 0;
this.rect.h = 0;
}
startRectangle(x, y) {
this.rect.startX = x;
this.rect.startY = y;
}
updateRectangle(x, y) {
if (this.draggingRect) {
const canvasPosition = this.network.DOMtoCanvas({
x,
y,
});
this.rect.w = canvasPosition.x - this.rect.startX;
this.rect.h = canvasPosition.y - this.rect.startY;
// Force redraw the canvas which will also draw the rectangle with new size.
this.network.redraw();
}
}
drawRectangle() {
this.ctx.setLineDash([5]);
this.ctx.strokeStyle = 'rgb(0, 102, 0)';
this.ctx.strokeRect(this.rect.startX, this.rect.startY, this.rect.w, this.rect.h);
this.ctx.setLineDash([]);
this.ctx.fillStyle = 'rgba(0, 255, 0, 0.2)';
this.ctx.fillRect(this.rect.startX, this.rect.startY, this.rect.w, this.rect.h);
}
selectNodesFromHighlight() {
const nodesIdInDrawing = [];
const xRange = Visualiser.getStartToEnd(this.rect.startX, this.rect.w);
const yRange = Visualiser.getStartToEnd(this.rect.startY, this.rect.h);
const allNodes = this.nodes.get();
const arrayLength = allNodes.length;
for (let i = 0; i < arrayLength; i++) {
const curNode = allNodes[i];
const nodePosition = this.network.getPositions([curNode.id]);
const nodeXY = nodePosition[curNode.id];
if (xRange.start <= nodeXY.x && nodeXY.x <= xRange.end && yRange.start <= nodeXY.y && nodeXY.y <= yRange.end) {
nodesIdInDrawing.push(curNode.id);
}
}
this.network.selectNodes(nodesIdInDrawing);
}
static getStartToEnd(start, theLen) {
return theLen > 0 ? {
start,
end: start + theLen,
} : {
start: start + theLen,
end: start,
};
}
// ---------------------------------------------- //
fixAllNodes() {
this.fixNodes(this.nodes.getIds());
}
releaseAllNodes() {
this.releaseNodes(this.nodes.getIds());
}
// Methods used to fix and release nodes when one or more are dragged //
fixNodes(nodeIds) {
if (new Date() - this.lastFixTime > 100) {
// this.network.storePositions();
this.lastFixTime = new Date();
if (nodeIds !== undefined) {
nodeIds.forEach(nodeId => this.fixSingleNode(nodeId));
} else {
this.nodes.forEach((node) => {
this.fixSingleNode(node.id);
});
}
}
}
fixSingleNode(nodeId) {
if (nodeId === undefined) return;
this.updateNode({
id: nodeId,
fixed: {
x: true,
y: true,
},
});
}
releaseNodes(nodeIds) {
if (nodeIds === undefined) return;
nodeIds.forEach(nodeId => this.updateNode({
id: nodeId,
fixed: {
x: false,
y: false,
},
}));
}
// -------------------------- //
// Fit the graph to the window size only on the first ajax call,
// then leave zoom control to the user
fitGraphToWindow() {
if (!this.alreadyFittedToWindow) {
this.network.fit();
this.alreadyFittedToWindow = true;
}
}
/**
* Add a node to the graph. This can be called at any time *after* render().
*/
addNode(href, bp, ap, ls, cn) {
if (!this.nodeExists(bp.id)) {
const colorObj = this.style.getNodeColour(bp.type, bp.baseType);
const highlightObj = {
highlight: Object.assign(colorObj.highlight, {
border: colorObj.highlight.background,
}),
};
const hoverObj = {
hover: highlightObj.highlight,
};
this.nodes.add({
id: bp.id,
href,
label: this.generateLabel(bp.type, ap, bp.label, bp.baseType),
baseLabel: bp.label,
type: bp.type,
title: bp.type,
baseType: bp.baseType,
color: Object.assign(colorObj, {
border: colorObj.background,
}, highlightObj, hoverObj),
font: this.style.getNodeFont(bp.type, bp.baseType),
shape: this.style.getNodeShape(bp.baseType),
size: this.style.getNodeSize(bp.baseType),
selected: false,
explore: bp.explore,
properties: ap,
links: ls,
});
this.nodes.flush();
} else if (bp.id !== cn && User.getFreezeNodes()) { // If node already in graph and it's not the node clicked by user, unlock it
this.updateNode({
id: bp.id,
fixed: {
x: false,
y: false,
},
});
}
return this;
}
// Given an array of instances(nodes) refresh all their labels with new resources
refreshLabels(instances) {
instances.forEach((instance) => {
const node = this.getNode(instance.id);
this.updateNode({
id: node.id,
label: this.generateLabel(node.type, node.properties, node.baseLabel, node.baseType),
});
});
}
updateNodeResources(id, properties) {
this.updateNode({
id,
properties,
});
}
/**
* Add edge between two nodes with @label. This can be called at any time *after* render().
*/
addEdge(fromNode, toNode, label) {
if (!this.alreadyConnected(fromNode, toNode)) {
this.edges.add({
from: fromNode,
to: toNode,
label,
color: this.style.getEdgeColour(label),
font: this.style.getEdgeFont(label),
arrows: {
to: (label !== 'relates'),
},
});
}
return this;
}
/**
* Delete a node and its edges
*/
deleteNode(id) {
if (this.nodeExists(id)) {
this.deleteEdges(id);
this.nodes.remove(id);
}
return this;
}
/**
* Removes all nodes and edges from graph
*/
clearGraph() {
this.nodes.clear();
this.edges.clear();
this.network.setData({
nodes: this.nodes,
edges: this.edges,
});
return this;
}
getNodeType(id) {
if (id in this.nodes._data) {
return this.nodes._data[id].type;
}
return undefined;
}
getNode(id) {
return this.nodes._data[id];
}
getAllNodeProperties(id) {
if (id in this.nodes._data) {
return Object.keys(this.nodes._data[id].properties).sort();
}
return [];
}
getNodeLabel(id) {
return this.nodes._data[id].label;
}
setDisplayProperties(type, properties) {
if (type in this.displayProperties && properties.length === 0) {
delete this.displayProperties[type];
} else {
this.displayProperties[type] = properties;
}
this.updateNodeLabels(type);
return this;
}
setDefaultNodeColour(baseType, nodeType) {
const colorObj = this.style.getDefaultNodeColour(nodeType, baseType);
const highlightObj = {
highlight: Object.assign(colorObj.highlight, {
border: colorObj.highlight.background,
}),
};
const hoverObj = {
hover: highlightObj.highlight,
};
this.nodes.get().forEach((v) => {
if (v.type === nodeType) {
this.updateNode({
id: v.id,
color: Object.assign(colorObj, {
border: colorObj.background,
}, highlightObj, hoverObj),
});
}
});
}
setColourOnNodeType(baseType, nodeType, colourString) {
if (colourString === undefined) {
this.setDefaultNodeColour(baseType, nodeType);
const t = (nodeType.length) ? nodeType : baseType;
NodeSettings.setNodeColour(t);
return;
}
if (nodeType.length) {
NodeSettings.setNodeColour(nodeType, {
background: colourString,
highlight: {
background: colourString,
} });
this.nodes.get().forEach((v) => {
if (v.type === nodeType) {
this.updateNode({
id: v.id,
color: {
background: colourString,
border: colourString,
highlight: {
background: colourString,
border: colourString,
},
hover: {
background: colourString,
border: colourString,
},
},
});
}
});
} else {
NodeSettings.setNodeColour(baseType, {
background: colourString,
highlight: {
background: colourString,
} });
// If it's an ontology node
this.nodes.get().forEach((v) => {
if (v.baseType === baseType) {
this.updateNode({
id: v.id,
color: {
background: colourString,
border: colourString,
highlight: {
background: colourString,
border: colourString,
},
hover: {
background: colourString,
border: colourString,
},
},
});
}
});
}
}
getNodeOnCoordinates(coordinates) {
const canvasX = coordinates.x;
const canvasY = coordinates.y;
const allNodes = this.nodes.get();
const arrayLength = allNodes.length;
for (let i = 0; i < arrayLength; i++) {
const curNode = allNodes[i];
const boundingBox = this.network.getBoundingBox(curNode.id);
if (canvasX <= boundingBox.right && canvasX >= boundingBox.left && canvasY >= boundingBox.top && canvasY <= boundingBox.bottom) {
return curNode.id;
}
}
return null;
}
/*
Internal methods
*/
/**
* Check if node has already been added to graph.
*/
nodeExists(id) {
return (id in this.nodes._data);
}
/**
* Check if (a,b) match (x,y) in either combination.
*/
static matching(a, b, x, y) {
return ((a === x && b === y) || (a === y && b === x));
}
/**
* Check if two nodes (a,b) are already connected by an edge.
*/
alreadyConnected(a, b) {
Eif (!(a in this.nodes._data && b in this.nodes._data)) {
return false;
}
return _.contains(_.values(this.edges._data)
.map(x => Visualiser.matching(a, b, x.to, x.from)),
true);
}
/**
* Delete all edges connected to node id
*/
deleteEdges(id) {
this.edges.forEach((x) => {
if (x.to === id || x.from === id) this.edges.remove(x.id);
});
}
generateLabel(type, properties, label, baseType) {
if (baseType === API.RELATION || baseType === API.GENERATED_RELATION_TYPE || baseType === API.INFERRED_RELATION_TYPE) return '';
if (NodeSettings.getLabelProperties(type).length) {
return NodeSettings.getLabelProperties(type).reduce((l, x) => {
let value;
if (x === 'type') {
value = type;
return `${(l.length ? `${l}\n` : l) + value}`;
}
value = (properties[x] === undefined) ? '' : properties[x].label;
if (value.length > 40) value = `${value.substring(0, 40)}...`;
return `${(l.length ? `${l}\n` : l) + x}: ${value}`;
}, '');
}
return label;
}
updateNodeLabels(type) {
this.nodes._data = _.mapObject(this.nodes._data, (v, k) => {
if (v.type === type) {
this.updateNode({
id: k,
label: this.generateLabel(type, v.properties, v.baseLabel, v.baseType),
});
}
return v;
});
}
flushUpdates() {
this.nodes.flush();
}
updateNode(obj) {
this.nodes.update(obj);
this.nodes.flush();
}
}
|