码迷,mamicode.com
首页 > Web开发 > 详细

VisJS 随机图

时间:2014-07-19 02:45:26      阅读:479      评论:0      收藏:0      [点我收藏+]

标签:javascript   图论   


bubuko.com,布布扣

<!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 随机图

标签:javascript   图论   

原文地址:http://blog.csdn.net/pandora_madara/article/details/37930291

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!