Running Generic Labeling Algorithms Stand-alone

Questions & Answers

Summary

Automatic labeling can be calculated as part of a normal graph layout, but can also be run stand-alone.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

Most often, node label positions and/or edge label positions are calculated as part of a normal layout algorithm run. In such a scenario, both generic labeling or integrated labeling can be used. (Integrated labeling is directly provided by some of the yFiles layout algorithms.)
Obviously however, when only label positions should be recalculated and neither the positions of the nodes, nor the paths of the edges should be modified, then only generic labeling is possible.

Generic Labeling

For generic labeling of only node labels and/or edge labels, the labeling algorithms from yFiles package y.layout.labeling, namely

GreedyMISLabeling and

SALabeling can be used directly.
The two algorithms represent the classic trade-off of quality vs. run-time: SALabeling yields better results than GreedyMISLabeling but is also slower.

The sample code snippet demonstrates how to invoke a labeling algorithm stand-alone.

// 'graph' is an implementation of interface y.layout.GraphLayout. 

new BufferedLayouter(new SALabeling()).doLayout(graph);
Starting with version 2.7 yFiles for Java Complete provides the new class Graph2DLayoutExecutor which eases performing a layout. We recommend using this class instead of using BufferedLayouter directly, if your distribution includes the Graph2DLayoutExecutor.

Running generic labeling stand-alone enables placing only a subset of labels.

Placing Only a Subset of Labels With Generic Labeling

Using the labeling algorithm's

label(LayoutGraph, YList, YList) method, it is possible to specify only assortments of node labels and edge labels that should be calculated new positions. The sample code below demonstrates how to recalculate edge label positions for all adjacent edges at a single node.

What the sample code snippet does:

  • Loops through all adjacent edges at the given node.
  • For every edge, retrieves the EdgeLabelLayout of all its edge labels. (Implementations of interface EdgeLabelLayout provide the geometric description of an edge label.)
  • Adds all EdgeLabelLayout objects to a list.
  • Invokes the given labeling algorithm with the composed list, so that only the positions of those edge labels are (re-)calculated.
public void recalculateEdgeLabelPositionsForAllAdjacentEdges(GraphLayout graph, Node node, AbstractLabelingAlgorithm la) { YList edgeLabels = new YList(); // Iterate over all adjacent edges of this node, i.e., incoming and outgoing ones. for (EdgeCursor ec = node.edges(); ec.ok(); ec.next()) { // Get the geometric descriptions for all the edge's labels. EdgeLabelLayout ell[] = graph.getEdgeLabelLayout(ec.edge()); // Append the descriptions to the list. for (int i = 0; i < ell.length; i++) edgeLabels.add(ell[i]); } // Invoke the given labeling algorithm. Only the edge label positions in the given list are recalculated. la.label(graph, null, edgeLabels); }

Node label positions can be recalculated in a similar manner. The geometry of node labels is held by NodeLabelLayout objects, which can be retrieved from the graph.

Categories this article belongs to:
yFiles for Java > yFiles Layout > Automatic Graph Layout > Automatic Label Placement
Applies to:
yFiles for Java 2: 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
yFiles.NET: 2.2, 2.3, 2.4
Keywords:
generic - labeling - integrated - node - edge - label - subset - NodeLabelLayout - EdgeLabelLayout - GreedyMISLabeling - SALabeling - AbstractLabelingAlgorithm