vis.js 创建网络拓扑图

Vis Network | Node Styles | Images

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Vis Network | Node Styles | Images</title>

    <style type="text/css">
      #mynetwork {
        width: 600px;
        height: 600px;
        border: 1px solid lightgray;
      }
    </style>

    <script
      type="text/javascript"
      src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"
    ></script>

    <script type="text/javascript">
      var nodes = null;
      var edges = null;
      var network = null;

      var DIR = "img/refresh-cl/";
      var EDGE_LENGTH_MAIN = 150;
      var EDGE_LENGTH_SUB = 50;

      // Called when the Visualization API is loaded.
      function draw() {
        // Create a data table with nodes.
        nodes = [];

        // Create a data table with links.
        edges = [];

        nodes.push({
          id: 1,
          label: "Main",
          image: DIR + "Network-Pipe-icon.png",
          shape: "image",
        });
        nodes.push({
          id: 2,
          label: "Office",
          image: DIR + "Network-Pipe-icon.png",
          shape: "image",
        });
        nodes.push({
          id: 3,
          label: "Wireless",
          image: DIR + "Network-Pipe-icon.png",
          shape: "image",
        });
        edges.push({ from: 1, to: 2, length: EDGE_LENGTH_MAIN });
        edges.push({ from: 1, to: 3, length: EDGE_LENGTH_MAIN });

        for (var i = 4; i <= 7; i++) {
          nodes.push({
            id: i,
            label: "Computer",
            image: DIR + "Hardware-My-Computer-3-icon.png",
            shape: "image",
          });
          edges.push({ from: 2, to: i, length: EDGE_LENGTH_SUB });
        }

        nodes.push({
          id: 101,
          label: "Printer",
          image: DIR + "Hardware-Printer-Blue-icon.png",
          shape: "image",
        });
        edges.push({ from: 2, to: 101, length: EDGE_LENGTH_SUB });

        nodes.push({
          id: 102,
          label: "Laptop",
          image: DIR + "Hardware-Laptop-1-icon.png",
          shape: "image",
        });
        edges.push({ from: 3, to: 102, length: EDGE_LENGTH_SUB });

        nodes.push({
          id: 103,
          label: "network drive",
          image: DIR + "Network-Drive-icon.png",
          shape: "image",
        });
        edges.push({ from: 1, to: 103, length: EDGE_LENGTH_SUB });

        nodes.push({
          id: 104,
          label: "Internet",
          image: DIR + "System-Firewall-2-icon.png",
          shape: "image",
        });
        edges.push({ from: 1, to: 104, length: EDGE_LENGTH_SUB });

        for (var i = 200; i <= 201; i++) {
          nodes.push({
            id: i,
            label: "Smartphone",
            image: DIR + "Hardware-My-PDA-02-icon.png",
            shape: "image",
          });
          edges.push({ from: 3, to: i, length: EDGE_LENGTH_SUB });
        }

        // create a network
        var container = document.getElementById("mynetwork");
        var data = {
          nodes: nodes,
          edges: edges,
        };
        var options = {};
        network = new vis.Network(container, data, options);
      }
    </script>
  </head>

  <body onload="draw()">
    <p>Display nodes as images.</p>
    <div id="mynetwork"></div>
  </body>
</html>

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vis.js是一个用于可视化网络、图表和数据的JavaScript库。它提供了许多功能,包括节点移动和位置设置。以下是vis.js节点移动相关的教程。 1. 开始使用vis.js 在开始使用vis.js之前,需要引入vis.js库和vis.css样式文件。可以从vis.js官方网站上下载最新版本的vis.js库和vis.css样式文件,然后将它们添加到HTML页面中。 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>vis.js Node Move Tutorial</title> <link rel="stylesheet" type="text/css" href="vis.css"> <script type="text/javascript" src="vis.js"></script> </head> <body> <div id="network"></div> <script type="text/javascript"> // your code here </script> </body> </html> ``` 2. 创建一个简单的网络 在vis.js中,可以使用vis.Network对象来创建网络。首先,需要创建一个HTML元素,用于显示网络。然后,可以创建一个vis.Network对象,将其绑定到该HTML元素,并设置节点和边的数据。 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>vis.js Node Move Tutorial</title> <link rel="stylesheet" type="text/css" href="vis.css"> <script type="text/javascript" src="vis.js"></script> </head> <body> <div id="network"></div> <script type="text/javascript"> // 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: 2}, {from: 1, to: 3}, {from: 2, to: 4}, {from: 2, to: 5} ]); // create a network var container = document.getElementById('network'); var data = {nodes: nodes, edges: edges}; var options = {}; var network = new vis.Network(container, data, options); </script> </body> </html> ``` 3. 启用节点移动 要启用节点移动,需要在创建vis.Network对象时,将moveable选项设置为true。可以通过options对象来设置该选项。 ```javascript var options = {moveable: true}; var network = new vis.Network(container, data, options); ``` 现在,可以在网络中拖动节点并放置到新的位置。 4. 监听节点移动事件 如果希望在节点移动时执行一些代码,可以监听节点移动事件。可以通过vis.Network对象的on方法来注册事件处理程序。 ```javascript network.on('dragEnd', function(params) { console.log(params.nodes[0] + ' was moved to x:' + params.pointer.canvas.x + ', y:' + params.pointer.canvas.y); }); ``` 上面的代码将在节点移动结束时,将节点ID和新位置的坐标输出到控制台。 5. 限制节点移动范围 有时,希望限制节点移动的范围,以保持网络的视觉效果。可以通过设置节点位置的边界来实现。可以在options对象中设置nodes选项,将其设置为一个对象,该对象包含每个节点的初始位置和位置边界。 ```javascript var options = { moveable: true, nodes: { 1: {x: 100, y: 100, fixed: true, borderWidth: 1, shape: 'circle', color: {background:'red', border:'black'}}, 2: {x: 300, y: 100, borderWidth: 1, shape: 'circle', color: {background:'blue', border:'black'}, physics: false}, 3: {x: 100, y: 300, borderWidth: 1, shape: 'circle', color: {background:'green', border:'black'}, physics: false}, 4: {x: 300, y: 300, borderWidth: 1, shape: 'circle', color: {background:'yellow', border:'black'}, physics: false} } }; ``` 如上所示,节点1的位置已经被固定,而其他节点则没有被固定,并且可以在屏幕上移动。 6. 停用节点移动 要停用节点移动,设置moveable选项为false即可。 ```javascript var options = {moveable: false}; var network = new vis.Network(container, data, options); ``` 现在,节点将无法移动。 以上就是vis.js节点移动相关的教程。通过这些教程,您可以在vis.js中轻松地启用节点移动,监听节点移动事件,限制节点移动范围,并停用节点移动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值