gojs
Version:
Interactive diagrams, charts, and graphs, such as trees, flowcharts, orgcharts, UML, BPMN, or business diagrams
257 lines (235 loc) • 12.6 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover"/>
<meta name="description" content="Arrange people in rings around a central person, in layers according to distance from the central person."/>
<link rel="stylesheet" href="../assets/css/style.css"/>
<!-- Copyright 1998-2023 by Northwoods Software Corporation. -->
<title>Radial Partition Layout</title>
</head>
<body>
<!-- This top nav is not part of the sample code -->
<nav id="navTop" class="w-full z-30 top-0 text-white bg-nwoods-primary">
<div class="w-full container max-w-screen-lg mx-auto flex flex-wrap sm:flex-nowrap items-center justify-between mt-0 py-2">
<div class="md:pl-4">
<a class="text-white hover:text-white no-underline hover:no-underline
font-bold text-2xl lg:text-4xl rounded-lg hover:bg-nwoods-secondary " href="../">
<h1 class="my-0 p-1 ">GoJS</h1>
</a>
</div>
<button id="topnavButton" class="rounded-lg sm:hidden focus:outline-none focus:ring" aria-label="Navigation">
<svg fill="currentColor" viewBox="0 0 20 20" class="w-6 h-6">
<path id="topnavOpen" fill-rule="evenodd" d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM9 15a1 1 0 011-1h6a1 1 0 110 2h-6a1 1 0 01-1-1z" clip-rule="evenodd"></path>
<path id="topnavClosed" class="hidden" fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path>
</svg>
</button>
<div id="topnavList" class="hidden sm:block items-center w-auto mt-0 text-white p-0 z-20">
<ul class="list-reset list-none font-semibold flex justify-end flex-wrap sm:flex-nowrap items-center px-0 pb-0">
<li class="p-1 sm:p-0"><a class="topnav-link" href="../learn/">Learn</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="../samples/">Samples</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="../intro/">Intro</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="../api/">API</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/products/register.html">Register</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="../download.html">Download</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="https://forum.nwoods.com/c/gojs/11">Forum</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/contact.html"
target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/contact.html', 'contact');">Contact</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/sales/index.html"
target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/sales/index.html', 'buy');">Buy</a></li>
</ul>
</div>
</div>
<hr class="border-b border-gray-600 opacity-50 my-0 py-0" />
</nav>
<div class="md:flex flex-col md:flex-row md:min-h-screen w-full max-w-screen-xl mx-auto">
<div id="navSide" class="flex flex-col w-full md:w-48 text-gray-700 bg-white flex-shrink-0"></div>
<!-- * * * * * * * * * * * * * -->
<!-- Start of GoJS sample code -->
<script src="../release/go.js"></script>
<div id="allSampleContent" class="p-4 w-full">
<script src="../extensions/RadialLayout.js"></script>
<script id="code">
var layerThickness = 70; // how thick each ring should be
function init() {
// Since 2.2 you can also author concise templates with method chaining instead of GraphObject.make
// For details, see https://gojs.net/latest/intro/buildingObjects.html
const $ = go.GraphObject.make; // for conciseness in defining templates
myDiagram =
new go.Diagram("myDiagramDiv", // must be the ID or reference to div
{
initialAutoScale: go.Diagram.Uniform,
isReadOnly: true,
maxSelectionCount: 1,
layout: $(RadialLayout, {
maxLayers: 5,
layerThickness: layerThickness,
rotateNode: function(node, angle, sweep, radius) { // method override must be function, not =>
// all nodes are centered at the origin
node.location = this.arrangementOrigin;
// because the Shape.geometry will be centered at the origin --
// see makeAnnularWedge, below
node.diagram.model.setDataProperty(node.data, "angle", angle);
node.diagram.model.setDataProperty(node.data, "sweep", sweep);
node.diagram.model.setDataProperty(node.data, "radius", radius);
}
}),
"animationManager.isEnabled": false
});
var commonToolTip =
$("ToolTip",
$(go.Panel, "Vertical",
{ margin: 3 },
$(go.TextBlock, // bound to node data
{ margin: 4, font: "bold 12pt sans-serif" },
new go.Binding("text")),
$(go.TextBlock, // bound to node data
new go.Binding("text", "color", c => "Color: " + c)),
$(go.TextBlock, // bound to Adornment because of call to Binding.ofObject
new go.Binding("text", "", ad => "Connections: " + ad.adornedPart.linksConnected.count).ofObject())
) // end Vertical Panel
); // end Adornment
// define the Node template
myDiagram.nodeTemplate =
$(go.Node, "Spot",
{
locationSpot: go.Spot.Center,
selectionAdorned: false,
click: nodeClicked,
toolTip: commonToolTip
},
$(go.Shape, // this always occupies the full circle
{ fill: "lightgray", strokeWidth: 0 },
new go.Binding("geometry", "", makeAnnularWedge),
new go.Binding("fill", "color")),
$(go.TextBlock,
{ width: layerThickness, textAlign: "center" },
new go.Binding("alignment", "", computeTextAlignment),
new go.Binding("angle", "angle", ensureUpright),
new go.Binding("text"))
);
function makeAnnularWedge(data) {
var angle = data.angle;
var sweep = data.sweep;
var radius = data.radius; // the inner radius
if (angle === undefined || sweep === undefined || radius === undefined) return null;
// the Geometry will be centered about (0,0)
var outer = radius + layerThickness; // the outer radius
var inner = radius;
var p = new go.Point(outer, 0).rotate(angle - sweep / 2);
var q = new go.Point(inner, 0).rotate(angle + sweep / 2);
var geo = new go.Geometry()
.add(new go.PathFigure(-outer, -outer)) // always make sure the Geometry includes the top left corner
.add(new go.PathFigure(outer, outer)) // and the bottom right corner of the whole circular area
.add(new go.PathFigure(p.x, p.y) // start at outer corner, go clockwise
.add(new go.PathSegment(go.PathSegment.Arc, angle - sweep / 2, sweep, 0, 0, outer, outer))
.add(new go.PathSegment(go.PathSegment.Line, q.x, q.y)) // to opposite inner corner, then anticlockwise
.add(new go.PathSegment(go.PathSegment.Arc, angle + sweep / 2, -sweep, 0, 0, inner, inner).close()));
return geo;
}
function computeTextAlignment(data) {
var angle = data.angle;
var radius = data.radius;
if (angle === undefined || radius === undefined) return go.Spot.Center;
var p = new go.Point(radius + layerThickness / 2, 0).rotate(angle);
return new go.Spot(0.5, 0.5, p.x, p.y);
}
function ensureUpright(angle) {
if (angle > 90 && angle < 270) return angle + 180;
return angle;
}
// 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,
toolTip: commonToolTip,
width: layerThickness * 2,
height: layerThickness * 2
},
$(go.Shape, "Circle",
{ fill: "white", strokeWidth: 0.5, spot1: go.Spot.TopLeft, spot2: go.Spot.BottomRight }),
$(go.TextBlock,
{ font: "bold 14pt sans-serif", textAlign: "center" },
new go.Binding("text"))
));
// define the Link template -- don't show anything!
myDiagram.linkTemplate =
$(go.Link);
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);
// layout based on a random person
var someone = nodeDataArray[Math.floor(Math.random() * nodeDataArray.length)];
var somenode = myDiagram.findNodeForData(someone);
nodeClicked(null, somenode);
}
function nodeClicked(e, root) {
var diagram = root.diagram;
if (diagram === null) return;
// all other nodes should be visible and use the default category
diagram.nodes.each(n => {
n.visible = true;
if (n !== root) n.category = "";
});
// make this Node the root
root.category = "Root";
// the root node always gets a full circle for itself, just in case the "Root"
// template has any bindings using these properties
diagram.model.setDataProperty(root.data, "angle", 0);
diagram.model.setDataProperty(root.data, "sweep", 360);
diagram.model.setDataProperty(root.data, "radius", 0);
// tell the RadialLayout what the root node should be
// setting this property will automatically invalidate the layout and then perform it again
diagram.layout.root = root;
}
window.addEventListener('DOMContentLoaded', init);
</script>
<div id="sample">
<div id="myDiagramDiv" style="border: solid 1px black; background: white; width: 100%; height: 600px"></div>
<p>
Click on a Node to center it and show its relationships.
</p>
<p>
The <code>RadialLayout</code> class is an extension defined at <a href="../extensions/RadialLayout.js">RadialLayout.js</a>.
The override of the <code>RadialLayout.rotateNode</code> sets the <code>angle</code>,
<code>sweep</code>, and <code>radius</code> data properties.
Bindings in the node template use those properties to produce the appropriate <a>Shape.geometry</a>
and the <a>GraphObject.alignment</a> and <a>GraphObject.angle</a> for each <a>TextBlock</a>.
</p>
</div>
</div>
<!-- * * * * * * * * * * * * * -->
<!-- End of GoJS sample code -->
</div>
</body>
<!-- This script is part of the gojs.net website, and is not needed to run the sample -->
<script src="../assets/js/goSamples.js"></script>
</html>