create-gojs-kit
Version:
A CLI for downloading GoJS samples, extensions, and docs
313 lines (275 loc) • 13.3 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="Links can connect with Links by using label Nodes on Links." />
<meta itemprop="description" content="Links can connect with Links by using label Nodes on Links." />
<meta property="og:description" content="Links can connect with Links by using label Nodes on Links." />
<meta name="twitter:description" content="Links can connect with Links by using label Nodes on Links." />
<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="Connecting Links to Links Using Link Label Nodes" />
<meta property="og:title" content="Connecting Links to Links Using Link Label Nodes" />
<meta name="twitter:title" content="Connecting Links to Links Using Link Label Nodes" />
<meta property="og:image" content="https://gojs.net/latest/assets/images/screenshots/linkstolinks.png" />
<meta itemprop="image" content="https://gojs.net/latest/assets/images/screenshots/linkstolinks.png" />
<meta name="twitter:image" content="https://gojs.net/latest/assets/images/screenshots/linkstolinks.png" />
<meta property="og:url" content="https://gojs.net/latest/samples/linksToLinks.html" />
<meta property="twitter:url" content="https://gojs.net/latest/samples/linksToLinks.html" />
<meta name="twitter:card" content="summary_large_image" />
<meta property="og:type" content="website" />
<meta property="twitter:domain" content="gojs.net" />
<title>
Connecting Links to Links Using Link Label Nodes | 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">
function init() {
myDiagram = new go.Diagram('myDiagramDiv', {
LinkDrawn: maybeChangeLinkCategory, // these two DiagramEvents call a
LinkRelinked: maybeChangeLinkCategory, // function that is defined below
'undoManager.isEnabled': true
});
// when the document is modified, add a "*" to the title and enable the "Save" button
myDiagram.addDiagramListener('Modified', e => {
const button = document.getElementById('SaveButton');
if (button) button.disabled = !myDiagram.isModified;
const idx = document.title.indexOf('*');
if (myDiagram.isModified) {
if (idx < 0) document.title += '*';
} else {
if (idx >= 0) document.title = document.title.slice(0, idx);
}
});
// the regular node template, which supports user-drawn links from the main Shape
myDiagram.nodeTemplate =
new go.Node('Auto', {
locationSpot: go.Spot.Center,
layerName: 'Background'
}) // always have regular nodes behind Links
.bindTwoWay('location', 'loc', go.Point.parse, go.Point.stringify)
.add(
new go.Shape('RoundedRectangle', {
fill: 'white',
stroke: null,
portId: '',
fromLinkable: true,
toLinkable: true,
cursor: 'pointer'
})
.bind('fill', 'color'),
new go.TextBlock({ margin: 8 }) // make some extra space for the shape around the text
.bind('text', 'key') // the label shows the node data's key
);
// This is the template for a label node on a link: just an Ellipse.
// This node supports user-drawn links to and from the label node.
myDiagram.nodeTemplateMap.add('LinkLabel',
new go.Node({
selectable: false,
movable: false,
avoidable: false,
layerName: 'Foreground'
}) // always have link label nodes in front of Links
.add(
new go.Shape('Ellipse', {
width: 5,
height: 5,
stroke: null,
portId: '',
fromLinkable: true,
toLinkable: true,
cursor: 'pointer'
})
)
);
// the regular link template, a straight blue arrow
myDiagram.linkTemplate =
new go.Link({
relinkableFrom: true, relinkableTo: true, toShortLength: 2
})
.add(
new go.Shape({ stroke: '#2E68CC', strokeWidth: 2 }),
new go.Shape({ fill: '#2E68CC', stroke: null, toArrow: 'Standard' })
);
// This template shows links connecting with label nodes as green and arrow-less.
myDiagram.linkTemplateMap.add('linkToLink',
new go.Link({ relinkableFrom: true, relinkableTo: true })
.add(
new go.Shape({ stroke: '#2D9945', strokeWidth: 2 })
));
// GraphLinksModel support for link label nodes requires specifying two properties.
myDiagram.model = new go.GraphLinksModel({ linkLabelKeysProperty: 'labelKeys' });
// Whenever a new Link is drawn by the LinkingTool, it also adds a node data object
// that acts as the label node for the link, to allow links to be drawn to/from the link.
myDiagram.toolManager.linkingTool.archetypeLabelNodeData = { category: 'LinkLabel' };
// this DiagramEvent handler is called during the linking or relinking transactions
function maybeChangeLinkCategory(e) {
var link = e.subject;
var linktolink = link.fromNode.isLinkLabel || link.toNode.isLinkLabel;
e.diagram.model.setCategoryForLinkData(link.data, linktolink ? 'linkToLink' : '');
}
load();
}
// Show the diagram's model in JSON format
function save() {
document.getElementById('mySavedModel').value = myDiagram.model.toJson();
myDiagram.isModified = false;
}
function load() {
myDiagram.model = go.Model.fromJson(document.getElementById('mySavedModel').value);
}
window.addEventListener('DOMContentLoaded', init);
</script>
<div id="sample">
<div id="myDiagramDiv" style="border: solid 1px black; width: 100%; height: 400px"></div>
<p>
This demonstrates the ability for a <a>Link</a> to appear to connect with another Link. Regular links are blue. Link-connecting links are green. Try moving
a node around to see how the links adapt. Initially the "Alpha" node connects with the link between Gamma and Delta. There is also a link between the two
horizontal links.
</p>
<p>
This effect is achieved by using "label nodes" that belong to links. Such "label nodes" are real <a>Node</a>s that are referenced from their owning
<a>Link</a>. This sample customizes the "Link Label" Node template to allow the user to draw new links to/from such label nodes.
</p>
<p>
Newly drawn links automatically get a label node by the <a>LinkingTool</a> because this sample initializes the
<a>LinkingTool.archetypeLabelNodeData</a> property of the <a>ToolManager.linkingTool</a>. The category (i.e. template) of each link is determined by what
kinds of nodes the link is connected with.
</p>
<div>
<div>
<button id="SaveButton" onclick="save()">Save</button>
<button onclick="load()">Load</button>
Diagram.model saved in JSON format:<br />
</div>
<textarea id="mySavedModel" style="width: 100%; height: 400px">
{ "class": "go.GraphLinksModel",
"linkLabelKeysProperty": "labelKeys",
"nodeDataArray": [
{"key":"Alpha", "color":"lightblue", "loc":"29 14"},
{"key":"Beta", "color":"orange", "loc":"129 14"},
{"key":"Gamma", "color":"lightgreen", "loc":"29 106"},
{"key":"Delta", "color":"pink", "loc":"129 106"},
{"key":"A-B", "category":"LinkLabel" },
{"key":"G-D", "category":"LinkLabel" },
{"key":"A-G", "category":"LinkLabel" },
{"key":"A-G-D", "category":"LinkLabel" },
{"key":"A-B-G-D", "category":"LinkLabel" }
],
"linkDataArray": [
{"from":"Alpha", "to":"Beta", "labelKeys":[ "A-B" ]},
{"from":"Gamma", "to":"Delta", "labelKeys":[ "G-D" ]},
{"from":"Alpha", "to":"Gamma", "labelKeys":[ "A-G" ]},
{"from":"Alpha", "to":"G-D", "labelKeys":[ "A-G-D" ], "category":"linkToLink"},
{"from":"A-B", "to":"G-D", "labelKeys":[ "A-B-G-D" ], "category":"linkToLink"}
]}
</textarea
>
</div>
</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>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>