Hello,
I’m trying to position labels at the start/end of the link.
Following the Link Labels | GoJS Intro we got it working for almost all scenarios except curved links.
If we use segmentIndex: -2
we get:
If we use segmentIndex: -1
we get:
We use toSpot
and fromSpot
and have set the {from|to}EndSegmentLength
to get more curvy bezier curves.
I’m not sure how we can achieve a label at the end of link which isn’t longer than the link (segmentIndex: -1
) or somewhere off in the distance (segmentIndex: -2
).
Before I try to build a (more complex) solution, maybe there is an obvious solution I haven’t considered.
There is a short html example below, where I tried to pinpoint the problem we’re having.
Cheers
Niklas
Example Code:
<!DOCTYPE html>
<html lang="en">
<body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/release/go.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<div id="allSampleContent" class="p-4 w-full">
<script id="code">
function init() {
diagram = new go.Diagram('myDiagramDiv', {
});
diagram.nodeTemplate =
new go.Node("Auto")
.bind("location", "loc", go.Point.parse)
.add(
new go.Shape("RoundedRectangle", { fill: "lightgray", portId: "", toSpot: go.Spot.TopCenter, fromSpot: go.Spot.BottomCenter }),
new go.TextBlock({ margin: 5 })
.bind("text", "key")
);
diagram.linkTemplate =
new go.Link({ curve: go.Link.Bezier, fromEndSegmentLength: 200, toEndSegmentLength: 200 })
.add(
new go.Shape(), // this is the link shape (the line)
new go.Shape({ toArrow: "Standard" }), // this is an arrowhead
new go.TextBlock({
segmentIndex: -2,
segmentOrientation: go.Link.OrientUpright,
alignmentFocus: go.Spot.BottomCenter,
textAlign: "center",
}) // this is a Link label
.bind("text")
);
const nodeDataArray = [
{ key: "Alpha", loc: "0 0" },
{ key: "Beta", loc: "200 50" }
];
const linkDataArray = [
{ from: "Alpha", to: "Beta", text: "a really long long long label" }
];
diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
}
window.addEventListener('DOMContentLoaded', init);
</script>
<div id="sample">
<div id="myDiagramDiv" style="border: solid 1px black; width: 100%; height: 470px; background: whitesmoke"></div>
</div>
</div>
</body>
</html>