Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show and hide node info on mouseover in cytoscape

Tags:

cytoscape.js

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') );
});

Please help !

like image 267
user10340258 Avatar asked Feb 06 '19 06:02

user10340258


People also ask

What is passthrough mapping in Cytoscape?

A passthrough mapping is typically used to specify node/edge labels. For example, a passthrough mapping can label all nodes with their common gene names.

How do you select multiple nodes in cytoscape?

Hold left-click and drag to select multiple nodes · Issue #810 · cytoscape/cytoscape.

How to change node shape in Cytoscape?

In the style control panel, click on the little triangle next to Shape to expand the selector, then select a column, select discrete mapping as mapping type then select a shape for each category of nodes.

How do you make a node in cytoscape?

Network Editing Cytoscape also has an edit functionality that enables you to build and modify networks interactively within the network canvas. To edit a network, just right-click on the open space of network window, select the menu item Add → Node, a new node will be added to the canvas.


1 Answers

Cytoscape.js has popper.js with tippy. I can give you a working exapmle for popper.js:

popper with tippy:

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>
like image 132
Stephan T. Avatar answered Sep 22 '22 01:09

Stephan T.