cytoscape.js - 在 cytoscape 中显示和隐藏鼠标悬停时的节点信息

标签 cytoscape.js

我正在浏览器中处理 cytoscape.js 图。当鼠标悬停在细胞景观图中的节点上时,我想显示节点的一些信息(例如节点标签)。以下代码适用于 console.log()但我想在浏览器中显示信息:

cy.on('mouseover', 'node', function(evt){
    var node = evt.target;
    console.log( 'mouse on node' + node.data('label') );
});

请帮忙 !

最佳答案

Cytoscape.js 有 popper.js带着醉意。我可以给你一个 popper.js 的工作示例:
弹出窗口宽度提示:

document.addEventListener("DOMContentLoaded", function() {
  var cy = (window.cy = cytoscape({
    container: document.getElementById("cy"),
    style: [{
        selector: "node",
        style: {
          content: "data(id)"
        }
      },
      {
        selector: "edge",
        style: {
          "curve-style": "bezier",
          "target-arrow-shape": "triangle"
        }
      }
    ],
    elements: {
      nodes: [{
        data: {
          id: "a"
        }
      }, {
        data: {
          id: "b"
        }
      }],
      edges: [{
        data: {
          id: "ab",
          source: "a",
          target: "b"
        }
      }]
    },
    layout: {
      name: "grid"
    }
  }));

  function makePopper(ele) {
    let ref = ele.popperRef(); // used only for positioning

    ele.tippy = tippy(ref, { // tippy options:
      content: () => {
        let content = document.createElement('div');

        content.innerHTML = ele.id();

        return content;
      },
      trigger: 'manual' // probably want manual mode
    });
  }

  cy.ready(function() {
    cy.elements().forEach(function(ele) {
      makePopper(ele);
    });
  });

  cy.elements().unbind('mouseover');
  cy.elements().bind('mouseover', (event) => event.target.tippy.show());

  cy.elements().unbind('mouseout');
  cy.elements().bind('mouseout', (event) => event.target.tippy.hide());

});
body {
  font-family: helvetica neue, helvetica, liberation sans, arial, sans-serif;
  font-size: 14px
}

#cy {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  z-index: 1;
}

h1 {
  opacity: 0.5;
  font-size: 1em;
  font-weight: bold;
}
<head>
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
  <script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script>
  <script src="https://unpkg.com/popper.js@1.14.7/dist/umd/popper.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/cytoscape-popper@1.0.4/cytoscape-popper.min.js"></script>
  <script src="https://unpkg.com/tippy.js@4.0.1/umd/index.all.min.js"></script>
  <link rel="stylesheet" href="https://unpkg.com/tippy.js@4.0.1/index.css" />
</head>

<body>
  <div id="cy"></div>
</body>

关于cytoscape.js - 在 cytoscape 中显示和隐藏鼠标悬停时的节点信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54547927/

相关文章:

layout - 父节点的广度优先布局,而不是子节点

cytoscape.js - 在 cytoscape.js 中的节点上方添加额外信息

javascript - Cytoscape 中的多个根节点

javascript - 任何 Javascript 图分析库都可以表明社区结构有多强?

javascript - 使用 Cytoscape.js 绘制二叉搜索树

javascript - 仅增加选定边缘的宽度 (cytoscape.js)

javascript - 使用 Cytoscape js 测量图形 Angular

cytoscape.js - 图形中的弹性(动画)边,节点对邻居施加引力?