<!doctype html>
<html>
<head>
<title>Random nodes</title>
<style type="text/css">
body {
font: 13pt "courier new";
}
#mynetwork {
width: 600px;
height: 600px;
border: 8px solid blueviolet;
}
</style>
<script type="text/javascript" src="VisJS/dist/vis.js"></script>
<script type="text/javascript">
function draw(){
var nodes = [];
var edges = [];
var countConnected = [];
var nodeCount = document.getElementById( "nodeCount" ).value;
for( var i = 0; i < nodeCount; ++i ){
nodes.push( { id: i, label: String( i ) } );
countConnected[i] = 0;
if( i == 1 ){
edges.push( { from: i, to: 0 } );
countConnected[i]++;
countConnected[0]++;
}
else if( i > 1 ){
var rand = Math.floor( Math.random() * edges.length * 2 );
var limit = countConnected.length;
var val = 0;
var j = 0;
while( val < rand && j < limit ){
val += countConnected[j];
j++;
}
edges.push( { from: i, to: j } );
countConnected[i]++;
countConnected[j]++;
}
}
var container = document.getElementById( "mynetwork" );
var data = { nodes: nodes, edges: edges };
var options = { edges: {}, stabilize: false };
var net = new vis.Network( container, data, options );
}
</script>
</head>
<body onload="draw();">
<form onsubmit="draw(); return false;">
<label for="nodeCount">Number of nodes:</label>
<input id="nodeCount" type="text" value="10" style="width: 50px;">
<input type="submit" value="Go">
</form>
<br>
<div id="mynetwork"></div>
</body>
</html>
VisJS 随机图