1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
| export enum ItemStatus { OPACITY = 'opacity', NORMAL = 'normal', ACTIVE = 'active', }
function getLetterWidth(letter: string, fontSize: number) { const pattern = new RegExp('[\u4E00-\u9FA5]+'); if (pattern.test(letter)) { return fontSize; } else { return G6.Util.getLetterWidth(letter, fontSize); } }
export function getStringWidth(str: string, fontSize: number) { let currentWidth = 0; str.split('').forEach(letter => { currentWidth += getLetterWidth(letter, fontSize); }); return currentWidth; }
function replaceTooLongStringWithEllipsis( strs: string[], maxWidth: number, fontSize: number, ) { const ellipsis = '...'; const ellipsisLength = getStringWidth(ellipsis, fontSize); if (strs.length > 2) { const secondLine = strs[1]; let currentWidth = ellipsisLength; for (let i = 0; i < secondLine.length; i++) { currentWidth += getLetterWidth(secondLine[i], fontSize); if (currentWidth >= maxWidth) { strs[1] = `${secondLine.slice(0, i)}${ellipsis}`; } } return [strs[0], strs[1]]; } else { return strs; } }
const NODE_ICON_SIZE = 48; const NODE_ICON_RADIUS = NODE_ICON_SIZE / 2; const NODE_NAME_FONT_SIZE = 14; const NODE_NAME_HEIGHT = 18
G6.registerNode( 'test-node', { draw(cfg, group) { const keyShape = group?.addShape('circle', { attrs: { x: 0, y: 0, r: NODE_ICON_RADIUS, fill: 'rgba(255,255,255,0)', opacity: 0, lineWidth: 1, cursor: 'pointer', }, name: 'test-node-dummy', draggle: true, zIndex: 10, });
group?.addShape('image', { attrs: { x: -NODE_ICON_SIZE / 2, y: -NODE_ICON_SIZE / 2, width: NODE_ICON_SIZE, height: NODE_ICON_SIZE, img: '', cursor: 'pointer', }, name: 'test-node-icon', draggle: true, zIndex: 9, });
const lableSplit = fittingString( cfg?.supName as string, 200, NODE_NAME_FONT_SIZE, ); for (let i = 0; i < lableSplit.length; i++) { const label = lableSplit[i]; const labelWidth = getStringWidth( label, NODE_NAME_FONT_SIZE, ); group?.addShape('rect', { attrs: { x: -labelWidth / 2, y: NODE_ICON_SIZE - NODE_NAME_HEIGHT + i * NODE_NAME_HEIGHT + 4, width: labelWidth, height: NODE_NAME_HEIGHT, fill: '#FFFFFF', }, name: `test-node-name-background-${i}`, zIndex: 3, });
group?.addShape('text', { attrs: { text: label, fill: '#646A73', fontSize: 14, textAlign: 'center', x: 0, y: NODE_ICON_SIZE + i * NODE_NAME_HEIGHT, width: labelWidth, height: NODE_NAME_HEIGHT, cursor: 'pointer', }, name: `test-node-name-${i}`, zIndex: 3, });
group?.addShape('path', { attrs: { path: [ [ 'M', -labelWidth / 2, NODE_ICON_SIZE + i * NODE_NAME_HEIGHT + 2, ], [ 'L', labelWidth / 2, NODE_ICON_SIZE + i * NODE_NAME_HEIGHT + 2, ], ], stroke: '#000000', lineWidth: 1, lineDash: [2, 2], }, name: `test-node-name-underline-${i}`, zIndex: 3, }); }
const back1 = group?.addShape('circle', { zIndex: 5, attrs: { x: 0, y: 0, r: 0, fill: 'rgba(255,255,255,0)', opacity: 0, }, name: 'test-node-wave1', draggle: true, }); const back2 = group?.addShape('circle', { zIndex: 6, attrs: { x: 0, y: 0, r: 0, fill: 'rgba(255,255,255,0)', opacity: 0, }, name: 'test-node-wave2', draggle: true, }); const back3 = group?.addShape('circle', { zIndex: 7, attrs: { x: 0, y: 0, r: 0, fill: 'rgba(255,255,255,0)', opacity: 0, }, name: 'test-node-wave3', draggle: true, });
back1?.animate( { r: NODE_ICON_RADIUS + 16, opacity: 0.1, }, { duration: 3000, easing: 'easeCubic', delay: 100, repeat: true, }, ); back2?.animate( { r: NODE_ICON_RADIUS + 16, opacity: 0.1, }, { duration: 3000, easing: 'easeCubic', delay: 1000, repeat: true, }, ); back3?.animate( { r: NODE_ICON_RADIUS + 16, opacity: 0.1, }, { duration: 3000, easing: 'easeCubic', delay: 2000, repeat: true, }, );
back1?.hide(); back2?.hide(); back3?.hide();
group?.sort(); return keyShape as IShape; }, setState(name, value, item) { if (name === 'status') { if (value === ItemStatus.ACTIVE) { const group = item?.getContainer(); const shapes = group?.getChildren() ?? []; for (const shape of shapes) { if (shape.cfg.name?.includes('test-node-wave')) { shape.attr('opacity', 0.6); shape.attr('fill', '#4E83FD'); shape.show(); } else if (shape.cfg.name?.includes('test-node-dummy')) { shape.attr('opacity', 1); shape.attr('stroke', '#4E83FD'); } else { shape.attr('opacity', 1); } } } else { const group = item?.getContainer(); const shapes = group?.getChildren() ?? []; for (const shape of shapes) { if (shape.cfg.name?.includes('test-node-wave')) { shape.attr('opacity', 0); shape.attr('fill', '#FFFFFF'); shape.hide(); } else if (shape.cfg.name?.includes('test-node-dummy')) { shape.attr('opacity', 0); shape.attr('stroke', '#FFFFFF'); } else { shape.attr('opacity', value === ItemStatus.OPACITY ? 0.2 : 1); } } } } }, }, 'single-node', );
|