We have a fairly simple diagram using a ForceDirectedLayout to show relationships between elements in an underlying business model.
A node’s location is in a two-way bind with the matching element in the underlying model. Model elements may be added by the user through other means than the diagram. When that happens, those elements don’t have a location.
When opening a diagram, we check whether at least 1 element is missing a location. In that case, we trigger an initial layout of the diagram, which assigns a location to all nodes.
This approach works as expected for new diagrams, where all elements/nodes are without a location. A similar approach that is used when the user drops new nodes into the diagram from the palette also works well.
However, there is a use case where we reliably hit a display issue. When a model element is added from outside of a diagram, and that diagram is then opened, we get misplaced links that look like the following:
Here the added element was “[Vision] Our Vision”, which incidentally is the only node whose link is correctly positioned.
This happens whether we use the isInitial layout property or call layoutDiagram() on the diagram after fully populating its model.
The issue can be corrected in 2 ways: either setting a timeout of a few seconds before calling layoutDiagram(), or having the user manually trigger a layout of the diagram through a toolbar button we have implemented.
So the issue seems to have to do with the interplay of the initial layout with the initialization of the diagram. Is this something that others have observed as well and do you have any recommendations to address the issue?
We tried to find an event that would fire once the diagram is fully initialized and drawn, so that we could listen for it and only trigger a layout at that time. Unfortunately, the only event that comes close is “InitialLayoutCompleted”, which obviously won’t do.