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:
z == zMax && graphHeight < cMax
, then keep the zoom level constant and resize the container to fit the graph.graphHeight == cMax
, then reduce z
if possible.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.
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.