Achieving Non-Overlapping Node and Edge Labeling
Questions & AnswersSummary
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.
The tutorial source code demo HierarchicLayoutModule.java shows how to configure both generic approach as well as integrated edge labeling for class HierarchicLayouter.