-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathgraph-layer.js
More file actions
146 lines (128 loc) · 3.9 KB
/
Copy pathgraph-layer.js
File metadata and controls
146 lines (128 loc) · 3.9 KB
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
// deck.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import {CompositeLayer, IconLayer, LineLayer, ScatterplotLayer} from 'deck.gl';
const defaultProps = {
offset: {x: 0, y: 0},
coordinateSystem: 'cartesian',
getLinkPosition: link => ({
sourcePosition: [link.source.x, link.source.y],
targetPosition: [link.target.x, link.target.y]
}),
getLinkWidth: link => 2,
getLinkColor: link => [179, 173, 158, 255],
getNodePosition: node => [node.x, node.y, 0],
getNodeColor: node => node.color || [18, 147, 154, 255],
getNodeSize: node => node.radius || 8,
nodeIconAccessors: {}
};
/**
* GraphLayer renders a collection of nodes and links between them,
* for e.g. displaying a force-directed network graph.
* As it only handles the rendering, the layout and data must be managed
* by an adaptor component.
*
* It is a composite layer, comprising:
* - a LineLayer for drawing links
* - a ScatterplotLayer for drawing nodes, or node icon backgrounds if icons are specified
* - an IconLayer for drawing node icons, if icons are specified
*/
export default class GraphLayer extends CompositeLayer {
initializeState() {
this.state = {
nodes: {},
links: {}
};
}
updateState({oldProps, props, changeFlags}) {
if (changeFlags.dataChanged) {
const {data} = props;
if (data) {
this.state.nodes = data.nodes;
this.state.links = data.links;
this.state.layoutTime = props.layoutTime;
}
}
}
renderLayers() {
const {id} = this.props;
const {nodes, links, layoutTime} = this.state;
// Accessor props for underlying layers
const {
getLinkPosition,
getLinkColor,
getLinkWidth,
getNodePosition,
getNodeColor,
getNodeSize,
nodeIconAccessors
} = this.props;
const {getIcon, iconAtlas, iconMapping, sizeScale} = nodeIconAccessors || {};
// base layer props
const {opacity, pickable, visible} = this.props;
// viewport props
const {coordinateSystem} = this.props;
const drawLinks = links && links.length > 0;
const drawNodes = nodes && nodes.length > 0;
// only draw icons if all required accessors are present
const drawIcons = drawNodes && getIcon && iconAtlas && iconMapping;
const linksLayer =
drawLinks &&
new LineLayer({
id: `${id}-link-layer`,
data: links,
getSourcePosition: d => getLinkPosition(d).sourcePosition,
getTargetPosition: d => getLinkPosition(d).targetPosition,
getColor: e => (e.highlighting ? [255, 0, 0, 200] : getLinkColor(e)),
strokeWidth: getLinkWidth(),
opacity,
pickable,
coordinateSystem,
updateTriggers: {
getSourcePosition: layoutTime,
getTargetPosition: layoutTime,
getColor: layoutTime
}
});
const nodesLayer =
drawNodes &&
new ScatterplotLayer({
id: `${id}-${drawIcons ? 'node-bg-layer' : 'node-layer'}`,
data: nodes,
getPosition: getNodePosition,
getRadius: getNodeSize,
getFillColor: n => (n.highlighting ? [255, 255, 0, 255] : getNodeColor(n)),
opacity,
pickable,
coordinateSystem,
updateTriggers: {
getPosition: layoutTime,
getFillColor: layoutTime
},
visible
});
const nodeIconsLayer =
drawIcons &&
new IconLayer({
id: `${id}-node-icon-layer`,
data: nodes,
getColor: getNodeColor,
getIcon,
getPosition: getNodePosition,
getSize: getNodeSize,
iconAtlas,
iconMapping,
opacity,
pickable,
coordinateSystem,
sizeScale,
updateTriggers: {
getPosition: layoutTime
},
visible
});
return [linksLayer, nodesLayer, nodeIconsLayer];
}
}
GraphLayer.layerName = 'GraphLayer';
GraphLayer.defaultProps = defaultProps;