@@ -115,21 +115,14 @@ function GraphD3(_selector: HTMLDivElement, _options: any) {
115
115
if ( svgScale ) {
116
116
scale *= svgScale ;
117
117
}
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 } )` )
133
126
} ) )
134
127
. on ( 'dblclick.zoom' , null )
135
128
svg = mainSvg . append ( 'g' )
@@ -346,23 +339,49 @@ function GraphD3(_selector: HTMLDivElement, _options: any) {
346
339
. attr ( 'y' , '14' ) ;
347
340
}
348
341
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
354
347
if ( typeof options . onLabelNode === 'function' ) {
355
- label = options . onLabelNode ( d ) ;
348
+ label = options . onLabelNode ( d )
356
349
} else {
357
- label = d . properties ?. name || ( d . labels ?. length ? d . labels [ 0 ] : '' ) ;
350
+ label = d . properties ?. name || d . properties ?. title ;
358
351
}
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 ] ;
362
381
} )
363
382
. 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 )
366
385
. attr ( 'pointer-events' , 'none' )
367
386
. attr ( 'text-anchor' , 'middle' )
368
387
. attr ( 'dy' , ( ) => options . nodeRadius / ( ( options . nodeRadius * 25 ) / 100 ) ) ;
@@ -374,7 +393,7 @@ function GraphD3(_selector: HTMLDivElement, _options: any) {
374
393
appendRingToNode ( n ) ;
375
394
appendOutlineToNode ( n ) ;
376
395
appendNodeInfo ( n ) ;
377
- appendTextToNode ( n ) ;
396
+ appendTextToNode ( n , null ) ;
378
397
379
398
return n ;
380
399
}
0 commit comments