This is the sixth article of the intermediate section of the JointJS tutorial. Return to custom links. See index of basic and intermediate articles.
The basic tutorial series offered an introduction into link labels. This section explains everything you need to know to make full use of the powerful label system.
JointJS offers a full suite of methods for working with link labels:
link.labels(labels)
- sets the labels
array of the link.
If called without arguments, returns the labels
array.link.label(index, label)
- sets the provided label
at the given index
.
If called with index
only, returns that label.link.insertLabel(index, label)
- inserts the provided label
at the given index
.link.appendLabel(label)
- shortcut function.
Inserts the label
at the end of the labels
array.link.removeLabel(index)
- removes the label at index
.A simple label definition (including markup and attrs) is built into the joint.dia.Link
class, from which all subtypes, including joint.shapes.standard.Link
, inherit it.
The built-in default label has two tags:
text
(the <text>
SVGElement of the label), and
rect
(the <rect>
SVGElement for the label background).
The built-in default attributes specify a simple vertical-centered text on a white rounded rectangle.
Thus, adding a label can be as simple as passing a value for the text/text
attribute:
link.appendLabel({
attrs: {
text: {
text: 'Hello, World!'
}
}
});
JointJS source code: links-label-builtin.js
The full built-in default label definition can be found in the documentation.
Labels are positioned at the center point of the link (distance
of 0.5
) by
default.
Three kinds of label.position.distance
values are recognized for setting a custom position.
A value between 0
and 1
causes the label to be positioned relatively to link length.
Positive values signify absolute position in local SVG units away from the start point.
Finally, negative values mean absolute position away from end point.
An animated example is presented below.
(Link labels can also be emulated with link
subelements and special attributes; this technique is explained elsewhere in the tutorial).
link.appendLabel({
attrs: {
text: {
text: '0.25'
}
},
position: {
distance: 0.25
}
});
link.appendLabel({
attrs: {
text: {
text: '150'
}
},
position: {
distance: 150
}
});
link.appendLabel({
attrs: {
text: {
text: '-100'
}
},
position: {
distance: -100
}
});
JointJS source code: link-labels-distance.js
It is also possible to set label offsets.
This is done with the label.position.offset
property.
With a positive number, the label is offset relatively and to the right of the link; a negative number
causes the label to be offset to the left.
An object with x
and y
coordinates offsets the label absolutely by that amount
in the two dimensions.
The following example illustrates these three options.
The red asterisk marks the reference point of all labels on the link.
link.appendLabel({
attrs: {
text: {
text: 'offset: 40'
}
},
position: {
distance: 0.66,
offset: 40
}
});
link.appendLabel({
attrs: {
text: {
text: 'offset: -40'
}
},
position: {
distance: 0.66,
offset: -40
}
});
link.appendLabel({
attrs: {
text: {
text: 'offset: -40,80'
}
},
position: {
distance: 0.66,
offset: {
x: -40,
y: 80
}
}
});
JointJS source code: link-labels-offset.js
By default, the labels' anchor point is centered horizontally and vertically, as it was in this example.
This can be changed by the textAnchor
and
textVerticalAnchor
attributes, respectively.
Link labels are horizontal by default, but JointJS allows you to specify label rotation.
If you provide a value for the label.position.angle
property, the link will rotate clockwise by that amount (with respect to the path of the link).
Furthermore, the label.position.args.keepGradient
boolean flag has another function, which may be combined with providing a specific rotation angle - when set to true
, it rotates the label according to the slope of the connection path at the given position.
Additionally, if the label.position.args.ensureLegibility
boolean flag is set to true
, it ensures that the text never rotates to end up upside-down - if necessary, JointJS adds an additional 180-degree rotation to make the text legible.
The following example shows rotated links in action.
The red asterisk marks the reference point of the two labels that are offset from the connection path.
link.appendLabel({
attrs: {
text: {
text: '70°\nkeepGradient'
}
},
position: {
distance: 0.05,
angle: 70,
args: {
keepGradient: true
}
}
});
link.appendLabel({
attrs: {
text: {
text: '0°\nkeepGradient'
}
},
position: {
distance: 0.3,
args: {
keepGradient: true
}
}
});
link.appendLabel({
attrs: {
text: {
text: '45°'
}
},
position: {
distance: 0.8,
angle: 45
}
});
link.appendLabel({
attrs: {
text: {
text: '135°'
}
},
position: {
distance: 0.9,
angle: 135
}
});
link.appendLabel({
attrs: {
text: {
text: '270°\nkeepGradient'
}
},
position: {
distance: 0.66,
offset: 80,
angle: 270,
args: {
keepGradient: true
}
}
});
link.appendLabel({
attrs: {
text: {
text: '270°\nkeepGradient\nensureLegibility'
}
},
position: {
distance: 0.66,
offset: -80,
angle: 270,
args: {
keepGradient: true,
ensureLegibility: true
}
}
});
JointJS source code: link-labels-rotation.js
Of course, it is also possible to change the appearance of your labels.
Simply specify custom markup
in JSON format and pass some attrs
.
As a bonus, you can define custom selectors to identify individual components of your label.
Let's define a complex circular label that shows what JointJS can do:
link.appendLabel({
markup: [
{
tagName: 'circle',
selector: 'body'
}, {
tagName: 'text',
selector: 'label'
}, {
tagName: 'circle',
selector: 'asteriskBody'
}, {
tagName: 'text',
selector: 'asterisk'
}
],
attrs: {
label: {
text: '½',
fill: '#000000',
fontSize: 14,
textAnchor: 'middle',
yAlignment: 'middle',
pointerEvents: 'none'
},
body: {
ref: 'label',
fill: '#ffffff',
stroke: '#000000',
strokeWidth: 1,
r: 'calc(s)',
cx: 0,
cy: 0
},
asterisk: {
ref: 'label',
text: '*',
fill: '#ff0000',
fontSize: 8,
textAnchor: 'middle',
textVerticalAnchor: 'middle',
pointerEvents: 'none',
x: 'calc(x+16.5)',
y: 'calc(y-2)'
},
asteriskBody: {
ref: 'asterisk',
fill: '#ffffff',
stroke: '#000000',
strokeWidth: 1,
r: 'calc(s)',
cx: 'calc(x+calc(0.5*w))',
cy: 'calc(y+calc(0.5*h))'
}
}
});
JointJS source code: link-labels-styling.js
The ref
attributes are an example of JointJS special attributes.
These are discussed in detail elsewhere in
the tutorial.
By default, users cannot interact with link labels in any way.
However, you can enable label dragging for all labels with the paper.options.interactive
paper option:
var paper = new joint.dia.Paper({
// ...
interactive: {
linkMove: false,
labelMove: true,
arrowheadMove: false,
vertexMove: false,
vertexAdd: false,
vertexRemove: false,
useLinkTools: false
}
});
JointJS source code: link-labels-interaction.js
The new position of dragged labels is recorded relatively to the link path. That means that the label will reposition itself when the link changes. (Unfortunately, it also means that the label can never be dragged beyond the endpoints of the link).
If you only want to allow the label to be dragged along the length of the link (and not outside of it), you can do so by specifying the paper.options.snapLabels
paper option:
var paper = new joint.dia.Paper({
// ...
snapLabels: true,
interactive: {
linkMove: false,
labelMove: true,
arrowheadMove: false,
vertexMove: false,
vertexAdd: false,
vertexRemove: false,
useLinkTools: false
}
});
JointJS source code: link-labels-interaction-snap-labels.js
In the next section of the intermediate tutorial, we will learn about element tools.