javascriptresizecytoscape.js

Cytoscape.js: calculate graph dimensions at current zoom level?


I would like to resize both a) the container DOM element and b) the zoom level based on the height of my graph.

Suppose I add nodes and edges dynamically to the graph. Given a maximum zoom level (zMax) and a maximum container height (cMax), I'd like to fit according to the following rules:

The problem is, I am not sure of any built-in functionality for calculating the height of the graph in pixels at the current zoom level. I suppose I could iterate over the nodes and find the lowest and highest y-values, but I am hoping there is a more efficient option.


Solution

  • You should use functions that return values in rendered (on-screen) co-ordinates.

    See http://js.cytoscape.org/#notation/position

    See also http://js.cytoscape.org/#eles.renderedBoundingBox

    E.g. cy.elements().renderedBoundingBox().h is the rendered height of the entire graph.