Skip to content

Commit 547267b

Browse files
authored
Merge pull request #413 from RedisInsight/redisgraph-node-label-wrap
[RedisGraph Plugin] Add support for node label text wrapping
2 parents 3e3cf8b + f508eaf commit 547267b

File tree

2 files changed

+55
-28
lines changed

2 files changed

+55
-28
lines changed

redisinsight/ui/src/packages/redisgraph/src/graphd3.ts

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,14 @@ function GraphD3(_selector: HTMLDivElement, _options: any) {
115115
if (svgScale) {
116116
scale *= svgScale;
117117
}
118-
let text = svg.selectAll('.node .text');
119-
let textSize = nominalTextSize;
120-
if (nominalTextSize * scale > maxTextSize) textSize = maxTextSize / scale;
121-
text.attr("font-size", (textSize - 3) + "px");
122-
text.text((d) => {
123-
let label: string;
124-
if (typeof options.onLabelNode === 'function') {
125-
label = options.onLabelNode(d);
126-
} else {
127-
label = d.properties?.name || (d.labels?.length ? d.labels[0] : '');
128-
}
129-
let maxLength = (maxTextSize - textSize) + 3;
130-
return label.length > maxLength ? Utils.truncateText(label, maxLength) : label;
131-
})
132-
svg.attr('transform', `translate(${translate[0]}, ${translate[1]}) scale(${scale})`);
118+
119+
let node = svg.selectAll('.node')
120+
let textSize = nominalTextSize
121+
if (nominalTextSize * scale > maxTextSize) textSize = maxTextSize / scale
122+
appendTextToNode(node, textSize)
123+
let text = node.selectAll('.text')
124+
text.attr("font-size", (textSize - 3) + "px")
125+
svg.attr('transform', `translate(${translate[0]}, ${translate[1]}) scale(${scale})`)
133126
}))
134127
.on('dblclick.zoom', null)
135128
svg = mainSvg.append('g')
@@ -346,23 +339,49 @@ function GraphD3(_selector: HTMLDivElement, _options: any) {
346339
.attr('y', '14');
347340
}
348341

349-
function appendTextToNode(svgNode) {
350-
svgNode.append('text')
351-
.text((d) => {
352-
let label;
353-
342+
function appendTextToNode(svgNode, textSize = null) {
343+
svgNode
344+
.selectAll("text")
345+
.data((d) => {
346+
let label: string
354347
if (typeof options.onLabelNode === 'function') {
355-
label = options.onLabelNode(d);
348+
label = options.onLabelNode(d)
356349
} else {
357-
label = d.properties?.name || (d.labels?.length ? d.labels[0] : '');
350+
label = d.properties?.name || d.properties?.title;
358351
}
359-
360-
let maxLength = maxTextSize - nominalTextSize - 5;
361-
return label.length > maxLength ? Utils.truncateText(label, maxLength) : label;
352+
label ||= ''
353+
if (textSize !== null) {
354+
let maxLength = (maxTextSize - textSize) + 15;
355+
label = label.length > maxLength ? Utils.truncateText(label, maxLength) : label;
356+
}
357+
let wrappedLabel = Utils.wrapText(label, 7)
358+
return wrappedLabel.split('\n').map(k => ({
359+
text: k,
360+
actual: wrappedLabel,
361+
d,
362+
}))
363+
})
364+
.join("text")
365+
.text((d) => d.text)
366+
.attr("y", (d, i) => {
367+
const calculatePosition = (l: number) => {
368+
if (l == 1) return 0;
369+
else {
370+
let arr = []
371+
for (let m = 0; m < l / 2; m++) {
372+
let z = m * 10;
373+
arr = [-z - 5, ...arr]
374+
arr = [...arr, z + 5]
375+
}
376+
return arr;
377+
}
378+
}
379+
const r = calculatePosition(d.actual.split('\n').length);
380+
return r[i];
362381
})
363382
.attr('class', 'text')
364-
.attr('font-size', (d) => nominalTextSize + "px")
365-
.attr('fill', (d) => labelColors(d.labels?.length ? d.labels[0] : '').textColor)
383+
.attr('font-size', () => nominalTextSize + "px")
384+
.attr('fill', ({d}) => labelColors(d.labels?.length ? d.labels[0] : '').textColor)
366385
.attr('pointer-events', 'none')
367386
.attr('text-anchor', 'middle')
368387
.attr('dy', () => options.nodeRadius / ((options.nodeRadius * 25) / 100));
@@ -374,7 +393,7 @@ function GraphD3(_selector: HTMLDivElement, _options: any) {
374393
appendRingToNode(n);
375394
appendOutlineToNode(n);
376395
appendNodeInfo(n);
377-
appendTextToNode(n);
396+
appendTextToNode(n, null);
378397

379398
return n;
380399
}

redisinsight/ui/src/packages/redisgraph/src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,11 @@ export class GoodColorPicker extends ColorPicker<IGoodColor> {
228228
super(COLORS);
229229
}
230230
}
231+
232+
233+
export function wrapText(s: string, w: number) {
234+
return s.replace(
235+
new RegExp(`(?![^\\n]{1,${w}}$)([^\\n]{1,${w}})\\s`, 'g'),
236+
'$1\n'
237+
)
238+
}

0 commit comments

Comments
 (0)