<!DOCTYPE html>
<html>
<head>
<title>Minimal GoJS Sample</title>
<!-- Copyright 1998-2022 by Northwoods Software Corporation. -->
</head>
<body>
<div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:600px"></div>
<script src="https://unpkg.com/gojs"></script>
<script id="code">
function init() {
const $ = go.GraphObject.make;
myDiagram =
$(go.Diagram, "myDiagramDiv",
{
layout: $(go.TreeLayout, { angle: 90 }),
"undoManager.isEnabled": true
});
myDiagram.nodeTemplate =
$(go.Node, "Auto",
{ locationSpot: go.Spot.Center },
$(go.Shape, { fill: "white" },
new go.Binding("fill", "color")),
$(go.TextBlock, { margin: 8, editable: true },
new go.Binding("text").makeTwoWay())
);
function updateGroupLabel(g) {
const tb = g.findObject("LABEL");
if (!tb) return;
tb.text = (g.isSubGraphExpanded ? "-" : "+") + g.memberParts.filter(m => m instanceof go.Node).count;
}
myDiagram.groupTemplate =
$(go.Group, "Vertical",
{
locationSpot: go.Spot.Center,
layout: $(go.ForceDirectedLayout, { defaultSpringLength: 10, defaultElectricalCharge: 40 }),
click: (e, g) => {
if (g.isSubGraphExpanded) e.diagram.commandHandler.collapseSubGraph(g);
else e.diagram.commandHandler.expandSubGraph(g);
},
subGraphExpandedChanged: updateGroupLabel,
memberAdded: updateGroupLabel,
memberRemoved: updateGroupLabel,
},
new go.Binding("isSubGraphExpanded").makeTwoWay(), // remember expansion state
$(go.Panel, "Auto",
$(go.Shape, "RoundedRectangle",
{ fill: "#333", strokeWidth: 0, spot1: go.Spot.TopLeft, spot2: go.Spot.BottomRight }),
$(go.TextBlock, { name: "LABEL", margin: 6, stroke: "white", font: "bold 12pt sans-serif" })
),
$(go.Shape, "LineV", { width: 0, height: 10 },
new go.Binding("visible", "isSubGraphExpanded").ofObject()),
$(go.Panel, "Auto",
$(go.Shape, "RoundedRectangle",
{ fill: "white", strokeWidth: 1.5, strokeDashArray: [4, 4],
spot1: go.Spot.TopLeft, spot2: go.Spot.BottomRight },
new go.Binding("visible", "isSubGraphExpanded").ofObject()),
$(go.Placeholder, { padding: 10 })
)
)
myDiagram.model = new go.GraphLinksModel(
[
{ key: 1, text: "Alpha", color: "lightblue" },
{ key: 2, isGroup: true },
{ key: 21, text: "Beta 1", color: "orange", group: 2 },
{ key: 22, text: "Beta 2", color: "pink", group: 2 },
{ key: 3, isGroup: true, isSubGraphExpanded: false },
{ key: 31, text: "Gamma 1", color: "lightgreen", group: 3 },
{ key: 32, text: "Gamma 2", color: "lightgreen", group: 3 },
{ key: 33, text: "Gamma 3", color: "lightgreen", group: 3 },
{ key: 4, text: "Delta", color: "yellow" }
],
[
{ from: 1, to: 2 },
{ from: 2, to: 3 },
{ from: 21, to: 22 },
{ from: 3, to: 4 },
{ from: 31, to: 32 },
{ from: 32, to: 33 },
{ from: 33, to: 31 }
]);
}
window.addEventListener('DOMContentLoaded', init);
</script>
</body>
</html>