Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the start position of nodes in cytoscape.js with cy.load?

I am trying to set the start position of my nodes manually but loading them from a JSON structure. If I put the position within the 'nodes' structure it works, however I am trying to add a seperate structure called 'positions:'which I thought should work? See below for an example of the structure.

var graphdata = { nodes: [ { data: { id: 'j', name: 'Jerry', width: 20, height: 20 ,     shape: 'rectangle' }},
                       { data: { id: 't', name: 'Tom', width: 20 , height: 20, shape: 'circle' } },
                       { data: { id: 'm', name: 'Mary', width: 20 , height: 20, shape: 'circle' } } ,
                       { data: { id: 'b', name: 'Bob', width: 20 , height: 20, shape: 'circle' } } ],

              edges: [
                      { data: { source: 'j', target: 't', faveColor: '#6FB1FC', strength: 90 } },
                      { data: { source: 't', target: 'm', faveColor: '#6FB1FC', strength: 90 } },
                      { data: { source: 'm', target: 'b', faveColor: '#6FB1FC', strength: 90 } } ],

             positions: [ { j: { x:100,y:100 } }, 
                          { t: { x:100,y:200 } } ]

Later on I call cy.load(graphdata).

The nodes and edges display fine but the position doesn't seem to be affected at all.

I have also loaded the preset layout.

Thanks.

like image 509
user1768233 Avatar asked Oct 12 '25 21:10

user1768233


2 Answers

Is this Cytoscape Web or Cytoscape.js?

In Cytoscape.js,

  • just specify the positions on init in each element (http://cytoscape.github.io/cytoscape.js/#core/initialisation), or
  • specify the positions in the preset layout options and use that layout at init (http://cytoscape.github.io/cytoscape.js/#core/initialisation).
like image 82
maxkfranz Avatar answered Oct 14 '25 09:10

maxkfranz


Change the layout name to "preset", then the positions value will be considered. Ref - http://js.cytoscape.org/#layouts/preset

like image 26
vijay Avatar answered Oct 14 '25 09:10

vijay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!