I’ve got a TreeLayout diagram set to initialAutoScale: go.Diagram.Uniform, but the diagram loads larger than the user’s browser window. If the browser is very wide, the diagram loads within the viewport but is located at the top right, instead of the specified TopCenter.
This diagram does make use of nodes with collapsed trees, which is the only thing I can identify that might be causing the issue. Any ideas?
myDiagram =
$(go.Diagram, "myDiagram", // the ID of the DIV HTML element
{
initialContentAlignment: go.Spot.TopCenter, // center Diagram contents
initialAutoScale: go.Diagram.Uniform, // zoom to fit
"undoManager.isEnabled": true, // enable Ctrl-Z to undo and Ctrl-Y to redo
layout: $(go.TreeLayout, //TreeLayout.AlignmentCenterChildren. specify a Diagram.layout that arranges trees
{
angle: 90,
nodeSpacing: 40,
layerSpacing: 125
}),
"commandHandler.archetypeGroupData": { isGroup: true, category: "OfNodes" },
"undoManager.isEnabled": true,
});
After a bit more digging, I think the issue has to do with this code below, which I’m not running until after initial layout has completed, and which expands the tree by 2 levels:
// Upon initial load, expand trees for first two levels
myDiagram.addDiagramListener("InitialLayoutCompleted", function(e) {
e.diagram.findTreeRoots().each(function(r) {
r.expandTree(2);
});
});
How can I call this / set up the expansion level before initialAutoScale so the diagram scales to fit the window properly?
load();
// Upon initial load, expand trees for first two levels
myDiagram.addDiagramListener("InitialLayoutCompleted", function(e) {
e.diagram.findTreeRoots().each(function(r) {
r.expandTree(2);
});
});
You don’t want to expand the tree after the layout is finished, you want to do it before that, immediately after loading your model. So instead, replace the above with this simplification:
load();
// Upon initial load, expand trees for first two levels
myDiagram.findTreeRoots().each(function(r) {
r.expandTree(2);
});
That should fix it.
(Why it moves to the right after expansion is another issue, and there must be something else in the code doing that)