| 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 |
1x
1x
1x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
9x
9x
9x
4x
4x
4x
10x
6x
9x
1x
8x
6x
9x
9x
| /*
* 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';
/*
* 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([]);
this.edges = new vis.DataSet([]);
this.callbacks = {
click: () => {},
doubleClick: () => {},
rightClick: () => {},
hover: () => {},
dragEnd: () => {},
hold: () => {},
};
this.style = new Style();
// vis.js network, instantiated on render.
this.network = {};
// vis.js default config
this.networkConfig = {
autoResize: true,
nodes: {
// shape: 'star',
font: {
size: 15,
face: 'DIN',
},
shadow: true,
},
edges: {
arrows: {
to: true,
},
smooth: {
forceDirection: 'none',
},
},
interaction: {
hover: true,
multiselect: false,
},
layout: {
improvedLayout: false,
},
};
// Additional properties to show in node label by type.
this.displayProperties = {};
this.alreadyFittedToWindow = false;
this.clusters = [];
}
/**
* Register callback for mouse click on nodes or edges.
*/
setOnClick(fn) {
this.callbacks.click = fn;
return this;
}
/**
* Register callback for double click on nodes or edges.
*/
setOnDoubleClick(fn) {
this.callbacks.doubleClick = fn;
return this;
}
/**
* Register callback for right click on node or edges.
*/
setOnRightClick(fn) {
this.callbacks.rightClick = fn;
return this;
}
/**
* Register callback for mouse hover on nodes.
*/
setOnHover(fn) {
this.callbacks.hover = fn;
return this;
}
/**
* Register callback for when a node dragging is finished.
*/
setOnDragEnd(fn) {
this.callbacks.dragEnd = fn;
return this;
}
setOnHoldOnNode(fn) {
this.callbacks.hold = fn;
return this;
}
/**
* 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);
this.network.on('click', this.callbacks.click);
this.network.on('doubleClick', this.callbacks.doubleClick);
this.network.on('oncontext', this.callbacks.rightClick);
this.network.on('hoverNode', this.callbacks.hover);
this.network.on('dragEnd', this.callbacks.dragEnd);
this.network.on('hold', this.callbacks.hold);
this.network.on('stabilized', () => {
this.setSimulation(false);
});
return this;
}
// 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(id, bp, ap, ls) {
Eif (!this.nodeExists(id)) {
this.nodes.add({
id,
uuid: bp.id,
label: this.generateLabel(bp.type, ap, bp.label),
baseLabel: bp.label,
type: bp.type,
baseType: bp.baseType,
color: this.style.getNodeColour(bp.type, bp.baseType),
font: this.style.getNodeFont(bp.type, bp.baseType),
shape: this.style.getNodeShape(bp.baseType),
selected: false,
ontology: bp.ontology,
properties: ap,
links: ls,
});
}
return this;
}
disablePhysicsOnNode(id) {
if (this.nodeExists(id)) {
this.nodes.update({
id,
physics: false,
});
}
return this;
}
/**
* Add edge between two nodes with @label. This can be called at any time *after* render().
*/
addEdge(fromNode, toNode, label) {
Eif (!this.alreadyConnected(fromNode, toNode)) {
this.edges.add({
from: fromNode,
to: toNode,
label,
color: this.style.getEdgeColour(),
font: this.style.getEdgeFont(),
});
}
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;
}
/**
* Stop/start physics simulation and all animation in displayed graph.
*/
setSimulation(state) {
if (state) { this.network.startSimulation(); } else {
this.network.stopSimulation();
}
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;
}
cluster() {
this.clusters.forEach((c) => {
this.network.cluster({
joinCondition: x => ((x.baseType !== 'resource-type') && (x.type !== 'resource-type') && (x.type === c)),
clusterNodeProperties: {
id: `cluster-${c}`,
label: `cluster of: ${c}`,
color: this.style.clusterColour(),
font: this.style.clusterFont(),
},
processProperties: (o, n, e) => {
if (!this.nodeExists(o.id)) this.nodes.add(o);
return o;
},
});
});
// this.predefinedClusters();
return this;
}
expandCluster(id) {
if (this.network.isCluster(id)) {
this.network.openCluster(id);
this.deleteNode(id);
return true;
}
return false;
}
/*
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) {
if (!(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) {
Iif (type in this.displayProperties) {
return this.displayProperties[type].reduce((l, x) => {
const value = (properties[x] === undefined) ? '' : properties[x].label;
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.nodes.update({
id: k,
label: this.generateLabel(type, v.properties, v.baseLabel),
});
}
return v;
});
// this.cluster();
}
addCluster(clusterBy) {
if (!_.contains(this.clusters, clusterBy)) {
this.clusters.push(clusterBy);
}
}
predefinedClusters() {
this.network.cluster({
joinCondition: x => (x.baseType === 'resource-type'),
clusterNodeProperties: {
id: 'cluster-resource-type',
label: 'cluster of: resource-type',
color: this.style.clusterColour(),
font: this.style.clusterFont(),
},
});
}
}
|