|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang='en' type="module"> |
| 3 | +<head> |
| 4 | + <base href="."> |
| 5 | + <meta charset='UTF-8'> |
| 6 | + <title>Orb | Data dynamics: Update nodes and edges data</title> |
| 7 | + <script type="text/javascript" src="./orb.js"></script> |
| 8 | +</head> |
| 9 | +<style> |
| 10 | + html, body { |
| 11 | + height: 100%; |
| 12 | + margin: 0; |
| 13 | + } |
| 14 | +</style> |
| 15 | +<body> |
| 16 | + <div style="display: flex; flex-direction: column; align-items: center; height: 100%;"> |
| 17 | + <h1>Example 7 - Data dynamics</h1> |
| 18 | + <p style="width: 70%"> |
| 19 | + Renders a simple graph to show graph data dynamics: updating nodes and edges data. |
| 20 | + In intervals of 3 seconds, 1 node gets new size and 1 node gets new color |
| 21 | + (green or red) and a new border color (white or black). Node will change its |
| 22 | + unselected color to blue on node click event. |
| 23 | + </p> |
| 24 | + |
| 25 | + <!-- |
| 26 | + Make sure that your graph container has a defined width and height. |
| 27 | + Orb will expand to any available space, but won't be visible if it's parent container is collapsed. |
| 28 | + --> |
| 29 | + <div id='graph' style="flex: 1; width: 100%;"></div> |
| 30 | + </div> |
| 31 | + <script type="text/javascript"> |
| 32 | + const container = document.getElementById('graph'); |
| 33 | + |
| 34 | + const nodes = [ |
| 35 | + { id: 0, label: 'Node A' }, |
| 36 | + { id: 1, label: 'Node B' }, |
| 37 | + { id: 2, label: 'Node C' }, |
| 38 | + ]; |
| 39 | + const edges = [ |
| 40 | + { id: 0, start: 0, end: 0, label: 'A -> A' }, |
| 41 | + { id: 1, start: 0, end: 1, label: 'A -> B' }, |
| 42 | + { id: 2, start: 0, end: 2, label: 'A -> C' }, |
| 43 | + { id: 3, start: 1, end: 2, label: 'B -> C' }, |
| 44 | + { id: 4, start: 2, end: 2, label: 'C -> C' }, |
| 45 | + { id: 5, start: 0, end: 1, label: 'A -> B' }, |
| 46 | + ]; |
| 47 | + |
| 48 | + const n = nodes.length - 1; |
| 49 | + |
| 50 | + const orb = new Orb.OrbView(container); |
| 51 | + |
| 52 | + orb.data.setDefaultStyle({ |
| 53 | + getNodeStyle(node) { |
| 54 | + return { |
| 55 | + borderColor: '#1d1d1d', |
| 56 | + borderWidth: 0.6, |
| 57 | + color: '#DD2222', |
| 58 | + colorHover: '#e7644e', |
| 59 | + colorSelected: '#e7644e', |
| 60 | + fontSize: 3, |
| 61 | + label: node.getData().label, |
| 62 | + size: 6, |
| 63 | + }; |
| 64 | + }, |
| 65 | + getEdgeStyle(edge) { |
| 66 | + return { |
| 67 | + color: '#999999', |
| 68 | + colorHover: '#1d1d1d', |
| 69 | + colorSelected: '#1d1d1d', |
| 70 | + fontSize: 3, |
| 71 | + width: 0.3, |
| 72 | + widthHover: 0.9, |
| 73 | + widthSelected: 0.9, |
| 74 | + label: edge.getData().label, |
| 75 | + }; |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + // Initialize nodes and edges |
| 80 | + orb.data.setup({ nodes, edges }); |
| 81 | + |
| 82 | + const randomlyChangeGraphData = () => { |
| 83 | + console.log('Randomly changing graph data...'); |
| 84 | + setInterval(() => { |
| 85 | + const firstNodeId = Math.round(Math.random() * n); |
| 86 | + const secondNodeId = Math.round(Math.random() * n); |
| 87 | + |
| 88 | + orb.data.getNodeById(firstNodeId).patchStyle({ size: Math.random() * 5 + 2 }); |
| 89 | + orb.data.getNodeById(secondNodeId).patchStyle({ |
| 90 | + color: Math.random() > 0.5 ? "#FF0000" : "#00FF00", |
| 91 | + borderColor: Math.random() > 0.5 ? "#FFFFFF" : "#000000" |
| 92 | + }); |
| 93 | + }, 3000); |
| 94 | + } |
| 95 | + |
| 96 | + randomlyChangeGraphData(); |
| 97 | + |
| 98 | + orb.events.on(Orb.OrbEventType.NODE_CLICK, (event) => { |
| 99 | + console.log('Node clicked: ', event.node); |
| 100 | + orb.data.getNodeById(event.node.getId()).patchStyle({ color: "#0000FF" }); |
| 101 | + }); |
| 102 | + |
| 103 | + // Render and recenter the view |
| 104 | + orb.render(() => { |
| 105 | + orb.recenter(); |
| 106 | + }); |
| 107 | + |
| 108 | + </script> |
| 109 | +</body> |
| 110 | +</html> |
0 commit comments