janhartmann
janhartmann

Reputation: 15003

Y-axis force with cola.js and Cytoscape

I have noticed that using Cola.js (with Cytoscape.js) most of my layouts tend to form in a square layout and not using up my bounding box which is more wide than tall.

I've been looking around and found d3-force which has this option of forceY that transform a square layout (https://bl.ocks.org/steveharoz/8c3e2524079a8c440df60c1ab72b5d03):

enter image description here

To this more wide layout:

enter image description here

I would really like to do the same for Cola.js, however I have been struggling to do it and tried all possible options like setting the bounding box, disabling zoom and etc. Is this possible at all?

I've found a demo for Cola.js that provides somewhat what I need, but unable to make it work in Cytoscape.js: https://ialab.it.monash.edu/webcola/examples/pageBoundsConstraints.html

Upvotes: 1

Views: 573

Answers (1)

Hasan Balcı
Hasan Balcı

Reputation: 1088

Based on the link you provide, you can apply a similar functionality with cola.js. You need to have two dummy nodes locked (one for top-left and one for bottom-right) and then add constraints for all other nodes appropriately. You can disable the visibility of dummy nodes (I left them as visible so we can see the top-left and bottom-right of the bounding box.). You can play with the position of dummy nodes to adjust your bounding box.

var cy = window.cy = cytoscape({
  container: document.getElementById('cy'),

  style: [{
      selector: 'node',
      css: {
        'content': 'data(id)',
        'text-valign': 'center',
        'text-halign': 'center'
      }
    },
    {
      selector: 'edge',
      css: {
        'curve-style': 'straight',
      }
    }
  ],
  layout: {
        name: 'preset'
  },
  elements: {
    nodes: [{
        data: {
          id: 'n0'
        }
      },
      {
        data: {
          id: 'n1'
        }
      },
      {
        data: {
          id: 'n2'
        }
      },
      {
        data: {
          id: 'n3'
        }
      },
      {
        data: {
          id: 'n4'
        }
      },      
      {
        data: {
          id: 'd0'
        },
        position: {x: 0, y:0}
      },
      {
        data: {
          id: 'd1'
        },
        position: {x: 400, y:150}
      }    
    ],
    edges: [{
        data: {
          id: 'n0n1',
          source: 'n0',
          target: 'n1'
        }
      },
      {
        data: {
          id: 'n1n2',        
          source: 'n1',
          target: 'n2'
        }
      },
      {
        data: {
          id: 'n2n3',        
          source: 'n2',
          target: 'n3'
        }
      },
      {
        data: {
          id: 'n4n1',        
          source: 'n4',
          target: 'n1'
        }
      }
    ]
  }
});

var tl = cy.getElementById('d0');
var br = cy.getElementById('d1');

tl.lock();
br.lock();

var realGraphNodes = cy.nodes().difference(tl.union(br));
var constraints = [];
    for (var i = 0; i < realGraphNodes.length; i++) {
        constraints.push({ axis: 'x', left: tl, right: realGraphNodes[i], gap: 100 });
        constraints.push({ axis: 'y', left: tl, right: realGraphNodes[i], gap: 100 });
        constraints.push({ axis: 'x', left: realGraphNodes[i], right: br, gap: 100 });
        constraints.push({ axis: 'y', left: realGraphNodes[i], right: br, gap: 100 });
    }

cy.layout({name: 'cola', randomize: true, animate: false, gapInequalities: constraints}).run();
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 95%;
  width: 95%;
  left: 0;
  top: 0;
  position: absolute;
}
<html>

<head>
  <meta charset=utf-8 />
  <meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
  <script src="https://unpkg.com/cytoscape@3.10.0/dist/cytoscape.min.js"></script>
  <script src="https://unpkg.com/webcola/WebCola/cola.min.js"></script>
  <script src="https://unpkg.com/cytoscape-cola/cytoscape-cola.js"></script>
</head>

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

</html>

Upvotes: 3

Related Questions