hemsby commited on
Commit
f30ab62
·
1 Parent(s): 1341700

feat: upgrade to vis-network 9.1, fix hover — fade unrelated, keep connected colored

Browse files
Files changed (1) hide show
  1. 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://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.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://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.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,45 +318,60 @@ function draw(id) {{
318
  }};
319
  var nodesDS = new vis.DataSet(data.nodes);
320
  var edgesDS = new vis.DataSet(data.edges);
321
- network = new vis.Network(document.getElementById('mynetwork'), {{ nodes: nodesDS, edges: edgesDS }}, options);
 
322
  document.getElementById('nodeCount').textContent = data.nodes.length;
323
  document.getElementById('edgeCount').textContent = data.edges.length;
324
 
325
- // Highlight connected nodes/edges on hover, fade the rest
 
 
 
 
 
 
 
 
 
 
 
 
 
326
  network.on('hoverNode', function(params) {{
327
- var hoveredId = params.node;
328
- var connected = network.getConnectedNodes(hoveredId);
329
- var connectedEdges = network.getConnectedEdges(hoveredId);
330
- var nodeUpdates = [];
 
331
  nodesDS.forEach(function(n) {{
332
- if (n.id === hoveredId || connected.indexOf(n.id) !== -1) {{
333
- nodeUpdates.push({{ id: n.id, opacity: 1.0 }});
334
- }} else {{
335
- nodeUpdates.push({{ id: n.id, opacity: 0.15 }});
336
  }}
337
  }});
338
- nodesDS.update(nodeUpdates);
339
- var edgeUpdates = [];
 
340
  edgesDS.forEach(function(e) {{
341
- if (connectedEdges.indexOf(e.id) !== -1) {{
342
- edgeUpdates.push({{ id: e.id, hidden: false }});
343
- }} else {{
344
- edgeUpdates.push({{ id: e.id, hidden: true }});
345
  }}
346
  }});
347
- edgesDS.update(edgeUpdates);
348
  }});
 
349
  network.on('blurNode', function() {{
350
- var nodeUpdates = [];
351
  nodesDS.forEach(function(n) {{
352
- nodeUpdates.push({{ id: n.id, opacity: 1.0 }});
 
353
  }});
354
- nodesDS.update(nodeUpdates);
355
- var edgeUpdates = [];
 
356
  edgesDS.forEach(function(e) {{
357
- edgeUpdates.push({{ id: e.id, hidden: false }});
358
  }});
359
- edgesDS.update(edgeUpdates);
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