3

I am new to javascript, and cytoscape as well. I want to generate a graph, (still hard coded)

  1. Now I need to zoom a clicked node..
  2. Also when a node is clicked, display a another graph

Appreciate your help

thank you

  • aka: `click-node-to-zoom in cytoscape`, aka [zoom-to-selected-node in cytoscape](https://ivis-at-bilkent.github.io/cytoscape.js-view-utilities/demo.html) aka [click node to fit graph in cytoscape](https://stackoverflow.com/a/23014149/10440128) aka [zoomable treemap in d3js](http://bl.ocks.org/guglielmo/16d880a6615da7f502116220cb551498) – milahu Nov 11 '21 at 08:24

1 Answers1

3

You can bind the click event on a node to cytoscape like this:

cy.bind('click ', 'node', function (evt) {
    var pos = cy.nodes("[id = " + evt.target.data().id + "]").position();
    cy.zoom({                       // Zoom to the specified position
      level: yourLevel,             // 0 <= yourLevel, maybe try out 1,2,3,4... and see what fits
      position: pos
    });
});

To display another graph, just do the same bind function and add the elements to the empty graph:

cy.bind('click', 'node', function (evt) {
    cy.remove(cy.elements());                 // remove elements and reset graph
    cy.reset();                               // graph is empty now
    cy.add(yourElements);                     // add new elements
    cy.elements().layout(yourLayout).run();   // give them a layout
});

At the end, you might want to cy.unbind() the bindings you have and bind them again, just to be sure you don't call these bind's multiple times.

Kind regards!

Stephan T.
  • 5,843
  • 3
  • 20
  • 42