feat: upgrade to vis-network 9.1, fix hover — fade unrelated, keep connected colored
Browse files- build_network.py +40 -25
build_network.py
CHANGED
|
@@ -231,11 +231,11 @@ def _build_html_template(*, chart_title, all_nodes, all_edges, driver_options,
|
|
| 231 |
<head>
|
| 232 |
<meta charset="utf-8">
|
| 233 |
<title>{chart_title} — Association Network</title>
|
| 234 |
-
<link type="text/css" href="https://
|
| 235 |
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
|
| 236 |
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
|
| 237 |
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
|
| 238 |
-
<script src="https://
|
| 239 |
<style>
|
| 240 |
* {{ box-sizing: border-box; margin: 0; padding: 0; }}
|
| 241 |
body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f8f9fa; color: #333; }}
|
|
@@ -318,45 +318,60 @@ function draw(id) {{
|
|
| 318 |
}};
|
| 319 |
var nodesDS = new vis.DataSet(data.nodes);
|
| 320 |
var edgesDS = new vis.DataSet(data.edges);
|
| 321 |
-
|
|
|
|
| 322 |
document.getElementById('nodeCount').textContent = data.nodes.length;
|
| 323 |
document.getElementById('edgeCount').textContent = data.edges.length;
|
| 324 |
|
| 325 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
network.on('hoverNode', function(params) {{
|
| 327 |
-
var
|
| 328 |
-
var connected = network.getConnectedNodes(
|
| 329 |
-
var
|
| 330 |
-
|
|
|
|
| 331 |
nodesDS.forEach(function(n) {{
|
| 332 |
-
if (n.id ==
|
| 333 |
-
|
| 334 |
-
}} else {{
|
| 335 |
-
nodeUpdates.push({{ id: n.id, opacity: 0.15 }});
|
| 336 |
}}
|
| 337 |
}});
|
| 338 |
-
nodesDS.update(
|
| 339 |
-
|
|
|
|
| 340 |
edgesDS.forEach(function(e) {{
|
| 341 |
-
if (
|
| 342 |
-
|
| 343 |
-
}} else {{
|
| 344 |
-
edgeUpdates.push({{ id: e.id, hidden: true }});
|
| 345 |
}}
|
| 346 |
}});
|
| 347 |
-
edgesDS.update(
|
| 348 |
}});
|
|
|
|
| 349 |
network.on('blurNode', function() {{
|
| 350 |
-
var
|
| 351 |
nodesDS.forEach(function(n) {{
|
| 352 |
-
|
|
|
|
| 353 |
}});
|
| 354 |
-
nodesDS.update(
|
| 355 |
-
|
|
|
|
| 356 |
edgesDS.forEach(function(e) {{
|
| 357 |
-
|
| 358 |
}});
|
| 359 |
-
edgesDS.update(
|
| 360 |
}});
|
| 361 |
}}
|
| 362 |
|
|
|
|
| 231 |
<head>
|
| 232 |
<meta charset="utf-8">
|
| 233 |
<title>{chart_title} — Association Network</title>
|
| 234 |
+
<link type="text/css" href="https://unpkg.com/vis-network@9.1.9/dist/dist/vis-network.min.css" rel="stylesheet" />
|
| 235 |
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
|
| 236 |
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
|
| 237 |
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
|
| 238 |
+
<script src="https://unpkg.com/vis-network@9.1.9/dist/vis-network.min.js"></script>
|
| 239 |
<style>
|
| 240 |
* {{ box-sizing: border-box; margin: 0; padding: 0; }}
|
| 241 |
body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f8f9fa; color: #333; }}
|
|
|
|
| 318 |
}};
|
| 319 |
var nodesDS = new vis.DataSet(data.nodes);
|
| 320 |
var edgesDS = new vis.DataSet(data.edges);
|
| 321 |
+
var netData = {{ nodes: nodesDS, edges: edgesDS }};
|
| 322 |
+
network = new vis.Network(document.getElementById('mynetwork'), netData, options);
|
| 323 |
document.getElementById('nodeCount').textContent = data.nodes.length;
|
| 324 |
document.getElementById('edgeCount').textContent = data.edges.length;
|
| 325 |
|
| 326 |
+
// Store original node styling for restore on blur
|
| 327 |
+
var origNodes = {{}};
|
| 328 |
+
nodesDS.forEach(function(n) {{
|
| 329 |
+
origNodes[n.id] = {{ color: n.color, font: n.font, borderWidth: n.borderWidth }};
|
| 330 |
+
}});
|
| 331 |
+
|
| 332 |
+
var FADED = {{
|
| 333 |
+
color: {{ background: '#f5f5f5', border: '#e0e0e0',
|
| 334 |
+
highlight: {{ background: '#f5f5f5', border: '#e0e0e0' }},
|
| 335 |
+
hover: {{ background: '#f5f5f5', border: '#e0e0e0' }} }},
|
| 336 |
+
font: {{ color: '#ccc' }},
|
| 337 |
+
borderWidth: 1
|
| 338 |
+
}};
|
| 339 |
+
|
| 340 |
network.on('hoverNode', function(params) {{
|
| 341 |
+
var hId = params.node;
|
| 342 |
+
var connected = network.getConnectedNodes(hId);
|
| 343 |
+
var connEdges = network.getConnectedEdges(hId);
|
| 344 |
+
|
| 345 |
+
var nUp = [];
|
| 346 |
nodesDS.forEach(function(n) {{
|
| 347 |
+
if (n.id !== hId && connected.indexOf(n.id) === -1) {{
|
| 348 |
+
nUp.push({{ id: n.id, color: FADED.color, font: FADED.font, borderWidth: FADED.borderWidth }});
|
|
|
|
|
|
|
| 349 |
}}
|
| 350 |
}});
|
| 351 |
+
nodesDS.update(nUp);
|
| 352 |
+
|
| 353 |
+
var eUp = [];
|
| 354 |
edgesDS.forEach(function(e) {{
|
| 355 |
+
if (connEdges.indexOf(e.id) === -1) {{
|
| 356 |
+
eUp.push({{ id: e.id, hidden: true }});
|
|
|
|
|
|
|
| 357 |
}}
|
| 358 |
}});
|
| 359 |
+
edgesDS.update(eUp);
|
| 360 |
}});
|
| 361 |
+
|
| 362 |
network.on('blurNode', function() {{
|
| 363 |
+
var nUp = [];
|
| 364 |
nodesDS.forEach(function(n) {{
|
| 365 |
+
var o = origNodes[n.id];
|
| 366 |
+
if (o) nUp.push({{ id: n.id, color: o.color, font: o.font, borderWidth: o.borderWidth }});
|
| 367 |
}});
|
| 368 |
+
nodesDS.update(nUp);
|
| 369 |
+
|
| 370 |
+
var eUp = [];
|
| 371 |
edgesDS.forEach(function(e) {{
|
| 372 |
+
eUp.push({{ id: e.id, hidden: false }});
|
| 373 |
}});
|
| 374 |
+
edgesDS.update(eUp);
|
| 375 |
}});
|
| 376 |
}}
|
| 377 |
|