1
1
import { Graph as IGraph } from '@antv/graphlib' ;
2
- import { isFunction , isNumber , isObject } from '@antv/util' ;
2
+ import { isNumber } from '@antv/util' ;
3
3
import type {
4
4
Edge ,
5
5
ForceLayoutOptions ,
@@ -11,7 +11,7 @@ import type {
11
11
OutNode ,
12
12
Point ,
13
13
} from '../types' ;
14
- import { formatNumberFn , isArray } from '../util' ;
14
+ import { formatNodeSizeToNumber , formatNumberFn } from '../util' ;
15
15
import { forceNBody } from './force-n-body' ;
16
16
import {
17
17
CalcEdge ,
@@ -42,7 +42,7 @@ const DEFAULTS_LAYOUT_OPTIONS: Partial<ForceLayoutOptions> = {
42
42
43
43
/**
44
44
* <zh/> 力导向布局
45
- *
45
+ *
46
46
* <en/> Force-directed layout
47
47
*/
48
48
export class ForceLayout implements LayoutWithIterations < ForceLayoutOptions > {
@@ -85,7 +85,7 @@ export class ForceLayout implements LayoutWithIterations<ForceLayoutOptions> {
85
85
* To directly assign the positions to the nodes.
86
86
*/
87
87
async assign ( graph : Graph , options ?: ForceLayoutOptions ) {
88
- await this . genericForceLayout ( true , graph , options ) ;
88
+ await this . genericForceLayout ( true , graph , options ) ;
89
89
}
90
90
91
91
/**
@@ -292,12 +292,7 @@ export class ForceLayout implements LayoutWithIterations<ForceLayoutOptions> {
292
292
graph : Graph ,
293
293
) : FormatedOptions {
294
294
const formattedOptions = { ...options } as FormatedOptions ;
295
- const {
296
- width : propsWidth ,
297
- height : propsHeight ,
298
- getMass,
299
- nodeSize,
300
- } = options ;
295
+ const { width : propsWidth , height : propsHeight , getMass } = options ;
301
296
302
297
// === formating width, height, and center =====
303
298
formattedOptions . width =
@@ -326,34 +321,10 @@ export class ForceLayout implements LayoutWithIterations<ForceLayoutOptions> {
326
321
}
327
322
328
323
// === formating node size =====
329
-
330
- const nodeSpacingFunc = formatNumberFn < Node > ( 0 , options . nodeSpacing ) ;
331
- let nodeSizeFn ;
332
- if ( ! nodeSize ) {
333
- nodeSizeFn = ( d ?: Node ) => {
334
- const { size } = d ?. data || { } ;
335
- if ( size ) {
336
- if ( isArray ( size ) ) {
337
- return Math . max ( size [ 0 ] , size [ 1 ] ) + nodeSpacingFunc ( d ) ;
338
- }
339
- if ( isObject < { width : number ; height : number } > ( size ) ) {
340
- return Math . max ( size . width , size . height ) + nodeSpacingFunc ( d ) ;
341
- }
342
- return ( size as number ) + nodeSpacingFunc ( d ) ;
343
- }
344
- return 10 + nodeSpacingFunc ( d ) ;
345
- } ;
346
- } else if ( isFunction ( nodeSize ) ) {
347
- nodeSizeFn = ( d ?: Node ) => ( nodeSize as Function ) ( d ) + nodeSpacingFunc ( d ) ;
348
- } else if ( isArray ( nodeSize ) ) {
349
- nodeSizeFn = ( d ?: Node ) => {
350
- const nodeSizeArr = nodeSize as [ number , number ] ;
351
- return Math . max ( nodeSizeArr [ 0 ] , nodeSizeArr [ 1 ] ) + nodeSpacingFunc ( d ) ;
352
- } ;
353
- } else {
354
- nodeSizeFn = ( d ?: Node ) => ( nodeSize as number ) + nodeSpacingFunc ( d ) ;
355
- }
356
- formattedOptions . nodeSize = nodeSizeFn ;
324
+ formattedOptions . nodeSize = formatNodeSizeToNumber (
325
+ options . nodeSize ,
326
+ options . nodeSpacing ,
327
+ ) ;
357
328
358
329
// === formating node / edge strengths =====
359
330
const linkDistanceFn = options . linkDistance
0 commit comments