Labels on Links
It is common to add annotations or decorations on a link, particularly text.
Simple Link labels
By default if you add a GraphObject to a Link, it will be positioned at the middle of the link. In this example, we just add a TextBlock to the link and bind its TextBlock.text property to the link data's "text" property.
Note that clicking on the text label results in selection of the whole Link.
Although it is commonplace to use a TextBlock as the link label, it can be any GraphObject such as a Shape or an arbitrarily complex Panel. This also works if the link is orthogonally routed or bezier-curved. Here is an example of a simple panel label on both a straight link and an orthogonally routed link:
Although positioning the label at the middle of the link is the default behavior, you can set GraphObject properties that start with "segment" to specify exactly where and how to arrange the object along the route of the link.
Link label segmentIndex and segmentFraction
Set the GraphObject.segmentIndex property in order to specify which segment of the link route the object should be on. Set the GraphObject.segmentFraction property to control how far the object should be, as a fraction from the start of the segment (zero) to the end of the segment (one).
When setting the GraphObject.segmentIndex property to NaN, the fraction will be calculated along the entire link route instead of a particular segment.
In the case of a link that comes from a node with no GraphObject.fromSpot (i.e. Spot.None) and goes to a node with no GraphObject.toSpot, there may be only one segment in the link, segment number zero.
In the case of a link that has many segments in it, you will want to specify different segment numbers. Orthogonal links, for example, typically have 6 points in the route, which means five segments numbered from 0 to 4. Segments 0 and 4 are the stubs on the ends of the link that connect to the other node (or to its ports if it has any) and segments 1, 2, and 3 are the long segments that make up most of the link.
However, you can also count segments backwards from the "to" end of the link. -1 is the last segment, -2 is the next to last, etc. When you use a negative segment index, the segment fraction goes from 0 closest to the "to" end to 1 for the end of that segment that is farthest back along the route from the "to" end. Thus a segmentIndex of -1 with a segmentFraction of 0 is the very end point of the link route. A segmentIndex of -1 with a segmentFraction of 1 is the same point as segmentIndex -2 and segmentFraction 0.
For labels that belong near the "to" end of a link, you will normally use negative values for GraphObject.segmentIndex. This convention works better when the number of segments in a link is unknown or may vary.
Lastly, one can specify a segmentIndex of NaN to have the fraction calculated along the entire link route instead of just a particular segment. This is also helpful when the number of segments of a link is unknown or varying. The following example uses this to place labels at the 1/3 and 2/3 points on the entire link. Try selecting the link and reshaping it with the control point handles:
More dynamic Link labels
This behavior can be made interactive be defining custom tools that modify the segment fraction or segment index. See the Dragging Link Label along Path sample for an example;
If you desire even more dynamic behavior from link labels, you can create "link label" nodes that are really Nodes but look like link labels because they are referenced by their associated link (and/or vice versa). An example of this can be found in the Connecting Links to Links Using Link Label Nodes sample.
Link label segmentOffset and alignmentFocus
There are two ways of making small adjustments to the position of a label object given a particular point on a link segment specified by the segment index and fractional distance.
The GraphObject.segmentOffset property controls where to position the object relative to the point on a link segment determined by the GraphObject.segmentIndex and GraphObject.segmentFraction properties. The offset is not a simple offset of the point -- it is rotated according to the angle of that link segment. A positive value for the Y offset moves the label element towards the right side of the link, as seen going in the direction of the link. Naturally a negative value for the Y offset moves it towards the left side. Try dragging the harbor and cove nodes to see this in action:
If you drag one node around in a circle around the other one, you will see how the "Port" and "Starboard" labels always stay on their correct sides, just as they would on a ship sailing in the direction of the arrow.
Another way to change the effective offset is by changing the spot in the object that is being positioned relative to the link segment point. You can do that by setting the GraphObject.alignmentFocus, which as you have seen above defaults to Spot.Center. (GraphObject.alignmentFocus is also used by other Panel types, which is why its name does not start with "segment".) In the following example, the "West" label has its alignment focus set just to the right of its text and the "East" label has the opposite. This causes each to appear on their respective sides regardless of the orientation of the link.
Yet you may instead want to control the angle of the individual labels based on the angle of the link segment.
Link label segmentOrientation
The GraphObject.segmentOrientation property controls the angle of the label object relative to the angle of the link segment. There are several possible values that you can use. The default orientation is Orientation.None, meaning no rotation at all. Orientation.Along is commonly used to have the object always rotated at the same angle as the link segment. Orientation.Upright is like Orientation.Along, but is often used when there is text in the label, to make it easier to read.
Now if you move a node around you will always be able to read the label texts, and yet each label stays on its intended side of the link, as seen going in the direction of the link.
This points out a difference between a segmentIndex/segmentFraction pair of 0/1 and 1/0. Although they both refer to the same point, the angle associated with the first pair is the angle of the first segment (segment 0), whereas the angle associated with the second pair is the angle of the second segment.
Link labels near the ends
For labels that are near either end of a link, it may be convenient to set the GraphObject.segmentOffset to Point(NaN, NaN). This causes the offset to be half the width and half the height of the label object.
Arrowheads
Now that you know more about the GraphObject "segment..." properties for controlling the position and angle of objects in a Link, it is easy to explain how arrowheads are defined. Arrowheads are just labels: Shapes that are initialized in a convenient manner.
You can see a copy of all of the built-in arrowhead definitions in this file: Arrowheads.js.
Here are the equivalent settings for initializing an arrowhead Shape by setting Shape.toArrow to "Standard" with diagram zoomed in 2x: