JSFiddle - React, Tailwind, and code Playground
by Ankit Arora
HTML
<script src="http://gojs.net/latest/release/go.js"></script>
<body onload="init()">
<div id="sample">
<div id="myDiagram" style="border: solid 1px black; width: 100%; height: 400px"></div>
<button onclick="insertNode()">Insert</button>
<p>
This demonstrates the ability to simulate having nodes be members of multiple "groups".
Regular <a>Group</a>s only support a single <a>Part.containingGroup</a> for nodes.
This sample does not make use of <a>Group</a>s at all, but simulates one possible "grouping" relationship
using a custom <a>Layout</a> and a custom <a>DraggingTool</a>.
</p>
<p>
The CustomLayout assumes regular nodes already have real locations, and only assigns <a>Part.location</a> to "Super" nodes.
It also sets the <a>GraphObject.desiredSize</a> on the "BODY" element of each "Super" node, based on the area occupied by all of its member nodes.
The CustomDraggingTool overrides the <a>DraggingTool.computeEffectiveCollection</a> method to determine what nodes to drag around.
</p>
<p>
This sample does not support dynamic restructuring of the relationships in the graph.
</p>
<button id="SaveButton" onclick="save()">Save</button>
<button onclick="load()">Load</button>
Diagram Model saved in JSON format:
<br />
<textarea id="mySavedModel" style="width:100%;height:300px">
{ "nodeDataArray": [
{ "key": -1, "text": "Operating", "category": "Super" },
{ "key": -3, "text": "Non Drying", "category": "Super" },
{ "key": 1, "loc": "100 100", "text": "Starting.End", "supers": [-1] },
{ "key": 3, "loc": "100 200", "text": "Starting.Init", "supers": [-1, -3] },
{ "key": 5, "loc": "100 300", "text": "Stopped", "supers": [-3] }
],
"linkDataArray": [
{ "from": 3, "to": 1 },
{ "from": 5, "to": 3 }
]
}
</textarea>
</div>
</body>
JavaScript
function insertNode()
{
myDiagram.startTransaction("Add Node");
myDiagram.model.addNodeData({
key: myDiagram.model.nodeDataArray.length + 1,
text: "New Element",
supers: [-2]
});
myDiagram.commitTransaction("Add Node");
}
function init() {
var goMake = go.GraphObject.make; // for conciseness in defining templates
var diagramOptions = {
initialContentAlignment: go.Spot.Center,
allowCopy: false,
allowDelete: false,
draggingTool: goMake(CustomDraggingTool),
layout: goMake(CustomLayout),
"undoManager.isEnabled": true
};
myDiagram = go.GraphObject.make(go.Diagram, "myDiagram", diagramOptions);
// define the Node template
// the node template describes how each Node should be constructed
// define the Node template
myDiagram.nodeTemplate =
goMake(go.Node, "Auto", // the Shape automatically fits around the TextBlock
new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
{
//movable: false,
selectionAdorned: false,
layoutConditions: go.Part.LayoutStandard,
click: function (e, obj) {
canvasObj.onNodeClick(e, obj, canvasObj);
}
},
goMake(go.Shape, "RoundedRectangle",
{
fill: "rgb(254, 201, 0)", stroke: "black", parameter1: 20, // the corner has a large radius
portId: "", fromSpot: go.Spot.AllSides, toSpot: go.Spot.AllSides
}),
goMake(go.TextBlock,
{ margin: 3 }, // some room around the text
// bind TextBlock.text to Node.data.key
new go.Binding("text", "key"))
);
myDiagram.nodeTemplateMap.add("Super",
goMake(go.Node, "Auto",
{ locationObjectName: "BODY" },
goMake(go.Shape, "RoundedRectangle",
...