Class ForceArrangement

java.lang.Object
com.codename1.graph.arrange.ForceArrangement
All Implemented Interfaces:
GraphArrangement

public class ForceArrangement extends Object implements GraphArrangement

An organic arrangement for networks: edges pull like springs and every node pushes every other away, until the graph settles.

This is Fruchterman and Reingold's method with a cooling schedule. Repulsion is summed through a Barnes-Hut quadtree, so a step costs n log n rather than n squared, and a graph of thousands of nodes settles in well under a second.

arrange runs every iteration at once. A view that animates the graph settling, or keeps it live while the user drags a node, calls start and then step once a frame instead. Starting positions come from the model, so a graph that was laid out before moves only as much as its changes need; nodes that are all at one point are first spread on a circle. The run is deterministic: the same graph gives the same picture.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Computes a position for every node, and a route for edges the arrangement bends.
    double
    The length an edge settles at, in world units, between node centres.
    int
    How many steps arrange runs, 300 by default.
    void
    hold(String nodeId, double centerX, double centerY)
    Holds a node at a centre position for the next steps; the view calls it while the user drags the node.
    void
    reheat(int steps)
    Keeps the run going for more steps, for instance while a node is dragged, at a temperature that lets the graph follow.
    void
    Pushes apart nodes that still overlap once the forces have settled, along the axis where they overlap least.
    The positions the run has reached.
    void
    setIdealLength(double idealLength)
    Sets the edge length.
    void
    setIterations(int iterations)
    Sets the number of steps.
    void
    Prepares a stepwise run from the model's current positions.
    boolean
    Moves every node one step towards rest.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ForceArrangement

      public ForceArrangement()
  • Method Details

    • getIdealLength

      public double getIdealLength()
      The length an edge settles at, in world units, between node centres.
    • setIdealLength

      public void setIdealLength(double idealLength)
      Sets the edge length.
    • getIterations

      public int getIterations()
      How many steps arrange runs, 300 by default.
    • setIterations

      public void setIterations(int iterations)
      Sets the number of steps.
    • arrange

      public ArrangementResult arrange(GraphModel model)
      Description copied from interface: GraphArrangement
      Computes a position for every node, and a route for edges the arrangement bends.
      Specified by:
      arrange in interface GraphArrangement
    • start

      public void start(GraphModel model)
      Prepares a stepwise run from the model's current positions.
    • step

      public boolean step()

      Moves every node one step towards rest.

      Returns

      false once the run has finished

    • reheat

      public void reheat(int steps)
      Keeps the run going for more steps, for instance while a node is dragged, at a temperature that lets the graph follow.
    • hold

      public void hold(String nodeId, double centerX, double centerY)
      Holds a node at a centre position for the next steps; the view calls it while the user drags the node.
    • result

      public ArrangementResult result()
      The positions the run has reached.
    • removeOverlaps

      public void removeOverlaps()
      Pushes apart nodes that still overlap once the forces have settled, along the axis where they overlap least. Neighbours are found through a grid of cells as large as the largest node, so a pass is linear.