vis.js 创建拓扑图编辑功能

Vis Network | Manipulation | Manipulation with Localizationicon-default.png?t=LA46https://visjs.github.io/vis-network/examples/network/other/manipulation.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Vis Network | Manipulation | Manipulation with Localization</title>

    <style type="text/css">
      body,
      select {
        font: 10pt sans;
      }
      #mynetwork {
        position: relative;
        width: 800px;
        height: 600px;
        border: 1px solid lightgray;
      }
      table.legend_table {
        font-size: 11px;
        border-width: 1px;
        border-color: #d3d3d3;
        border-style: solid;
      }
      table.legend_table,
      td {
        border-width: 1px;
        border-color: #d3d3d3;
        border-style: solid;
        padding: 2px;
      }
      div.table_content {
        width: 80px;
        text-align: center;
      }
      div.table_description {
        width: 100px;
      }

      #operation {
        font-size: 28px;
      }
      #network-popUp {
        display: none;
        position: absolute;
        top: 350px;
        left: 170px;
        z-index: 299;
        width: 250px;
        height: 120px;
        background-color: #f9f9f9;
        border-style: solid;
        border-width: 3px;
        border-color: #5394ed;
        padding: 10px;
        text-align: center;
      }
    </style>

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

    <script
      type="text/javascript"
      src="https://unpkg.com/alea@1.0.0/alea.js"
    ></script>
    <script type="text/javascript" src="../exampleUtil.js"></script>

    <script type="text/javascript">
      var nodes = null;
      var edges = null;
      var network = null;
      // randomly create some nodes and edges
      var data = getScaleFreeNetwork(25);
      var seed = 2;

      function setDefaultLocale() {
        var defaultLocal = navigator.language;
        var select = document.getElementById("locale");
        select.selectedIndex = 0; // set fallback value
        for (var i = 0, j = select.options.length; i < j; ++i) {
          if (select.options[i].getAttribute("value") === defaultLocal) {
            select.selectedIndex = i;
            break;
          }
        }
      }

      function destroy() {
        if (network !== null) {
          network.destroy();
          network = null;
        }
      }

      function draw() {
        destroy();
        nodes = [];
        edges = [];

        // create a network
        var container = document.getElementById("mynetwork");
        var options = {
          layout: { randomSeed: seed }, // just to make sure the layout is the same when the locale is changed
          locale: document.getElementById("locale").value,
          interaction: { keyboard: true },
          manipulation: {
            addNode: function (data, callback) {
              // filling in the popup DOM elements
              document.getElementById("operation").innerText = "Add Node";
              document.getElementById("node-id").value = data.id;
              document.getElementById("node-label").value = data.label;
              document.getElementById("saveButton").onclick = saveData.bind(
                this,
                data,
                callback
              );
              document.getElementById("cancelButton").onclick =
                clearPopUp.bind();
              document.getElementById("network-popUp").style.display = "block";
            },
            editNode: function (data, callback) {
              // filling in the popup DOM elements
              document.getElementById("operation").innerText = "Edit Node";
              document.getElementById("node-id").value = data.id;
              document.getElementById("node-label").value = data.label;
              document.getElementById("saveButton").onclick = saveData.bind(
                this,
                data,
                callback
              );
              document.getElementById("cancelButton").onclick = cancelEdit.bind(
                this,
                callback
              );
              document.getElementById("network-popUp").style.display = "block";
            },
            addEdge: function (data, callback) {
              if (data.from == data.to) {
                var r = confirm("Do you want to connect the node to itself?");
                if (r == true) {
                  callback(data);
                }
              } else {
                callback(data);
              }
            },
          },
        };
        network = new vis.Network(container, data, options);
      }

      function clearPopUp() {
        document.getElementById("saveButton").onclick = null;
        document.getElementById("cancelButton").onclick = null;
        document.getElementById("network-popUp").style.display = "none";
      }

      function cancelEdit(callback) {
        clearPopUp();
        callback(null);
      }

      function saveData(data, callback) {
        data.id = document.getElementById("node-id").value;
        data.label = document.getElementById("node-label").value;
        clearPopUp();
        callback(data);
      }

      function init() {
        setDefaultLocale();
        draw();
      }
    </script>
  </head>

  <body onload="init();">
    <h2>Editing the nodes and edges (localized)</h2>
    <p style="width: 700px; font-size: 14px; text-align: justify">
      The localization is only relevant to the manipulation buttons.
    </p>

    <p>
      <label for="locale">Select a locale:</label>
      <select id="locale" onchange="draw();">
        <option value="en">en</option>
        <option value="cn">cn</option>
        <option value="cs">cs</option>
        <option value="de">de</option>
        <option value="es">es</option>
        <option value="fr">fr</option>
        <option value="it">it</option>
        <option value="nl">nl</option>
        <option value="pt-br">pt</option>
        <option value="ru">ru</option>
        <option value="uk">uk</option>
      </select>
    </p>

    <div id="network-popUp">
      <span id="operation">node</span> <br />
      <table style="margin: auto">
        <tr>
          <td>id</td>
          <td><input id="node-id" value="new value" /></td>
        </tr>
        <tr>
          <td>label</td>
          <td><input id="node-label" value="new value" /></td>
        </tr>
      </table>
      <input type="button" value="save" id="saveButton" />
      <input type="button" value="cancel" id="cancelButton" />
    </div>
    <br />
    <div id="mynetwork"></div>
  </body>
</html>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值