cytoscape / cytoscape.js

Graph theory (network) library for visualisation and analysis
https://js.cytoscape.org
MIT License
10.09k stars 1.64k forks source link

cy.load not loading node positions #674

Closed dbeanm closed 10 years ago

dbeanm commented 10 years ago

In version 2.3.1, if I add some elements to the graph and then do

cy.load(cy.json()['elements'])

the positions of the nodes are not loaded and instead a grid layout is applied. I was loading from json with no problems using 2.2.10. The issue seems to be caused in the definition of cy.load by lines 6869-6872 of cytoscape.js:

var layoutOpts = $$.util.extend({}, cy._private.options.layout);
layoutOpts.eles = cy.$();

cy.layout( layoutOpts );

everything works as expected if I replace that block with:

cy.layout( cy._private.options.layout );
maxkfranz commented 10 years ago

(1) Have you tried using cy.add() instead? There's no need to use cy.load() unless you want to effectively reinit with the init options layout.

(2) You may alternatively run a different layout afterwards with cy.load():

cy.one('layoutstop', function(){
  cy.layout({ name: 'circle'  });
});

cy.load( eles );

(3) Your modification to cytoscape.js is broken, because it's not running the layout on any elements. layoutOpts.eles is mandatory internally (and set for you via eles.layout() etc).

dbeanm commented 10 years ago

1) I'm loading a saved state, so I do want to reset first but I can achieve the desired effect by first removing all elements, then using cy.add() as you said, thanks.

2) I want to use preset positions.

3) Thanks, I thought it probably would be! As I said, I don't want to run a layout on load.

So there's no way to use preset node positions with cy.load() now?

maxkfranz commented 10 years ago

cy.load() uses your init layout. If you want the preset layout on cy.load(), then use the preset layout in your init options.

maxkfranz commented 10 years ago

But really, there's no need to use cy.load() unless you want a layout (e.g. nodes don't yet have positions defined). Most people most of the time should be using cy.add().

Unless there's a bug in cy.load(), I'll close this for now.