Class ForceDirectedLayout

Extends Layout. Force-directed layout treats the graph as if it were a system of physical bodies with forces acting on them and between them. The algorithm seeks a configuration of the bodies with locally minimal energy, i.e. vertex positions such that the sum of the forces on each vertex is zero. There are many samples that use ForceDirectedLayout. The layout cannot guarantee that it provides optimal positioning of nodes. Nodes will normally not overlap each other, but when there is a dense interconnectivity overlaps might not be avoidable.

If you want to experiment interactively with most of the properties, try the Force Directed Layout sample. See samples that make use of ForceDirectedLayout in the samples index.

This layout makes use of a LayoutNetwork of ForceDirectedVertexes and ForceDirectedEdges that normally correspond to the Nodes and Links of the Diagram.

Constructor Summary Details

Name Description
ForceDirectedLayout()

Constructs a ForceDirectedLayout with no Layout.network and with no owning Layout.diagram.

Properties Summary Details

Name, Value Type Description
arrangementSpacing
{Size}

Gets or sets the space between which the layout will position the connected graphs that together compose the network.More... This defaults to Size(100, 100). These distances are used during a clustered layout; afterwards the normal force-directed layout will likely cause the size of any space between connected graphs to change, perhaps considerably.

arrangesToOrigin
{boolean}

Gets or sets whether commitNodes should move all of the nodes so that the nodes all fit with the top-left corner at the Layout.arrangementOrigin.More... By default this is false -- the Layout.arrangementOrigin is ignored. When this is true, nodes are moved even though isFixed was true.

comments
{boolean}

Gets or sets whether this layout should find all Nodes whose category is "Comment" and whose anchors are nodes represented in the network, and add ForceDirectedVertexes representing those balloon comments as nodes in the network.More... The default value is false.

currentIteration
{number}

This read-only property returns the current iteration count, valid during a call to doLayout.

defaultCommentElectricalCharge
{number}

Gets or sets the default value computed by electricalCharge.More... The initial value is 5.

defaultCommentSpringLength
{number}

Gets or sets the default value computed by springLength.More... The initial value is 10.

defaultElectricalCharge
{number}

Gets or sets the default value computed by electricalCharge.More... The initial value is 150.

defaultGravitationalMass
{number}

Gets or sets the default value computed by gravitationalMass.More... The initial value is zero.

defaultSpringLength
{number}

Gets or sets the default value computed by springLength.More... The initial value is 50.

defaultSpringStiffness
{number}

Gets or sets the default value computed by springStiffness.More... The initial value is 0.05.

epsilonDistance
{number}

Gets or sets approximately how far a node must move in order for the iterations to continue.More... The default value is 1. The value must be larger than zero.

infinityDistance
{number}

Gets or sets a threshold for the distance beyond which the electrical charge forces may be ignored.More... The default value is 1000. The value must be larger than 1.

maxIterations
{number}

Gets or sets the maximum number of iterations to perform when doing the force-directed auto layout.More... The value must be non-negative. The default value is 100.

randomNumberGenerator
{Object} 1.5

Gets or sets a random number generator.More... The default value is Math, which results in calling Math.random(). Change this to null in order to use an instance of an internal repeatable pseudo-random number generator, which will become the new value of this property.

The new value must be either null or an Object with a method named "random" taking zero arguments and returning a random number between zero (inclusive) and one (exclusive).

setsPortSpots
{boolean}

Gets or sets whether the fromSpot and the toSpot of every Link should be set to Spot.Default.More... The default value is true.

Properties borrowed from class Layout:
arrangementOrigin, diagram, group, isInitial, isOngoing, isRealtime, isRouting, isValidLayout, isViewportSized, network

Method Summary Details

Name, Return Type Description
addComments(v)
1.3

Find any associated objects to be positioned along with the LayoutVertex.node.More...

This method is called for each vertex in the network, when comments is true. The standard behavior is to look for Nodes whose Part.category is "Comment" and that refer to the LayoutVertex.node. By default this method will not be called unless you set comments to true.

You may want to override this method in order to customize how any associated objects are found and how a new ForceDirectedVertex and ForceDirectedEdge may be added to the network to represent the (balloon?) comment. This method sets the new vertex's ForceDirectedVertex.charge to the value of defaultCommentElectricalCharge, and sets the new edge's ForceDirectedEdge.length to the value of defaultCommentSpringLength.

Parameters:
{ForceDirectedVertex} v
commitLayout()

Set the fromSpot and toSpot on each Link, position each Node according to the vertex position, and then position/route the Links.More...

This calls the commitNodes and commitLinks methods, the latter only if isRouting is true. You should not call this method -- it is a "protected virtual" method. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

commitNodes()

Commit the position of all nodes.More...

This is called by commitLayout. See also commitLinks. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

createNetwork()
{ForceDirectedNetwork}

Create a new LayoutNetwork of ForceDirectedVertexes and ForceDirectedEdges.

Returns:
{ForceDirectedNetwork} a new LayoutNetwork.
doLayout(coll)

Perform the force-directed layout.More...

