Idiosyncrasies with DefaultLayoutGraph in a Layout-only Environment
Applies to: yFiles for Java 2.7, yFiles for Java 2.6 print article email article

Type: Troubleshooting

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

Considerations regarding node sizes and label setup when working with DefaultLayoutGraph.

Working with DefaultLayoutGraph

Class DefaultLayoutGraph is the default graph structure implementation available in the yFiles Layout distribution. It makes a graph structure available to automatic layout algorithms by providing both positional and dimensional information for graph elements.
When working with this class, the following aspects should be kept in mind:

There is no predefined default node size

Class DefaultLayoutGraph stores for each of its nodes an implementation of interface NodeLayout which holds the node's layout information. The layout information comprises the coordinates for the node's upper left corner as well as its width and height.
By default, class DefaultNodeLayout is used as the NodeLayout implementation. Instances of this class are created and stored lazily by DefaultLayoutGraph.

Initially, i.e. upon instantiation, both width and height in a DefaultNodeLayout instance are zero (0.0). When applying a layout algorithm to a graph with zero-sized nodes, however, such nodes might lead to unexpected results. In order to achieve reliable results it is necessary that nodes have non-zero width and height set.
The following code snippet outlines how to set a default node size for all nodes in a DefaultLayoutGraph using the LayoutGraph.setSize method.

// 'graph' is of type y.layout.DefaultLayoutGraph.

for (NodeCursor nc = graph.nodes(); nc.ok(); nc.next()) {
  graph.setSize(nc.node(), 30, 30);
}

Alternatively, you could also use a customized variant of DefaultLayoutGraph as presented in the following code snippet, for example.

DefaultLayoutGraph myGraph = new DefaultLayoutGraph() {
  protected NodeLayout createNodeLayout() {
    NodeLayout nodeLayout = super.createNodeLayout();
    nodeLayout.setSize(30, 30);
    return nodeLayout;
  }
};

Labels are actually only rectangular boxes

DefaultLayoutGraph provides no direct support for setting a label text with a node (or an edge), i.e., in particular there is no setLabelText method or similar. Nevertheless, rectangular boxes that define a label's layout (i.e., size and location) are supported and can be set using the setLabelLayout methods.
Usually, the size of the box is derived from the bounding box of the label text being displayed using some font. Note that the location of the box is not necessarily needed when an automatic labeling algorithm is used to find the box's location according to some label model.

For example, setting the box for a node label can be done as follows:

// 'graph' is of type y.layout.DefaultLayoutGraph.
// 'myNode' is of type y.base.Node.

NodeLabelLayoutImpl nodeLabelLayout = new NodeLabelLayoutImpl();
nodeLabelLayout.setBox(new YRectangle(0, 0, 80, 20));
graph.setLabelLayout(myNode, nodeLabelLayout);

Setting the box for an edge label can be done as follows:

// 'graph' is of type y.layout.DefaultLayoutGraph.
// 'myEdge' is of type y.base.Edge.

EdgeLabelLayoutImpl edgeLabelLayout = new EdgeLabelLayoutImpl();
edgeLabelLayout.setBox(new YRectangle(0, 0, 80, 40));
graph.setLabelLayout(myEdge, new EdgeLabelLayout[]{ edgeLabelLayout });

Keywords: DefaultLayoutGraph - DefaultNodeLayout - setSize - size - label - box - setBox - layout - text - rectangle - NodeLabelLayout - EdgeLabelLayout - NodeLabelLayoutImpl - EdgeLabelLayoutImpl - setBox - setLabelLayout - font - NodeLayout - zero - width - height - dimension

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