|
|
- <!doctype html>
- <html>
- <head>
- <title>Network | Basic usage</title>
-
- <script type="text/javascript" src="../../../../dist/vis.js"></script>
- <link href="../../../../dist/vis.css" rel="stylesheet" type="text/css" />
-
- <style type="text/css">
- #mynetwork {
- width: 600px;
- height: 400px;
- border: 1px solid lightgray;
- }
- </style>
- </head>
- <body>
-
- <p>
- Create a simple network with some nodes and edges.
- </p>
- <input type="button" onclick="addNode()" value="add node dynamically">
- <input type="button" onclick="changeNode1()" value="change node 1's color dynamically">
- <input type="button" onclick="removeRandomNode()" value="remove a random Node">
- <div id="mynetwork"></div>
-
- <script type="text/javascript">
- // this list is kept to remove a random node.. we do not add node 1 here because it's used for changes
- var nodeIds = [2,3,4,5];
-
- // create an array with nodes
- var nodes = new vis.DataSet([
- {id: 1, label: 'Node 1'},
- {id: 2, label: 'Node 2'},
- {id: 3, label: 'Node 3'},
- {id: 4, label: 'Node 4'},
- {id: 5, label: 'Node 5'}
- ]);
-
- // create an array with edges
- var edges = new vis.DataSet([
- {from: 1, to: 3},
- {from: 1, to: 2},
- {from: 2, to: 4},
- {from: 2, to: 5}
- ]);
-
- // create a network
- var container = document.getElementById('mynetwork');
- var data = {
- nodes: nodes,
- edges: edges
- };
- var options = {};
- var network = new vis.Network(container, data, options);
-
- function addNode() {
- var newId = (Math.random() * 1e7).toString(32);
- nodes.add({id:newId, label:"I'm new!"});
- nodeIds.push(newId);
- }
-
- function changeNode1() {
- var newColor = '#' + Math.floor((Math.random() * 255 * 255 * 255)).toString(16);
- nodes.update([{id:1, color:{background:newColor}}]);
- }
-
- function removeRandomNode() {
- var randomNodeId = nodeIds[Math.floor(Math.random() * nodeIds.length)];
- nodes.remove({id:randomNodeId});
-
- var index = nodeIds.indexOf(randomNodeId);
- nodeIds.splice(index,1);
- }
- </script>
-
- <script src="../../../googleAnalytics.js"></script>
- </body>
- </html>
|