<!DOCTYPEhtml><html><headlang="en"><metacharset="UTF-8"><title></title><styletype="text/css">canvas{border: 0px;outline: none;}</style><scriptsrc="js/go-2.0.11.js"></script><scriptsrc="js/jquery-1.12.4.min.js"></script><scripttype="text/javascript">$(function(){gojsInit();});functiongojsInit(){var $ = go.GraphObject.make;// for conciseness in defining templates
diagram =$(go.Diagram,"myDiagram",// must name or refer to the DIV HTML element{initialContentAlignment: go.Spot.Center,//初始位置initialScale:1.5,//初始比例});
diagram.nodeTemplate =$(go.Node,{dragComputation: stayInGroup
},newgo.Binding("location","loc", go.Point.parse).makeTwoWay(go.Point.stringify),$(go.TextBlock,newgo.Binding("text","key")));
diagram.groupTemplate =$(go.Group,"Vertical",{selectionObjectName:"PH",locationObjectName:"PH",resizeObjectName:"PH",resizable:true},newgo.Binding("location","loc", go.Point.parse).makeTwoWay(go.Point.stringify),$(go.TextBlock,// group title{font:"Bold 12pt Sans-Serif"},newgo.Binding("text","key")),$(go.Shape,// using a Shape instead of a Placeholder{name:"PH",fill:"lightyellow"},newgo.Binding("desiredSize","size", go.Size.parse)));var nodeDataArray =[{key:"Alpha",loc:"0 0"},{key:"Beta",group:"Omega",loc:"75 75"},{key:"Gamma",group:"Omega",loc:"125 75"},{key:"Omega",isGroup:true,loc:"50 50",size:"150 50"},{key:"Delta",loc:"200 0"}];var linkDataArray =[{from:"Alpha",to:"Beta"},// from outside the Group to inside it{from:"Beta",to:"Gamma"},// this link is a member of the Group{from:"Omega",to:"Delta"}// from the Group to a Node];
diagram.model =newgo.GraphLinksModel(nodeDataArray, linkDataArray);functionstayInGroup(part, pt, gridpt){// 获取节点所在组const grp = part.containingGroup;if(grp ===null)return pt;// 获取组节点访问const back = grp.resizeObject;if(back ===null)return pt;// 如果按下Shift键,则允许将节点拖出组if(part.diagram.lastInput.shift)return pt;const r = back.getDocumentBounds();const b = part.actualBounds;const loc = part.location;// 限制节点移动范围const x = Math.max(r.x, Math.min(pt.x, r.right - b.width -1))+(loc.x - b.x);const y = Math.max(r.y, Math.min(pt.y, r.bottom - b.height -1))+(loc.y - b.y);returnnewgo.Point(x, y);}}functionsave(){
document.getElementById("mySavedModel").value = diagram.model.toJson();
diagram.isModified =false;}functionload(){
diagram.model = go.Model.fromJson(document.getElementById("mySavedModel").value);}</script></head><body><divid="sample"><divstyle="width:100%;white-space:nowrap;"><spanstyle="display: inline-block;vertical-align: top;padding: 5px;width:95%"><divid="myDiagram"style="border: solid 1px gray;height: 400px"></div><buttonid="saveModel"onclick="save()">Save</button><buttonid="loadModel"onclick="load()">Load</button></span></div><textareaid="mySavedModel"style="width:100%;height:300px"></textarea></div></body></html>