Skip to content

Commit 3ba5b82

Browse files
committed
Multiline labels are now wrapped correctly.
1 parent 3f857b0 commit 3ba5b82

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/model/Node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ export class Node extends BaseElement {
151151
else {
152152
jsStringProperties.push(`fontSize: '1em'`);
153153
}
154-
if (this.label !== undefined) {
155-
jsStringProperties.push(`label: '${this.label.replace("'", "\\'")}'`);
154+
if (label !== '') {
155+
jsStringProperties.push(`label: '${label}'`);
156156
} else {
157157
jsStringProperties.push(`label: '${this.id}'`);
158158
}

templates/dgmlViewer_Template.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,26 +201,42 @@
201201
});
202202
}
203203

204-
function calculateLabelWidths() {
204+
function calculateLabelHeightsAndWidths() {
205205
nodeElements.forEach(node => {
206206
if (node.data.label && node.data.label.length > 0) {
207-
node.data.width = txtCtx.measureText(node.data.label).width * 1.75; // Don't know why, but the width of node has to be about 75% bigger than the width of the label text.
207+
let labelText = node.data.label;
208+
let metrics = txtCtx.measureText(labelText);
209+
if (labelText.indexOf('\n') > -1){
210+
let longestStringLength = 0;
211+
const lines = node.data.label.split('\n');
212+
lines.forEach(s => {
213+
if (s.length > longestStringLength) {
214+
longestStringLength = s.length;
215+
labelText = s;
216+
}
217+
});
218+
metrics = txtCtx.measureText(labelText);
219+
node.data.height = (metrics.fontBoundingBoxAscent + metrics.fontBoundingBoxDescent) * (lines.length + 2);
220+
}
221+
node.data.width = metrics.width * 1.75; // Don't know why, but the width of node has to be about 75% bigger than the width of the label text.
208222
}
209223
});
210224
}
211225

212226
function setNetworkLayout() {
213-
calculateLabelWidths();
227+
calculateLabelHeightsAndWidths();
214228
cy = cytoscape({
215229
container: cyContainerDiv,
216230

217231
style: [{
218232
selector: 'node',
219233
style: {
220234
'width': 'data(width)',
235+
'height': 'data(height)',
221236
'label': 'data(label)',
222237
'text-valign': 'data(labelvalign)',
223238
'text-halign': 'center',
239+
'text-wrap': 'wrap',
224240
'font-family': 'data(fontFamily)',
225241
'font-size': 'data(fontSize)',
226242
'font-weight': 'data(fontWeight)',

0 commit comments

Comments
 (0)