From 06c485dd080667400f1763c0a0cbda7fd31c1c4f Mon Sep 17 00:00:00 2001 From: liu2592603532 <2592603532@qq.com> Date: Wed, 10 Apr 2024 15:28:56 +0800 Subject: [PATCH] merge --- .../pages/Experiment/experimentText/index.jsx | 49 ++++++++++++++----- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/react-ui/src/pages/Experiment/experimentText/index.jsx b/react-ui/src/pages/Experiment/experimentText/index.jsx index 15fd0f51..18e525b6 100644 --- a/react-ui/src/pages/Experiment/experimentText/index.jsx +++ b/react-ui/src/pages/Experiment/experimentText/index.jsx @@ -168,6 +168,27 @@ function ExperimentText() { }, [message]); const initGraph = () => { + const fittingString = (str, maxWidth, fontSize) => { + const ellipsis = '...'; + const ellipsisLength = G6.Util.getTextSize(ellipsis, fontSize)[0]; + let currentWidth = 0; + let res = str; + const pattern = new RegExp('[\u4E00-\u9FA5]+'); // distinguish the Chinese charactors and letters + str.split('').forEach((letter, i) => { + if (currentWidth > maxWidth - ellipsisLength) return; + if (pattern.test(letter)) { + // Chinese charactors + currentWidth += fontSize; + } else { + // get the width of single letter according to the fontSize + currentWidth += G6.Util.getLetterWidth(letter, fontSize); + } + if (currentWidth > maxWidth - ellipsisLength) { + res = `${str.substr(0, i)}${ellipsis}`; + } + }); + return res; + }; G6.registerNode( 'rect-node', { @@ -194,19 +215,21 @@ function ExperimentText() { }, draggable: true, }); - // if (cfg.label) { - // group.addShape('text', { - // attrs: { - // x: 0, - // y: cfg.height / 2 - 5, - // textAlign: 'center', - // textBaseline: 'middle', - // text: cfg.label, - // fill: '#fff', - // }, - // draggable: true, - // }); - // } + if (cfg.label) { + group.addShape('text', { + attrs: { + text: fittingString(cfg.label, 70, 10), + x: -20, + y: 0, + fontSize: 10, + textAlign: 'left', + textBaseline: 'middle', + fill: '#000', + }, + name: 'text-shape', + draggable: true, + }); + } const bbox = group.getBBox(); const anchorPoints = this.getAnchorPoints(cfg); // console.log(anchorPoints);