VisJS 随机图



<!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>


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。