本文介绍了在cytoscape中的鼠标悬停时显示和隐藏节点信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a cytoscape.js graph in browser. I want to show some information of nodes (e.g. node label) as the mouse hovers over the nodes in a cytoscape graph. The following code is working for console.log() but I want to show the information in the browser:

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

请帮助!

推荐答案

Cytoscape.js具有带有小费。我可以给你一个popper.js的实例:

Cytoscape.js has popper.js with tippy. I can give you a working exapmle for 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/[email protected]/dist/umd/popper.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/cytoscape-popper.min.js"></script>
  <script src="https://unpkg.com/[email protected]/umd/index.all.min.js"></script>
  <link rel="stylesheet" href="https://unpkg.com/[email protected]/index.css" />
</head>

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

这篇关于在cytoscape中的鼠标悬停时显示和隐藏节点信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 07:51