Running Generic Labeling Algorithms Stand-alone |
| Applies to: yFiles 2.6, yFiles 2.5, 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 |
Type: Questions & Answers
Categories this article belongs to:
| yFiles for Java | > yFiles Layout | > Automatic Graph Layout | > Automatic Label Placement |
Automatic labeling can be calculated as part of a normal graph layout, but can also be run stand-alone.
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.
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.
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.
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 GreedyMISLabelingThe 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); |
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)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.
| Keywords: | generic - labeling - integrated - node - edge - label - subset - NodeLabelLayout - EdgeLabelLayout - GreedyMISLabeling - SALabeling - AbstractLabelingAlgorithm |