If there is no Layout.network, this calls makeNetwork to create a LayoutNetwork from the given collection of Parts. This removes any reflexive edges in the network, since they should be ignored.

For each vertex this calls and remembers the result of electricalCharge as the ForceDirectedVertex.charge and the result of gravitationalMass as the ForceDirectedVertex.mass.

For each edge this calls and remembers the result of springStiffness as the ForceDirectedEdge.stiffness and the result of springLength as the ForceDirectedEdge.length.

This then iterates, updating the position of each vertex according to the forces upon it, until reaching maxIterations or until no vertex moves more than about epsilonDistance.

Finally this calls Layout.updateParts to commit the Node positions from the vertex positions. Layout.updateParts calls commitLayout within a transaction.

Parameters:
{Diagram|Group|Iterable.} coll
A Diagram or a Group or a collection of Parts
electricalCharge(v)
{number}

Returns the charge of the vertex, the value of ForceDirectedVertex.charge if it's a number, or else the value of defaultElectricalCharge.More... Please read the Introduction page on Extensions for how to override methods and how to call this base method.

Parameters:
{ForceDirectedVertex} v
Returns:
{number}
electricalFieldX(x, y)
{number}

Returns the electrical field in the X direction acting on a vertex at the given point.More...

Used to define an external electrical field at a point independent of the vertex charges. A vertex L is acted upon by a force in the X direction of magnitude electricalFieldX(L.center.x, L.center.y) * electricalCharge(L). Please read the Introduction page on Extensions for how to override methods and how to call this base method.

Parameters:
{number} x
{number} y
Returns:
{number} the default implementation returns zero.
electricalFieldY(x, y)
{number}

Returns the electrical field in the Y direction acting on a vertex at the given point.More...

Used to define an external electrical field at a point independent of the vertex charges. A vertex L is acted upon by a force in the Y direction of magnitude electricalFieldY(L.center.x, L.center.y) * electricalCharge(L). Please read the Introduction page on Extensions for how to override methods and how to call this base method.

Parameters:
{number} x
{number} y
Returns:
{number} the default implementation returns zero.
gravitationalFieldX(x, y)
{number}

This returns the gravitational field in the X direction acting on a vertex at the given point.More...

Used to define an external gravitational field at a point independent of the vertex masses. A vertex L is acted upon by a force in the X direction of magnitude gravitationalFieldX(L.center.x, L.center.y) * gravitationalMass(L). Please read the Introduction page on Extensions for how to override methods and how to call this base method.

Parameters:
{number} x
{number} y
Returns:
{number} the default implementation returns zero.
gravitationalFieldY(x, y)
{number}

This returns the gravitational field in the Y direction acting on a vertex at the given point.More...

Used to define an external gravitational field at a point independent of the vertex masses. A vertex L is acted upon by a force in the Y direction of magnitude gravitationalFieldY(L.center.x, L.center.y) * gravitationalMass(L). Please read the Introduction page on Extensions for how to override methods and how to call this base method.

Parameters:
{number} x
{number} y
Returns:
{number} the default implementation returns zero.
gravitationalMass(v)
{number}

Returns the mass of the vertex, the value of ForceDirectedVertex.mass if it's a number, or else the value of defaultGravitationalMass.More... Please read the Introduction page on Extensions for how to override methods and how to call this base method.

Parameters:
{ForceDirectedVertex} v
Returns:
{number}
isFixed(v)
{boolean}

This predicate returns true if the vertex should not be moved by the layout algorithm but still have an effect on nearby and connected vertexes.More... The default implementation returns ForceDirectedVertex.isFixed. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

Parameters:
{ForceDirectedVertex} v
Returns:
{boolean} returns true if the node should not be moved by the layout algorithm.
moveFixedVertex(v)

Maybe move a vertex that isFixed.More... This is called each iteration on each such vertex. By default this does nothing.

Parameters:
{ForceDirectedVertex} v
springLength(e)
{number}

Returns the length of the spring representing an edge.More... The two vertexes connected by the edge E are acted upon by a force of magnitude springStiffness(E) * (getNodeDistance(E.fromVertex, E.toVertex) - springLength(E)). Please read the Introduction page on Extensions for how to override methods and how to call this base method.

Parameters:
{ForceDirectedEdge} e
Returns:
{number} Returns the length of the edge representing a link, the value of ForceDirectedEdge.length if it's a number, or else the value of defaultSpringLength.
springStiffness(e)
{number}

Returns the stiffness of the spring representing an edge.More... The two vertexes connected by the edge E are acted upon by a force of magnitude springStiffness(E) * (getNodeDistance(E.fromVertex, E.toVertex) - springLength(E)). Please read the Introduction page on Extensions for how to override methods and how to call this base method.

Parameters:
{ForceDirectedEdge} e
Returns:
{number} Returns the stiffness of the edge representing a link, the value of ForceDirectedEdge.stiffness if it's a number, or else the value of defaultSpringStiffness.
Methods borrowed from class Layout:
cloneProtected, collectParts, copy, invalidateLayout, makeNetwork, updateParts