-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
app.js
434 lines (389 loc) · 11.1 KB
/
app.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
/* global window,document */
import React, {Component} from 'react';
import {render} from 'react-dom';
import {json as requestJSON, csv as requestCSV} from 'd3-request';
import DeckGLOverlay from './deckgl-overlay';
import {default as GraphBasic} from './graph-layer/adaptor/graph-basic';
import {default as GraphFlare} from './graph-layer/adaptor/graph-flare';
import {default as GraphSNAP} from './graph-layer/adaptor/graph-snap';
// change this to load a different sample dataset:
// 0: typed data, with types represented as icons
// 1: named data, to show labeling on interaction
// 2: larger dataset
const DATASET = 1;
class Root extends Component {
//
// React lifecycle
//
constructor(props) {
super(props);
this._resize = this._resize.bind(this);
this._animate = this._animate.bind(this);
this._onHover = this._onHover.bind(this);
this._onClick = this._onClick.bind(this);
this._onMouseDown = this._onMouseDown.bind(this);
this._onMouseMove = this._onMouseMove.bind(this);
this._onMouseUp = this._onMouseUp.bind(this);
this._getNodeColor = this._getNodeColor.bind(this);
// set initial state
this.state = {
viewport: {
width: 500,
height: 500
},
data: null,
iconMapping: null,
hovered: null,
clicked: null,
dragging: null,
lastDragged: null
};
/* eslint-disable max-len */
const dataConfig = [
{
data:
'https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/graph/sample-graph.json',
loader: requestJSON,
adaptor: GraphBasic,
hasNodeTypes: true
},
{
data:
'https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/graph/flare.json',
loader: requestJSON,
adaptor: GraphFlare,
hasNodeTypes: false
},
{
data:
'https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/graph/facebook-SNAP.csv',
loader: requestCSV,
adaptor: GraphSNAP,
hasNodeTypes: false
}
];
/* eslint-enable max-len */
const loader = dataConfig[DATASET].loader;
loader(dataConfig[DATASET].data, (error, response) => {
if (!error) {
// "adaptors" are used to parse data into the format
// required by your layout (e.g. nodes and links arrays for d3-force),
// and to manage addition / removal of graph elements.
const GraphAdaptor = dataConfig[DATASET].adaptor;
const graph = new GraphAdaptor(response);
this.setState({
data: [graph]
});
}
});
// only set up icon accessors for sample datasets that have types to be represented as icons.
if (dataConfig[DATASET].hasNodeTypes) {
// load icon atlas
requestJSON('./data/node-icon-atlas.json', (error, response) => {
if (!error) {
this.setState({
iconMapping: response
});
}
});
}
}
componentDidMount() {
window.addEventListener('resize', this._resize);
this._resize();
this._animate();
}
shouldComponentUpdate() {
// Prevent component from updating while animation is running,
// to ensure layout updating and rendering are synchronized.
return !this._animationFrame;
}
componentDidUpdate() {
/* eslint-disable react/no-did-update-set-state */
// lastDragged state lasts only one frame
const {lastDragged} = this.state;
if (lastDragged) {
this.setState({
lastDragged: null
});
}
/* eslint-enable react/no-did-update-set-state */
}
componentWillUnmount() {
this._stopAnimation();
}
_animate() {
this.forceUpdate();
if (typeof window !== 'undefined') {
this._animationFrame = window.requestAnimationFrame(this._animate);
}
}
_stopAnimation() {
window.cancelAnimationFrame(this._animationFrame);
this._animationFrame = null;
}
_resize() {
this._onViewportChange({
width: window.innerWidth,
height: window.innerHeight
});
}
_onViewportChange(viewport) {
this.setState({
viewport: {...this.state.viewport, ...viewport}
});
}
_onHover(info) {
if (info) {
this.setState({hovered: info});
}
}
_onClick(info) {
if (info) {
this.setState({clicked: info});
} else {
const {clicked} = this.state;
if (clicked) {
this.setState({clicked: null});
}
}
}
_onMouseDown(event) {
// Use DeckGL.pickObject() to find the object under the mouse,
// and store it for updating on mouse move.
const info = this.deckGL.pickObject({x: event.clientX, y: event.clientY});
if (info) {
this.setState({
clicked: info,
dragging: {
node: info.object
}
});
this._updateDraggedElement([event.clientX, event.clientY], info.object);
}
}
_onMouseMove(event) {
if (this.state.dragging) {
this._updateDraggedElement([event.clientX, event.clientY], this.state.dragging.node);
}
}
_onMouseUp(event) {
if (this.state.dragging) {
this.setState({
dragging: null,
lastDragged: {
node: this.state.dragging.node
}
});
}
}
_updateDraggedElement(point, node) {
const {
viewport: {width, height}
} = this.state;
const x = point[0] - width / 2;
const y = point[1] - height / 2;
this.setState({
dragging: {
node,
x,
y
}
});
}
//
// layout (d3-force) accessors
//
_linkDistance(link, i) {
return 20;
}
_linkStrength(link, i) {
if (link.sourceCount || link.targetCount) {
return 1 / Math.min(link.sourceCount, link.targetCount);
}
return 0.5;
}
_nBodyStrength(node, i) {
if (node.size) {
return -Math.pow(node.size, 1.5) * 3;
}
return -60;
}
//
// rendering (deck.gl) accessors
//
_getNodeColor(node) {
return [18, 147, 154, 255];
}
_getNodeSize(node) {
return node.size || 8;
}
// map node type to icon in texture atlas
_getNodeIcon(node) {
switch (node.type) {
case 0:
return 'burger';
case 1:
return 'fries';
case 2:
return 'soda';
case 3:
case 4:
return 'pie';
default:
return null;
}
}
//
// rendering
//
/**
* The "interaction layer" is an SVG overlay used to apply highlights
* and labels to the targets of interaction (hovered and clicked/dragged elements).
*/
_renderInteractionLayer(viewport, hovered, clicked) {
// set flags used below to determine if SVG highlight elements should be rendered.
// if truthy, each flag is replaced with the corresponding element to render.
const elements = {
hovered: hovered && hovered.object,
clicked: clicked && clicked.object
};
const relatedElements = {
hovered: hovered && hovered.relatedObjects,
clicked: clicked && clicked.relatedObjects
};
const elementInfo = {
hovered: hovered && hovered.object,
clicked: clicked && clicked.object
};
// process related elements first, since they compare themselves to the focused elements.
// related elements are:
// - all links and nodes attached to a node; or
// - the nodes at each end of a link.
// see `graph-layout-layer::getPickingInfo()` for more information.
Object.keys(relatedElements).forEach(k => {
const els = relatedElements[k];
if (!els || !els.length) {
relatedElements[k] = null;
} else {
relatedElements[k] = [];
els.forEach(el => {
relatedElements[k].push(this._renderInteractionElement(el, `related ${k}`, viewport));
});
}
});
// process the focused (hovered / clicked) elements
Object.keys(elements).forEach(k => {
const el = elements[k];
elements[k] = el ? this._renderInteractionElement(el, k, viewport) : null;
});
// render additional info about the focused elements (only nodes, not links)
Object.keys(elementInfo).forEach(k => {
const el = elementInfo[k];
if (el && el.name) {
elementInfo[k] = (
<text x={el.x} y={el.y} dx={this._getNodeSize(el) + 10} dy={-10}>
{el.name}
</text>
);
} else {
elementInfo[k] = null;
}
});
// Note: node.x/y, calculated by d3 layout,
// is measured from the center of the layout (of the viewport).
// Therefore, we offset the <g> container to align.
return (
<svg width={viewport.width} height={viewport.height} className="interaction-overlay">
<g transform={`translate(${viewport.width / 2},${viewport.height / 2})`}>
{relatedElements.hovered}
{elements.hovered}
{relatedElements.clicked}
{elements.clicked}
{elementInfo.hovered}
{elementInfo.clicked}
</g>
</svg>
);
}
_renderInteractionElement(el, className, viewport) {
let element;
if (el.source) {
// link
element = (
<line
x1={el.source.x}
y1={el.source.y}
x2={el.target.x}
y2={el.target.y}
className={className}
key={`link-${className}-${el.id}`}
/>
);
} else {
// node
element = (
<circle
cx={el.x}
cy={el.y}
r={this._getNodeSize(el)}
className={className}
key={`node-${className}-${el.id}`}
/>
);
}
return element;
}
render() {
const {viewport, data} = this.state;
const {hovered, clicked, dragging, lastDragged} = this.state;
const layoutProps = {
fixedNodes: dragging ? [dragging] : null,
unfixedNodes: lastDragged ? [lastDragged] : null
};
const layoutAccessors = {
linkDistance: this._linkDistance,
linkStrength: this._linkStrength,
nBodyStrength: this._nBodyStrength
};
const linkAccessors = {};
const nodeAccessors = {
getNodeColor: this._getNodeColor,
getNodeSize: this._getNodeSize
};
let nodeIconAccessors;
const {iconMapping} = this.state;
if (iconMapping) {
// pass icon accessors if icon mapping is loaded
nodeIconAccessors = {
getIcon: this._getNodeIcon.bind(this),
iconAtlas: './data/node-icon-atlas.png',
iconMapping,
sizeScale: 4
};
}
return (
<div
onMouseDown={this._onMouseDown}
onMouseMove={this._onMouseMove}
onMouseUp={this._onMouseUp}
>
<DeckGLOverlay
// eslint-disable-next-line no-return-assign
deckGLRef={ref => (this.deckGL = ref)}
viewport={viewport}
data={data}
onHover={this._onHover}
onClick={this._onClick}
layoutProps={layoutProps}
layoutAccessors={layoutAccessors}
linkAccessors={linkAccessors}
nodeAccessors={nodeAccessors}
nodeIconAccessors={nodeIconAccessors}
/>
{this._renderInteractionLayer(viewport, hovered, dragging || clicked)}
</div>
);
}
}
render(<Root />, document.body.appendChild(document.createElement('div')));