-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleGraph.html
More file actions
327 lines (257 loc) · 9.97 KB
/
simpleGraph.html
File metadata and controls
327 lines (257 loc) · 9.97 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
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
<!DOCTYPE html>
<meta charset="utf-8">
<body bgcolor="#0a0a0a">
<!-- <body background="bgimage4.jpg"> -->
<svg width="width" height="height"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
@import url(http://fonts.googleapis.com/css?family=Source+Code+Pro:400,600);
body {font-family: "Source Code Pro", Consolas, monaco
, monospace; line-height: 60%; font-size: 16px; margin: 0; }
path.link {
fill: none;
stroke-width: 2px;
}
.node:not(:hover) .nodetext {
display: none;
}
h1 {color: #fffefe ; font-size: 26px; margin: 9px 0; text-transform: uppercase; font-weight: normal , font-colour: red;}
h2, h3 {color: #fffefe ; font-size: 10px; margin: 5px 0 ; font-weight: normal;}
header {padding: 8px; position: absolute; top: 0; left: 0;}
a:link { color: #EE3124; text-decoration: none;}
a:visited { color: #EE3124; }
a:hover { color: #A4CD39; text-decoration: underline;}
a:active { color: #EE3124; }
</style>
</head>
<header>
<h1>Product Process and Use</h1>
<h2>ThisGrah holds two vatiales:</h2>
<h2>baseNodes holds the i.d, group and level for each technology.</h2>
<h2>baseLinks holds the target, source and strength and for eatch technology.</h2>
</header>
<!-- container for force layout visualisation -->
<section id="vis"></section>
<script>
var baseNodes = [
{ id: "process", group: 0, label: "PROCESS", level: 1 , },
{ id: "coworking" , group: 0, label: "Co Working" , level: 2 , },
{ id: "blockchain" , group: 0, label: "Blockchain" , level: 2 },
{ id: "3dprint" , group: 0, label: "3D Print" , level: 2 },
{ id: "iot" , group: 0, label: "IoT" , level: 2 },
{ id: "product", group: 1, label: "PRODUCT", level: 1 },
{ id: "5g" , group: 1, label: "5G" , level: 2 },
{ id: "bim" , group: 1, label: "BIM" , level: 2 },
{ id: "use" , group: 2, label: "USE" , level: 1 },
{ id: "datacenters" , group: 2, label: "Data Centers" , level: 2 },
{ id: "digital twin" , group: 0, label: "Digital Twin" , level: 0 },
{ id: "darkFactories" , group: 2, label: "Dark factories" , level: 2 },
{ id: "foodfarms" , group: 2, label: "Indor Food Farms" , level: 2 },
{ id: "graphene" , group: 2, label: "Graphene" , level: 2 },
{ id: "thome" , group: 2, label: "Tiny Homes" , level: 2 },
{ id: "transalum" , group: 2, label: "Transparent aluminum" , level: 2 },
{ id: "carbonnano" , group: 2, label: "Carbon Nano Tube" , level: 2 }
]
var baseLinks = [
{ target: "use", source: "coworking" , strength: 0.7 },
{ target: "process", source: "blockchain" , strength: 0.5 },
{ target: "process", source: "3dprint" , strength: 0.2 },
{ target: "process", source: "iot" , strength: 0.2 },
{ target: "product", source: "5g" , strength: 0.1 },
{ target: "product", source: "bim" , strength: 0.1 },
{ target: "use" , source: "datacenters", strength: 0.3 },
{ target: "use" , source: "darkFactories", strength: 0.1 },
{ target: "datacenters" , source: "iot" , strength: 0.1 },
{ target: "digital twin" , source: "product" , strength: 0.3},
{ target: "digital twin" , source: "process" , strength: 0.1},
{ target: "product" , source: "carbonnano" , strength: 0.1 },
{ target: "carbonnano" , source: "product" , strength: 0.1 },
{ target: "bim" , source: "process" , strength: 0.1 },
{ target: "iot" , source: "5g" , strength: 0.1},
{ target: "foodfarms" , source: "use" , strength: 0.1},
{ target: "use" , source: "thome" , strength: 0.1},
{ target: "graphene" , source: "product" , strength: 0.1},
{ target: "product" , source: "transalum" , strength: 0.1},
{ target: "product" , source: "coworking" , strength: 0.1},
]
var nodes = [...baseNodes]
var links = [...baseLinks]
function getNeighbors(node) {
return baseLinks.reduce(function (neighbors, link) {
if (link.target.id === node.id) {
neighbors.push(link.source.id)
} else if (link.source.id === node.id) {
neighbors.push(link.target.id)
}
return neighbors
},
[node.id]
)
}
function isNeighborLink(node, link) {
return link.target.id === node.id || link.source.id === node.id
}
function getNodeColor(node, neighbors) {
if (Array.isArray(neighbors) && neighbors.indexOf(node.id) > -1) {
return node.level === 1 ? 'blue' : 'green'
}
return node.level === 1 ? 'red' : 'gray'
}
function getLinkColor(node, link) {
return isNeighborLink(node, link) ? 'green' : '#E5E5E5'
}
function getTextColor(node, neighbors) {
return Array.isArray(neighbors) && neighbors.indexOf(node.id) > -1 ? 'yellow' : 'gray'
}
var width = 1500
var height = 550
var svg = d3.select('svg')
svg.attr('width', 1800).attr('height', 1800)
var linkElements,
nodeElements,
textElements
// we use svg groups to logically group the elements together
var linkGroup = svg.append('g').attr('class', 'links')
var nodeGroup = svg.append('g').attr('class', 'nodes')
var textGroup = svg.append('g').attr('class', 'texts')
// we use this reference to select/deselect
// after clicking the same element twice
var selectedId
// simulation setup with all forces
var linkForce = d3
.forceLink()
.id(function (link) { return link.id })
.strength(function (link) { return link.strength })
var simulation = d3
.forceSimulation()
.force('link', linkForce)
.force('charge', d3.forceManyBody().strength(-120))
.force('center', d3.forceCenter(width / 2, height / 2))
var dragDrop = d3.drag().on('start', function (node) {
node.fx = node.x
node.fy = node.y
}).on('drag', function (node) {
simulation.alphaTarget(0.7).restart()
node.fx = d3.event.x
node.fy = d3.event.y
}).on('end', function (node) {
if (!d3.event.active) {
simulation.alphaTarget(0)
}
node.fx = null
node.fy = null
})
// select node is called on every click
// we either update the data according to the selection
// or reset the data if the same node is clicked twice
function selectNode(selectedNode) {
if (selectedId === selectedNode.id) {
selectedId = undefined
resetData()
updateSimulation()
} else {
selectedId = selectedNode.id
updateData(selectedNode)
updateSimulation()
}
var neighbors = getNeighbors(selectedNode)
// we modify the styles to highlight selected nodes
nodeElements.attr('fill', function (node) { return getNodeColor(node, neighbors) })
textElements.attr('fill', function (node) { return getTextColor(node, neighbors) })
linkElements.attr('stroke', function (link) { return getLinkColor(selectedNode, link) })
}
// this helper simple adds all nodes and links
// that are missing, to recreate the initial state
function resetData() {
var nodeIds = nodes.map(function (node) { return node.id })
baseNodes.forEach(function (node) {
if (nodeIds.indexOf(node.id) === -1) {
nodes.push(node)
}
})
links = baseLinks
}
// diffing and mutating the data
function updateData(selectedNode) {
var neighbors = getNeighbors(selectedNode)
var newNodes = baseNodes.filter(function (node) {
return neighbors.indexOf(node.id) > -1 || node.level === 1
})
var diff = {
removed: nodes.filter(function (node) { return newNodes.indexOf(node) === -1 }),
added: newNodes.filter(function (node) { return nodes.indexOf(node) === -1 })
}
diff.removed.forEach(function (node) { nodes.splice(nodes.indexOf(node), 1) })
diff.added.forEach(function (node) { nodes.push(node) })
links = baseLinks.filter(function (link) {
return link.target.id === selectedNode.id || link.source.id === selectedNode.id
})
}
function updateGraph() {
// links
linkElements = linkGroup.selectAll('line')
.data(links, function (link) {
return link.target.id + link.source.id
})
linkElements.exit().remove()
var linkEnter = linkElements
.enter().append('line')
.attr('stroke-width', 2)
.attr('stroke', 'rgba(0,255,255)')
linkElements = linkEnter.merge(linkElements)
// nodes
nodeElements = nodeGroup.selectAll('circle')
.data(nodes, function (node) { return node.id })
nodeElements.exit().remove()
var nodeEnter = nodeElements
.enter()
.append('circle')
.attr('r', 10)
.attr('fill', function (node) { return node.level === 1 ? 'blue' : 'purple' })
.attr('r', function (node) { return node.level === 1 ? '12': '8' })
.attr('stroke', function (node) { return node.level === 1 ? 'orange': 'white' })
.attr('stroke-width', function (node) { return node.level === 1 ? '3': '1' })
.call(dragDrop)
// we link the selectNode method here
// to update the graph on every click
.on('click', selectNode)
nodeElements = nodeEnter.merge(nodeElements)
// texts
textElements = textGroup.selectAll('text')
.data(nodes, function (node) { return node.id })
textElements.exit().remove()
var textEnter = textElements
.enter()
.append('text')
.text(function (node) { return node.label })
.attr('font-size', 12, )
.attr('font-family', "250sans-serif", )
.attr('fill', "white", )
.attr('font-size', function (node) { return node.level === 1 ? '14': '10' })
.attr('fill', function (node) { return node.level === 1 ? 'yellow': 'white ' })
.attr('dx', 16)
.attr('dy', 5)
textElements = textEnter.merge(textElements)
}
function updateSimulation() {
updateGraph()
simulation.nodes(nodes).on('tick', () => {
nodeElements
.attr('cx', function (node) { return node.x })
.attr('cy', function (node) { return node.y })
textElements
.attr('x', function (node) { return node.x })
.attr('y', function (node) { return node.y })
linkElements
.attr('x1', function (link) { return link.source.x })
.attr('y1', function (link) { return link.source.y })
.attr('x2', function (link) { return link.target.x })
.attr('y2', function (link) { return link.target.y })
})
simulation.force('link').links(links)
simulation.alphaTarget(0.7).restart()
}
// last but not least, we call updateSimulation
// to trigger the initial render
updateSimulation()
</script>