HierarchicLayouter: Getting a Node's Layer Number
Applies to: yFiles 2.4, yFiles 2.3, yFiles 2.2, yFiles 2.1, yFiles 2.0, yFiles.NET 2.4, yFiles.NET 2.3, yFiles.NET 2.2 print article email article

Type: Tips & Tricks

Categories this article belongs to:
yFiles for Java > yFiles Layout > Automatic Graph Layout > Hierarchical Layout

Getting a node's layer number independently of the layering strategy the Hierarchical Layouter applies.

Applying a hierarchical layout to a graph conceptually partitions the graph's node set into a number of node subsets. In a resulting drawing these subsets can be seen as distinct layers where the nodes are placed into (assuming, e.g., drawing direction "top to bottom", these layers are horizontally oriented).

To actually partition the node set, class HierarchicLayouter, which is responsible for computing hierarchical layouts, can be configured to use different so-called layering strategies. If the (zero-based) layer number of a node has to be derived programmatically, these layering strategies have to be properly taken into account.

The following sample code shows how to get the layer numbers of all nodes from a graph independently of the layering strategy the Hierarchical Layouter applies. Class LayerGetterStage is a wrapper around HierarchicLayouter that uses a node map (class NodeMap) to save the actual layer number for every node. This is done by encapsulating the active Layerer instance of class HierarchicLayouter and using it as a delegate thereafter.

import y.base.EdgeList;
import y.base.Graph;
import y.base.NodeCursor;
import y.base.NodeMap;

import y.layout.AbstractLayoutStage;
import y.layout.LayoutGraph;

import y.layout.hierarchic.HierarchicLayouter;
import y.layout.hierarchic.Layerer;

class LayerGetterStage extends AbstractLayoutStage
{
  private HierarchicLayouter hierarchic = null;
  
  public LayerGetterStage(LayoutGraph graph, NodeMap layers)
  {
    hierarchic = new HierarchicLayouter();
    Layerer l = hierarchic.getLayerer();
    hierarchic.setLayerer(new InternalLayererWrapper(l, graph, layers));
  }
  
  public boolean canLayout(LayoutGraph graph)
  {
    return hierarchic.canLayout(graph);
  }
  
  public void doLayout(LayoutGraph graph)
  {
    hierarchic.doLayout(graph);
  }
  
  class InternalLayererWrapper implements Layerer
  {
    private Layerer l = null;
    private Graph graph = null;
    private NodeMap layers = null;
    
    public InternalLayererWrapper(Layerer coreLayerer, LayoutGraph graph, NodeMap layers)
    {
      l = coreLayerer;
      this.graph = graph;
      this.layers = layers;
    }
    
    public int assignNodeLayer(LayoutGraph g, NodeMap nm, EdgeList el)
    {
      int result = l.assignNodeLayer(g, nm, el);
      
      for (NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
      {
        Node n = nc.node();
        layers.setInt(n, nm.getInt(n));
      }
      
      return result;
    }
  }
}
Class LayerGetterStage could be called using the following sample code. The sample code does:
  • First, a node map is created that will hold the layer number for every node.
  • This node map is given to the wrapped Hierarchical Layouter where it is filled with the actual values.
  • Afterwards, the layer number of every node from the graph is printed to the console.
// The layers will be stored in this node map.
NodeMap layers = graph.createNodeMap();

// Create a new wrapped hierarchical layouter that additionally 
// provides the layers.
LayerGetterStage lgs = new LayerGetterStage(graph, layers);

// Run camouflaged hierarchical layouter.
lgs.doLayout(graph);

// Now, earn the fruits.
for (NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
{
  Node n = nc.node();
  System.out.println("Node '" + n + "' is in layer " + layers.getInt(n) + ".");
}

// After the node map has been used dispose of it.
graph.disposeNodeMap(layers);

Note
Since yFiles 2.3 IncrementalHierarchicLayouter offers a much more convenient way of determining the layer of a node after the layout. See IncrementalHierarchicLayouter.LAYER_VALUE_HOLDER_DPKEY.

Keywords: hierarchic - Hierarchical - Layouter - node - layer - number - HierarchicLayouter

Provide feedback:
How useful was this article?    less 1 2 3 4 5 more
Email address (optional):
COPYRIGHT © 2008 yWorks · ALL RIGHTS RESERVED imprint | top | home