As you make nodes or links visible or not-visible, a layout will automatically be performed at the end of the transaction.
Does this code help you understand what is going on?
<script src="go.js"></script>
<script src="../extensions/RadialLayout.js"></script>
<script id="code">
function init() {
var $ = go.GraphObject.make; // for conciseness in defining templates
myDiagram =
$(go.Diagram, "myDiagramDiv", // must be the ID or reference to div
{
initialAutoScale: go.Diagram.Uniform,
padding: 10,
isReadOnly: true,
layout: $(RadialLayout, {
//maxLayers: 2,
rotateNode: function(node, angle, sweep, radius) {
// rotate the nodes and make sure the text is not upside-down
node.angle = angle;
var label = node.findObject("TEXTBLOCK");
if (label !== null) {
label.angle = ((angle > 90 && angle < 270 || angle < -90) ? 180 : 0);
}
}
})
});
myDiagram.nodeTemplate =
$(go.Node, "Spot",
{
locationSpot: go.Spot.Center,
locationObjectName: "SHAPE", // Node.location is the center of the Shape
selectionAdorned: false,
click: nodeClicked
},
$(go.Shape, "Circle",
{
name: "SHAPE",
fill: "lightgray", // default value, but also data-bound
stroke: "transparent",
strokeWidth: 2,
desiredSize: new go.Size(20, 20),
portId: "" // so links will go to the shape, not the whole node
},
new go.Binding("fill", "color")),
$(go.TextBlock,
{
name: "TEXTBLOCK",
alignment: go.Spot.Right,
alignmentFocus: go.Spot.Left
},
new go.Binding("text"))
);
myDiagram.linkTemplate =
$(go.Link,
{ curve: go.Link.Bezier },
$(go.Shape),
$(go.Shape, { toArrow: "Standard" })
);
// this is the root node, at the center of the circular layers
myDiagram.nodeTemplateMap.add("Root",
$(go.Node, "Auto",
{
locationSpot: go.Spot.Center,
selectionAdorned: false
},
$(go.Shape, "Circle",
{ fill: "white" }),
$(go.TextBlock,
{ font: "bold 12pt sans-serif", margin: 5 },
new go.Binding("text"))
));
generateGraph();
}
function generateGraph() {
var names = [
"Joshua", "Daniel", "Robert", "Noah", "Anthony",
"Elizabeth", "Addison", "Alexis", "Ella", "Samantha",
"Joseph", "Scott", "James", "Ryan", "Benjamin",
"Walter", "Gabriel", "Christian", "Nathan", "Simon",
"Isabella", "Emma", "Olivia", "Sophia", "Ava",
"Emily", "Madison", "Tina", "Elena", "Mia",
"Jacob", "Ethan", "Michael", "Alexander", "William",
"Natalie", "Grace", "Lily", "Alyssa", "Ashley",
"Sarah", "Taylor", "Hannah", "Brianna", "Hailey",
"Christopher", "Aiden", "Matthew", "David", "Andrew",
"Kaylee", "Juliana", "Leah", "Anna", "Allison",
"John", "Samuel", "Tyler", "Dylan", "Jonathan"
];
var nodeDataArray = [];
for (var i = 0; i < names.length; i++) {
nodeDataArray.push({ key: i, text: names[i], color: go.Brush.randomColor(128, 240) });
}
var linkDataArray = [];
var num = nodeDataArray.length;
for (var i = 1; i < num; i++) {
var a = Math.floor(Math.random() * i);
linkDataArray.push({ from: a, to: i, color: go.Brush.randomColor(0, 127) });
}
myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
myDiagram.layout.root = myDiagram.findNodeForData(nodeDataArray[0]);
myDiagram.layout.root.category = "Root";
}
function nodeClicked(e, node) {
if (node.isTreeExpanded) {
e.diagram.commandHandler.collapseTree(node);
} else {
e.diagram.commandHandler.expandTree(node);
}
}
window.addEventListener('DOMContentLoaded', init);
</script>
<div id="sample">
<div id="myDiagramDiv" style="border: solid 1px black; background: white; width: 100%; height: 600px"></div>
</div>