How to reload graph in cytoscape.js?
Asked Answered
C

3

6

Is there a function that resets the graph to it's freshly loaded state? I've tried cy.reset() but that just resets zoom and pan, not restoring to the virgin graph.

Also is there a way to restore all removed elements?

Thanks!

Climactic answered 22/8, 2013 at 22:36 Comment(0)
D
3

Call cy.elements().remove() and cy.add() with the same graph data.

Disario answered 23/8, 2013 at 14:17 Comment(3)
Thanks Max. Is it possible to request this function for the next release?Climactic
It's already there.cytoscape.github.io/cytoscape.js/#core/graph-manipulation/…Disario
Thanks Max! I called cy.elements().remove() and cy.add(), but some styling on the nodes is missing. I tried to call cy.style() and cy.resize(), but it didn't fix the styling. How do I fix it?Orthochromatic
X
0

Save in a variable the elements you removed, e.g.:

const elsRemoved = cy.elements().remove();

Then, for restoring them simply call:

elsRemoved.restore();

Refer to https://js.cytoscape.org/#eles.restore for more info

Xylol answered 8/4, 2021 at 16:56 Comment(0)
A
0

With data (here, a .cyjs file exported from Cytoscape), loaded via a function into Cytoscape.js, and redrawn at the click of a button.

<head>
  <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>

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

  <script>
  $.getJSON("ontology2.cyjs", function (data) {
    var cy = cytoscape({
      container: document.getElementById('cy'),
      elements: data.elements,
      // ...
      });

    // Reset graph:
    $('button.reset')
        .on("click", function() {
            cy.remove('node["*"]');
            cy.add(data.elements);
        });
    });
  </script>

  <button class="reset">Reset</button>
<body>

cytoscape.js reset button demo

Albuquerque answered 2/6, 2021 at 21:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.