Achieving Non-Overlapping Node and Edge Labeling

Questions & Answers

Summary

Automatically placing node and edge labels that do not overlap other graph elements.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

Support for automatic label placement is provided by the yFiles library in two ways.

First, the generic approach where the graph elements themselves are left untouched, but only the labels are moved. And second, the integrated variant where the labels are placed while a graph layout is calculated.

Package y.layout.labeling contains the algorithms for the generic approach, e.g., class GreedyMISLabeling. The layout algorithms that have label placement seamlessly integrated are HierarchicLayouter,
HierarchicGroupLayouter, IncrementalHierarchicLayouter, OrthogonalLayouter, OrthogonalGroupLayouter, DirectedOrthogonalLayouter, GenericTreeLayouter, BalloonLayouter, and SeriesParallelLayouter.

Generic Label Placement

The algorithms that follow the generic approach try to find label positions that do not overlap other graph elements. To this end, they can choose from a set of predefined label positions, that have been associated with a label by means of a so-called "label model."

Node labels and edge labels provide several type-specific label models, each having a predefined set of valid label positions. With class NodeLabel, for example, label model NodeLabel.CORNERS has four valid label positions. These are at the corners of a node, i.e., outside its bounds.

The tutorial source code demos LabelingDemo.java and LabelingModule.java show how to use these labeling algorithms. Since yFiles for Java 2.8, the labeling algorithms are demonstrated separately for node labels and edge labels in NodeLabelingDemo.java and EdgeLabelingDemo.java

Integrated Label Placement

The layout algorithms where labeling is integrated into the layout process itself can calculate a graph layout with respect to label position and size. That way, non-overlapping label positions can be guaranteed.

To guarantee non-overlapping label positions, it is highly recommended to use a 'free' label model for the edge labels. Such label models allow arbitrary positions for labels; concrete implementations are SmartEdgeLabelModel and FreeEdgeLabelModel. Non-free models on the other hand restrict the label placement to a (small) set of available locations. Therefore, even integrated labeling mechanisms cannot guarantee that there are no overlaps.

The tutorial source code demo HierarchicLayoutModule.java shows how to configure both generic approach as well as integrated edge labeling for class HierarchicLayouter.

Read more about automatic edge labeling in article Demystifying Automatic Edge Labeling.

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.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
Keywords:
automatic - label - placement - non-overlapping - labeling - node - edge - generic - integrated - overlap - minimize