If the layout algorithms provided in the product do not
meet your needs, you can develop your own layout algorithms by subclassing ibm_ilog.graphlayout.GraphLayout.
When a subclass of ibm_ilog.graphlayout.GraphLayout is
created, it automatically fits into the generic IBM® ILOG® Dojo Diagrammer layout framework and benefits
from its infrastructure, such as:
- Generic parameters: see Base class parameters and features
- Notification of progress: see Using event listeners
- Capability to apply the layout separately for the connected components of a disconnected graph: see Laying out connected components of a disconnected graph
- Capability to lay out nested graphs: see Nested layouts
Example
To illustrate the basic ideas for defining a new layout,
the following simple example shows a possible implementation of the
simplest layout algorithm, the Random Layout. The new layout class
is called
MyRandomLayout
. The following code shows the skeleton of the class:
dojo.declare("MyRandomLayout", ibm_ilog.graphlayout.GraphLayout, { copy: function(){ return new MyRandomLayout(this); }, copyParameters: function(source){ this.inherited(arguments); // Now copy any specific parameters from this class... }, layout: function(){ // ... } });
The
copy
method is implemented,
because it is used when laying out a nested graph (see Nested layouts). Then, the method layout() of
the base class is implemented as follows:
layout: function(){ var model = this.getGraphModel(); var report = this.getLayoutReport(); var rect = this.getCalcLayoutRegion(); var xMin = rect.x; var yMin = rect.y; var xMax = rect.x + rect.width; var yMax = rect.y + rect.height; var nds = model.getNodes(); var count = nds.length; for (var i = 0; i < count; i++) { var node = nds[i]; if (this.isPreserveFixedNodes() && this.isFixed(node)) return; var x = xMin + (xMax - xMin) * Math.random(); var y = yMin + (yMax - yMin) * Math.random(); model.moveNode(node, x, y); this.callLayoutStepPerformedIfNeeded(); } report.code = ibm_ilog.graphlayout.GraphLayoutReport.LAYOUT_DONE; }