UNPKG

gojs

Version:

Interactive diagrams, charts, and graphs, such as trees, flowcharts, orgcharts, UML, BPMN, or business diagrams

219 lines (203 loc) 9.16 kB
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Friend Wheel</title> <meta name="description" content="Show the relationships between people using a friend wheel diagram, implemented using circular layout." /> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Copyright 1998-2020 by Northwoods Software Corporation. --> <script src="../release/go.js"></script> <script src="../assets/js/goSamples.js"></script> <!-- this is only for the GoJS Samples framework --> <script id="code"> function WheelLayout() { go.CircularLayout.call(this); } go.Diagram.inherit(WheelLayout, go.CircularLayout); // override makeNetwork to set the diameter of each node and ignore the TextBlock label WheelLayout.prototype.makeNetwork = function(coll) { var net = go.CircularLayout.prototype.makeNetwork.call(this, coll); net.vertexes.each(function(cv) { cv.diameter = 20; // because our desiredSize for nodes is (20, 20) }); return net; } // override commitNodes to rotate nodes so the text goes away from the center, // and flip text if it would be upside-down WheelLayout.prototype.commitNodes = function() { go.CircularLayout.prototype.commitNodes.call(this); this.network.vertexes.each(function(v) { var node = v.node; if (node === null) return; // get the angle of the node towards the center, and rotate it accordingly var a = v.actualAngle; if (a > 90 && a < 270) { // make sure the text isn't upside down var textBlock = node.findObject("TEXTBLOCK"); textBlock.angle = 180; } node.angle = a; }); }; // override commitLinks in order to make sure all of the Bezier links are "inside" the ellipse; // this helps avoid links crossing over any other nodes WheelLayout.prototype.commitLinks = function() { go.CircularLayout.prototype.commitLinks.call(this); if (this.network.vertexes.count > 4) { this.network.vertexes.each(function(v) { v.destinationEdges.each(function(de) { var dv = de.toVertex; var da = dv.actualAngle; var sa = v.actualAngle; if (da - sa > 180) da -= 360; else if (sa - da > 180) sa -= 360; de.link.curviness = (sa > da) ? 15 : -15; }) }) } } // end WheelLayout class var highlightColor = "red"; // color parameterization function init() { if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this 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, contentAlignment: go.Spot.Center, layout: $(WheelLayout, // set up a custom CircularLayout // set some properties appropriate for this sample { arrangement: go.CircularLayout.ConstantDistance, nodeDiameterFormula: go.CircularLayout.Circular, spacing: 10, aspectRatio: 0.7, sorting: go.CircularLayout.Optimized }), isReadOnly: true, click: function(e) { // background click clears any remaining highlighteds e.diagram.startTransaction("clear"); e.diagram.clearHighlighteds(); e.diagram.commitTransaction("clear"); } }); // define the Node template myDiagram.nodeTemplate = $(go.Node, "Horizontal", { selectionAdorned: false, locationSpot: go.Spot.Center, // Node.location is the center of the Shape locationObjectName: "SHAPE", mouseEnter: function(e, node) { node.diagram.clearHighlighteds(); node.linksConnected.each(function(l) { highlightLink(l, true); }); node.isHighlighted = true; var tb = node.findObject("TEXTBLOCK"); if (tb !== null) tb.stroke = highlightColor; }, mouseLeave: function(e, node) { node.diagram.clearHighlighteds(); var tb = node.findObject("TEXTBLOCK"); if (tb !== null) tb.stroke = "black"; } }, new go.Binding("text", "text"), // for sorting the nodes $(go.Shape, "Ellipse", { name: "SHAPE", fill: "lightgray", // default value, but also data-bound stroke: "transparent", // modified by highlighting 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"), new go.Binding("stroke", "isHighlighted", function(h) { return h ? highlightColor : "transparent"; }) .ofObject()), $(go.TextBlock, { name: "TEXTBLOCK" }, // for search new go.Binding("text", "text")) ); function highlightLink(link, show) { link.isHighlighted = show; link.fromNode.isHighlighted = show; link.toNode.isHighlighted = show; } // define the Link template myDiagram.linkTemplate = $(go.Link, { routing: go.Link.Normal, curve: go.Link.Bezier, selectionAdorned: false, mouseEnter: function(e, link) { highlightLink(link, true); }, mouseLeave: function(e, link) { highlightLink(link, false); } }, $(go.Shape, new go.Binding("stroke", "isHighlighted", function(h, shape) { return h ? highlightColor : shape.part.data.color; }) .ofObject(), new go.Binding("strokeWidth", "isHighlighted", function(h) { return h ? 2 : 1; }) .ofObject()) // no arrowhead -- assume directionality of relationship need not be shown ); 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 = 0; i < num * 2; i++) { var a = Math.floor(Math.random() * num); var b = Math.floor(Math.random() * num / 4) + 1; linkDataArray.push({ from: a, to: (a + b) % num, color: go.Brush.randomColor(0, 127) }); } myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray); } </script> </head> <body onload="init()"> <div id="sample"> <div id="myDiagramDiv" style="border: solid 1px black; background: white; width: 100%; height: 600px" ></div> <p> This "friend wheel" demonstrates the use of <a>CircularLayout</a>. The layout has been customized to make sure each node is considered to have a fixed diameter, ignoring the size of any <a>TextBlock</a>. </p> <p> The custom layout also rotates each <a>Node</a> according to the actual angle at which the node was positioned. This information is available on the <a>CircularVertex</a> used by the <a>LayoutNetwork</a> that the <a>CircularLayout</a> constructs from the nodes and links of the diagram. Furthermore, when laying out the nodes it also flips the angle of the <a>TextBlock</a> so that the text is not upside-down. </p> <p> <a>GraphObject.mouseEnter</a> and <a>GraphObject.mouseLeave</a> event handlers on the <a>Node</a> template highlight both the Node and all of the Links that connect with the Node. The same event handlers on the <a>Link</a>s highlight that Link and both connected Nodes. Changes made in these event handlers automatically are not recorded in the <a>UndoManager</a>, although this sample does not enable the UndoManager anyway. </p> </div> </body> </html>