Newer
Older
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
const colorPallette = ['rgb(104, 169, 77)', 'rgb(102, 75, 154)', 'rgb(41, 171, 226)', 'rgb(224, 133, 35)',
'rgb(214, 207, 126)', 'rgb(239, 65, 35)', 'rgb(255, 255, 255)'];
const edgeColors = {};
var graph_element = document.getElementById('3d-graph');
class Graph {
constructor(dataUrl) {
this.graph = null;
this.highlightNodes = new Set();
this.highlightLinks = new Set();
this.hoverNode = null;
this.firstTick = true;
this.infooverlay = null;
this.loadGraph(dataUrl);
}
async loadGraph(dataUrl) {
const gData = await fetch(dataUrl).then(res => res.json())
this.graph = ForceGraph3D({extraRenderers: [new THREE.CSS2DRenderer(), new THREE.CSS3DRenderer()]})
(document.getElementById('3d-graph'))
.graphData(gData)
.nodeLabel('id')
.nodeAutoColorBy('group')
.nodeColor(node => this.getNodeColor(node))
.linkWidth(link => this.getLinkWidth(link))
.onNodeClick(node => {
this.focusOnNode(node);
this.infooverlay.updateInfoOverlay(node);
})
.onNodeHover(node => {
this.onNodeHover(node);
this.updateHighlight();
})
.onLinkHover(link => this.onLinkHover(link))
.linkColor(link => this.getLinkColor(link))
.linkOpacity(0.8)
.nodeThreeObjectExtend(false)
.nodeThreeObject(node => this.drawNode(node))
.onEngineTick(() => this.initializeModel())
.width(getWidth())
.height(getHeight());
}
getNodeColor(node) {
return this.highlightNodes.has(node) ? node === hoverNode ? 'rgb(255,0,0,1)' : 'rgba(255,160,0,0.8)' : 'rgba(0,255,255,0.6)';
}
getLinkColor(link) {
if ('type' in link) {
return edgeColors[link.type]
}
return 'rgb(255, 255, 255)'
}
getLinkWidth(link) {
return this.highlightLinks.has(link) ? 2 : 0.8;
}
onNodeHover(node) {
// no state change
if ((!node && !this.highlightNodes.size) || (node && this.hoverNode === node)) return;
this.highlightNodes.clear();
this.highlightLinks.clear();
if (node) {
this.highlightNodes.add(node);
node.neighbors.forEach(neighbor => this.highlightNodes.add(neighbor));
node.links.forEach(link => this.highlightLinks.add(link));
}
this.hoverNode = node || null;
}
onLinkHover(link) {
this.highlightNodes.clear();
this.highlightLinks.clear();
if (link) {
this.highlightLinks.add(link);
this.highlightNodes.add(link.source);
this.highlightNodes.add(link.target);
}
this.updateHighlight();
}
getLinkClasses() {
const linkClasses = [];
this.graph.graphData().links.forEach(link => linkClasses.push(link.type));
return [... new Set(linkClasses)];
}
mapEdgeColors() {
const linkClasses = this.getLinkClasses()
for (let i = 0; i < linkClasses.length; i++) {
edgeColors[linkClasses[i]] = colorPallette[i % colorPallette.length]
}
}
updateLinks() {
const gData = this.graph.graphData()
// cross-link node objects
gData.links.forEach(link => {
const a = link.source;
const b = link.target;
if (!a.neighbors) a.neighbors = [];
if (!b.neighbors) b.neighbors = [];
a.neighbors.push(b);
b.neighbors.push(a);
if (!a.links) a.links = [];
if (!b.links) b.links = [];
a.links.push(link);
b.links.push(link);
});
this.graph.graphData(gData)
}
updateHighlight() {
// trigger update of highlighted objects in scene
this.graph
.nodeColor(this.graph.nodeColor())
.linkWidth(this.graph.linkWidth())
.linkDirectionalParticles(this.graph.linkDirectionalParticles());
}
addBackground() {
const sphereGeometry = new THREE.SphereGeometry(20000, 32, 32);
//const planeGeometry = new THREE.PlaneGeometry(1000, 1000, 1, 1);
const loader = new THREE.TextureLoader();
//const planeMaterial = new THREE.MeshLambertMaterial({color: 0xFF0000, side: THREE.DoubleSide}); //THREE.BackSide
const planeMaterial = new THREE.MeshBasicMaterial({map: loader.load(plugin_path + 'backgrounds/background_4.jpg'), side: THREE.DoubleSide}); //THREE.BackSide
const mesh = new THREE.Mesh(sphereGeometry, planeMaterial);
mesh.position.set(0, 0, 0);
//mesh.rotation.set(0.5 * Math.PI, 0, 0);
this.graph.scene().add(mesh);
}
focusOnNode(node) {
// Aim at node from outside it
const distance = 250;
const distRatio = 1 + distance/Math.hypot(node.x, node.y, node.z);
this.graph.cameraPosition(
{ x: node.x * distRatio, y: node.y * distRatio, z: node.z * distRatio }, // new position
node, // lookAt ({ x, y, z })
1000 // ms transition duration
);
}
focusOnNodeId(id) {
const gData = this.graph.graphData();
gData.nodes.forEach(node => {
if (node.id === id) {
this.onNodeClick(node);
}
})
}
resizeGraph() {
this.graph.width(getWidth());
this.graph.height(getHeight());
}
initializeModel() {
if (this.firstTick) {
this.mapEdgeColors();
this.updateLinks();
this.addBackground();
loadComponents();
this.firstTick = false;
}
}
drawNode(node) {
// Draw node as label + image
const nodeDiv = document.createElement('div');
const group = new THREE.Group();
const labelDiv = document.createElement('div')
labelDiv.textContent = node.name;
labelDiv.style.color = node.color;
labelDiv.className = 'node-label';
nodeDiv.appendChild(labelDiv);
const cssobj = new THREE.CSS3DSprite(nodeDiv);
cssobj.scale.set(0.25, 0.25, 0.25);
cssobj.position.set(0, -6, 0);
group.add(cssobj)
// Draw node circle image
const textureLoader = new THREE.TextureLoader();
const imageAlpha = textureLoader.load(plugin_path + 'datasets/images/alpha.png');
let imageTexture = null;
if ('image' in node) {
imageTexture = textureLoader.load(plugin_path + 'datasets/images/' + node.image);
} else {
imageTexture = textureLoader.load(plugin_path + 'datasets/images/default.jpg');
}
const material = new THREE.SpriteMaterial({map: imageTexture, alphaMap: imageAlpha, transparent: true,
alphaTest: 0.2, depthWrite:false, depthTest: false});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 999; // This may not be optimal. But it allows us to render the sprite on top of everything else.
if ('image' in node) {
sprite.scale.set(20, 20);
} else {
sprite.scale.set(5, 5);
}
group.add(sprite)
return group;
}
}
const dataUrl = plugin_path + 'datasets/aud1.json'
G = new Graph(dataUrl);
linkoverlay = new LinkOverlay(G);
infooverlay = new InfoOverlay();
G.infooverlay = infooverlay;
function loadComponents() {
linkoverlay.create();
infooverlay.create();
add_fullscreen_mode_button();
add_fullscreen_Listener();
window.onresize = G.resizeGraph();
}
function getWidth() {
return document.getElementById('3d-graph').offsetWidth;
}
function getHeight() {
return window.innerHeight - 200
}
function getCanvasDivNode() {
const domNode = document.getElementById('3d-graph');
return domNode.firstChild.firstChild.firstChild;
}
function add_fullscreen_mode_button() {
const sceneNode = document.getElementById('3d-graph');
const overlayNode = document.createElement('button');
overlayNode.className = 'fullscreen_button';
overlayNode.innerText = 'fullscreen';
overlayNode.addEventListener("click", fullscreen_mode);
sceneNode.appendChild(overlayNode);
}
function fullscreen_mode(){
if(getCanvasDivNode().requestFullscreen) {
getCanvasDivNode().requestFullscreen().catch();
}
}
function resize_canvas() {
if(document.fullscreenElement == getCanvasDivNode()) {
G.graph.height(screen.height);
G.graph.width(screen.width);
} else {
G.graph.height(window.innerHeight - 200);
G.graph.width(getWidth());
}
}
function add_fullscreen_Listener() {
getCanvasDivNode().addEventListener("fullscreenchange", resize_canvas);
}