The layout parameter of
cyto.Cytoscape takes as argument a
dictionary specifying how the nodes should be positioned on the screen.
Every graph requires this dictionary with a value specified for the
name key. It represents a built-in display method, which is one of the
All those layouts, along with their options, are described in the
official Cytoscape documentation.
There, you can find the exact keys accepted by your dictionary, enabling
advanced fine-tuning (demonstrated below).
If preset is given, the positions will be rendered based on the positions
specified in the elements. Otherwise, the positions will be computed by
Cytoscape.js behind the scene, based on the given items of the layout
dictionary. Let’s start with an example of declaring a graph with a preset
Here, we provided toy elements using geographically positioned nodes. If
you’d like to reproduce this example by yourself, check out the code
In most cases, the position of the nodes will not be given. In these
cases, one of the built-in methods can be used. Let’s see what happens
when the value of
name is set to
For any given
name item, a collection of keys are accepted by the layout
dictionary. For example, the
startAngle, and so forth. Here is the grid layout
with the same graph as above, but with different layout options:
In the case of the circle layout, we can force the nodes to start and end
at a certain angle in radians (import
math for this example):
breadthfirst layout, a tree is created from the existing nodes
by performing a breadth-first search of the graph. By default, the root(s)
of the tree is inferred, but can also be specified as an option. Here is
how the graph would look like if we choose New York City as the root:
Here is what would happen if we chose Montreal and Vancouver instead:
Notice here that we are not giving the ID of the nodes to the
key, but instead using a specific syntax to select the desired elements.
This concept of selector is extensively documented in Cytoscape.js,
and will be further explored in part 3.
For preset layouts, you can also specify the positions for which you would like to render each
of your nodes:
In the callbacks chapter, you will learn how to interactively update your layout; in order
preset, you will need to specify the position of each node.
cose layout can be used to position the nodes using
a force-directed layout by simulating attraction and repulsion among the
elements, based on the paper by
Dogrusoz et al, 2009.
External layouts are now available! Update your
version 0.1.1 or later.
In order to use them, you will need to use the
load_extra_layouts() function from
We also provided a
demo app directly derived from
but with the option to use the external layouts.
Make sure you use external layouts only when necessary. The distribution package takes
almost 3x more space than the regular bundle, which means that it will take more time to
load your apps, especially on slower networks.
shows how long it would take to load the dev package on a slower network.