【问题标题】:How to add onhover tags/ annotations to a network visualized using cytoscape.js如何将悬停标签/注释添加到使用 cytoscape.js 可视化的网络
【发布时间】:2020-02-05 21:59:56
【问题描述】:

我正在使用 Rstudio 并创建网络可视化,我正在使用 Cytoscape。 我找不到任何帮助文档来了解如何在鼠标悬停时向节点添加注释。 对于节点的 onmouseclick 事件,它已在 http://js.cytoscape.org/demos/colajs-graph/ 上完成。但我的要求是 onHover。

任何帮助或指导都会很有帮助。

【问题讨论】:

    标签: rstudio bioinformatics cytoscape.js bioconductor graph-visualization


    【解决方案1】:

    例如 tippy.js/popper.js 扩展,如 cytoscape.js documentation 中所述,可以处理此功能。使用 cytoscape 事件侦听器,您可以自己构建这些东西,但这里有一个用于您的用例的示例:

    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;
    }
    
    
    /* makes sticky faster; disable if you want animated tippies */
    
    .tippy-popper {
      transition: none !important;
    }
    <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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      相关资源
      最近更新 更多