create-gojs-kit
Version:
A CLI for downloading GoJS samples, extensions, and docs
372 lines (328 loc) • 15.7 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="A custom Link routing that goes smoothly through a sequence of Nodes." />
<meta itemprop="description" content="A custom Link routing that goes smoothly through a sequence of Nodes." />
<meta property="og:description" content="A custom Link routing that goes smoothly through a sequence of Nodes." />
<meta name="twitter:description" content="A custom Link routing that goes smoothly through a sequence of Nodes." />
<link rel="preconnect" href="https://rsms.me/">
<link rel="stylesheet" href="../assets/css/style.css">
<!-- Copyright 1998-2025 by Northwoods Software Corporation. -->
<meta itemprop="name" content="Curved Multi-Node Path Link Routes" />
<meta property="og:title" content="Curved Multi-Node Path Link Routes" />
<meta name="twitter:title" content="Curved Multi-Node Path Link Routes" />
<meta property="og:image" content="https://gojs.net/latest/assets/images/screenshots/multinodepathlinks.png" />
<meta itemprop="image" content="https://gojs.net/latest/assets/images/screenshots/multinodepathlinks.png" />
<meta name="twitter:image" content="https://gojs.net/latest/assets/images/screenshots/multinodepathlinks.png" />
<meta property="og:url" content="https://gojs.net/latest/samples/multiNodePathLinks.html" />
<meta property="twitter:url" content="https://gojs.net/latest/samples/multiNodePathLinks.html" />
<meta name="twitter:card" content="summary_large_image" />
<meta property="og:type" content="website" />
<meta property="twitter:domain" content="gojs.net" />
<title>
Curved Multi-Node Path Link Routes | GoJS Diagramming Library
</title>
</head>
<body>
<!-- This top nav is not part of the sample code -->
<nav id="navTop" class=" w-full h-[var(--topnav-h)] z-30 bg-white border-b border-b-gray-200">
<div class="max-w-screen-xl mx-auto flex flex-wrap items-start justify-between px-4">
<a class="text-white bg-nwoods-primary font-bold !leading-[calc(var(--topnav-h)_-_1px)] my-0 px-2 text-4xl lg:text-5xl logo"
href="../">
GoJS
</a>
<div class="relative">
<button id="topnavButton" class="h-[calc(var(--topnav-h)_-_1px)] px-2 m-0 text-gray-900 bg-inherit shadow-none md:hidden hover:!bg-inherit hover:!text-nwoods-accent hover:!shadow-none" aria-label="Navigation">
<svg class="h-7 w-7 block" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
<path d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
<div id="topnavList" class="hidden md:block">
<div class="absolute right-0 z-30 flex flex-col items-end rounded border border-gray-200 p-4 pl-12 shadow bg-white text-gray-900 font-semibold
md:flex-row md:space-x-4 md:items-start md:border-0 md:p-0 md:shadow-none md:bg-inherit">
<a href="../learn/">Learn</a>
<a href="../samples/">Samples</a>
<a href="../intro/">Intro</a>
<a href="../api/">API</a>
<a href="../download.html">Download</a>
<a href="https://forum.nwoods.com/c/gojs/11" target="_blank" rel="noopener">Forum</a>
<a id="tc" href="https://nwoods.com/contact.html"
target="_blank" rel="noopener" onclick="getOutboundLink('https://nwoods.com/contact.html', 'contact');">Contact</a>
<a id="tb" href="https://nwoods.com/sales/index.html"
target="_blank" rel="noopener" onclick="getOutboundLink('https://nwoods.com/sales/index.html', 'buy');">Buy</a>
</div>
</div>
</div>
</div>
</nav>
<script>
window.addEventListener("DOMContentLoaded", function () {
// topnav
var topButton = document.getElementById("topnavButton");
var topnavList = document.getElementById("topnavList");
if (topButton && topnavList) {
topButton.addEventListener("click", function (e) {
topnavList
.classList
.toggle("hidden");
e.stopPropagation();
});
document.addEventListener("click", function (e) {
// if the clicked element isn't the list, close the list
if (!topnavList.classList.contains("hidden") && !e.target.closest("#topnavList")) {
topButton.click();
}
});
// set active <a> element
var url = window
.location
.href
.toLowerCase();
var aTags = topnavList.getElementsByTagName('a');
for (var i = 0; i < aTags.length; i++) {
var lowerhref = aTags[i]
.href
.toLowerCase();
if (lowerhref.endsWith('.html'))
lowerhref = lowerhref.slice(0, -5);
if (url.startsWith(lowerhref)) {
aTags[i]
.classList
.add('active');
break;
}
}
}
});
</script>
<div class="flex flex-col prose">
<div class="w-full max-w-screen-xl mx-auto">
<!-- * * * * * * * * * * * * * -->
<!-- Start of GoJS sample code -->
<script src="https://cdn.jsdelivr.net/npm/gojs@3.1.0"></script>
<div id="allSampleContent" class="p-4 w-full">
<script id="code">
class MultiNodePathLink extends go.Link {
constructor(init) {
super();
if (init) Object.assign(this, init);
}
// ignores this.routing, this.adjusting, this.corner, this.smoothness, this.curviness
computePoints() {
// get the list of Nodes that should be along the path
const nodes = [];
if (this.fromNode !== null && this.fromNode.location.isReal()) {
nodes.push(this.fromNode);
}
const midkeys = this.data.path;
if (Array.isArray(midkeys)) {
const diagram = this.diagram;
for (let i = 0; i < midkeys.length; i++) {
const node = diagram.findNodeForKey(midkeys[i]);
if (node instanceof go.Node && node.location.isReal()) {
nodes.push(node);
// Optimization?: remember on each path Node all of
// the MultiNodePathLinks that go through it;
// but this optimization requires maintaining this cache
// in a Diagram Changed event listener.
let set = node._PathLinks;
if (!set) set = node._PathLinks = new go.Set(/*go.Link*/);
set.add(this);
}
}
}
if (this.toNode !== null && this.toNode.location.isReal()) {
nodes.push(this.toNode);
}
// now do the routing
this.clearPoints();
let prevloc = null;
let thisloc = null;
let nextloc = null;
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
thisloc = node.location;
nextloc = i < nodes.length - 1 ? nodes[i + 1].location : null;
let prevpt = null;
let nextpt = null;
if (this.curve === go.Curve.Bezier) {
if (prevloc !== null && nextloc !== null) {
const prevang = thisloc.directionPoint(prevloc);
const nextang = thisloc.directionPoint(nextloc);
let avg = (prevang + nextang) / 2;
let clockwise = prevang > nextang;
if (Math.abs(prevang - nextang) > 180) {
avg += 180;
clockwise = !clockwise;
}
if (avg >= 360) avg -= 360;
prevpt = new go.Point(Math.sqrt(thisloc.distanceSquaredPoint(prevloc)) / 4, 0);
prevpt.rotate(avg + (clockwise ? 90 : -90));
prevpt.add(thisloc);
nextpt = new go.Point(Math.sqrt(thisloc.distanceSquaredPoint(nextloc)) / 4, 0);
nextpt.rotate(avg - (clockwise ? 90 : -90));
nextpt.add(thisloc);
} else if (nextloc !== null) {
prevpt = null;
nextpt = thisloc; // fix this point after the loop
} else if (prevloc !== null) {
prevpt = thisloc; // fix this point after the loop
nextpt = null;
}
}
if (prevpt !== null) this.addPoint(prevpt);
this.addPoint(thisloc);
if (nextpt !== null) this.addPoint(nextpt);
prevloc = thisloc;
}
// fix up the end points when it's Bezier
if (this.curve === go.Curve.Bezier) {
// fix up the first point and the first control point
const start = this.getLinkPointFromPoint(this.fromNode, this.fromPort, this.fromPort.getDocumentPoint(go.Spot.Center), this.getPoint(3), true);
const ctrl2 = this.getPoint(2);
this.setPoint(0, start);
this.setPoint(1, new go.Point((start.x * 3 + ctrl2.x) / 4, (start.y * 3 + ctrl2.y) / 4));
// fix up the last point and the last control point
const end = this.getLinkPointFromPoint(
this.toNode,
this.toPort,
this.toPort.getDocumentPoint(go.Spot.Center),
this.getPoint(this.pointsCount - 4),
false
);
const ctrl1 = this.getPoint(this.pointsCount - 3);
this.setPoint(this.pointsCount - 2, new go.Point((end.x * 3 + ctrl1.x) / 4, (end.y * 3 + ctrl1.y) / 4));
this.setPoint(this.pointsCount - 1, end);
}
return true;
}
}
// end MultiNodePathLink class
function init() {
myDiagram = new go.Diagram('myDiagramDiv', {
allowCopy: false, // would need to copy linkdata.path and update all of the refenced node keys
allowDelete: false, // would need to update linkdata.path for all links going through that node
Changed: invalidateLinkRoutes,
'undoManager.isEnabled': true
});
myDiagram.nodeTemplate =
new go.Node("Auto", { locationSpot: go.Spot.Center })
.bind('location', 'loc', go.Point.parse)
.add(
new go.Shape({ figure: 'Circle', fill: 'white' })
.bind('fill', 'color'),
new go.TextBlock({ font: 'bold 11pt sans-serif' })
.bind('text')
);
myDiagram.linkTemplate =
new MultiNodePathLink({ // subclass of Link, defined above
curve: go.Curve.Bezier,
layerName: 'Background',
toShortLength: 4
})
.add(
new go.Shape({ strokeWidth: 4 })
.bind('stroke', 'color'),
new go.Shape({
toArrow: 'Standard',
scale: 3,
strokeWidth: 0
})
.bind('fill', 'color')
);
function invalidateLinkRoutes(e) {
// when a Node is moved, invalidate the route for all MultiNodePathLinks that go through it
if (e.change === go.ChangeType.Property && e.propertyName === 'location' && e.object instanceof go.Node) {
const diagram = e.diagram;
const node = e.object;
if (node._PathLinks) {
node._PathLinks.each(l => l.invalidateRoute());
}
} else if (e.change === go.ChangeType.Remove && e.object instanceof go.Layer) {
// when a Node is deleted that has MultiNodePathLinks going through it, invalidate those link routes
if (e.oldValue instanceof go.Node) {
const node = e.oldValue;
if (node._PathLinks) {
node._PathLinks.each(l => l.invalidateRoute());
}
} else if (e.oldValue instanceof MultiNodePathLink) {
// when deleting a MultiNodePathLink, remove all references to it in Node._PathLinks
const link = e.oldValue;
const diagram = e.diagram;
const midkeys = link.data.path;
if (Array.isArray(midkeys)) {
for (let i = 0; i < midkeys.length; i++) {
const node = diagram.findNodeForKey(midkeys[i]);
if (node !== null && node._PathLinks) node._PathLinks.remove(link);
}
}
}
}
}
// create a few nodes and links
myDiagram.model = new go.GraphLinksModel(
[
{ key: 1, text: 'Alpha', color: 'lightyellow', loc: '0 0' },
{ key: 2, text: 'Beta', color: 'brown', loc: '200 0' },
{ key: 3, text: 'Gamma', color: 'green', loc: '300 100' },
{ key: 4, text: 'Delta', color: 'slateblue', loc: '100 200' },
{ key: 5, text: 'Epsilon', color: 'aquamarine', loc: '300 350' },
{ key: 6, text: 'Zeta', color: 'tomato', loc: '0 100' },
{ key: 7, text: 'Eta', color: 'goldenrod', loc: '0 300' },
{ key: 8, text: 'Theta', color: 'orange', loc: '300 200' }
],
[
{ from: 1, to: 5, path: [2, 3, 4], color: 'blue' },
{ from: 6, to: 5, path: [7, 4, 8], color: 'red' }
]
);
}
window.addEventListener('DOMContentLoaded', init);
</script>
<div id="sample">
<div id="myDiagramDiv" style="border: solid 1px black; width: 100%; height: 700px; min-width: 200px"></div>
<p>
This sample demonstrates customization of the <a>Link</a>'s routing to go through multiple Nodes. The nodes are specified by key in the link data's "path"
property, which must be an Array of node keys.
</p>
<p>As the user drags around Nodes on the "path", the routing is automatically recomputed to maintain a smooth curve.</p>
</div>
</div>
<!-- * * * * * * * * * * * * * -->
<!-- End of GoJS sample code -->
</div>
<div id="allTagDescriptions" class="p-4 w-full max-w-screen-xl mx-auto">
<hr/>
<h3 class="text-xl">GoJS Features in this sample</h3>
<!-- blacklist tags that do not correspond to a specific GoJS feature -->
<h4>Collections</h4>
<p>
<b>GoJS</b> provides its own collection classes: <a href="../api/symbols/List.html" target="api">List</a>, <a href="../api/symbols/Set.html" target="api">Set</a>, and <a href="../api/symbols/Map.html" target="api">Map</a>.
You can iterate over a collection by using an <a href="../api/symbols/Iterator.html" target="api">Iterator</a>.
More information can be found in the <a href="../intro/collections.html">GoJS Intro</a>.
</p>
<p>
<a href="../samples/index.html#collections">Related samples</a>
</p>
<hr>
<!-- blacklist tags that do not correspond to a specific GoJS feature -->
<h4>Links</h4>
<p>
The <a href="../api/symbols/Link.html" target="api">Link</a> class is used to implement a visual relationship between nodes.
Links are normally created by the presence of link data objects in the <a href="../api/symbols/GraphLinksModel.html#linkDataArray" target="api">GraphLinksModel.linkDataArray</a>
or by a parent key reference as the value of the <a href="../api/symbols/TreeModel.html#nodeParentKeyProperty" target="api">TreeModel.nodeParentKeyProperty</a> of a node data object
in a <a href="../api/symbols/TreeModel.html" target="api">TreeModel</a>.
More information can be found in the <a href="../intro/links.html">GoJS Intro</a>.
</p>
<p>
<a href="../samples/index.html#links">Related samples</a>
</p>
<hr>
</div>
</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>