Controlling the Rendering Order of Graph Elements in the View
Questions & AnswersSummary
Description
The rendering order of elements in the view can be controlled in several ways, including:
- Changing the order of graph elements in their respective set, which propagates to the rendering.
- Using layered painting to directly change the rendering order.
- Explicitly specifying the rendering order for all elements.
Changing the order of graph elements
The most simple solution is to move those graph elements that shall be on top of others towards the end of the respective data structure that is held by the graph.
This can be achieved using methods
Graph.moveToLast(Node) for nodes and
Graph.moveToLast(Edge) for edges.
During rendering, the data structures are processed from first to last, so graph elements at the end are rendered later and thus appear on top.
For consistent results in hierarchically organized graphs, each call to Graph.moveToLast(Node) needs to be accompanied with a corresponding call to the HierarchyManager's
moveToLast(Node)
method.
Note that the general rendering order of a graph's node set and edge set, i.e., whether first all nodes and then all edges are rendered or vice versa, cannot be affected using this scheme. In other words, it is not possible to mix the rendering of nodes and edges.
Using layered painting to directly change the rendering order
Without affecting the order of graph elements in their respective set, the rendering order can also be changed directly using DefaultGraph2DRenderer's support for "layered painting." Using layered painting is discussed in this Knowledge Base article.
With layered painting it is possible to have nodes and edges in the same layer, which effectively allows to mix the rendering of nodes and edges.
Explicitly specifying the rendering order for all elements
The rendering order can also be explicitly specified for all elements by providing a Graph2DTraversal implementation that is used in conjunction with an OrderRenderer implementation. The Graph2DTraversal implementation defines iterators that specify the first-to-last and the last-to-first order of graph elements. The latter is explicitly used when hit-testing the elements in the view.
This scheme allows fine-grained control over the rendering order for all element types.