11import { Graph as GGraph } from '@antv/graphlib' ;
2- import { isFunction , isNumber , isObject } from '@antv/util' ;
2+ import { isNumber } from '@antv/util' ;
33import type {
44 Edge ,
55 EdgeData ,
@@ -14,8 +14,9 @@ import type {
1414 OutNodeData ,
1515 PointTuple ,
1616} from '../types' ;
17- import { cloneFormatData , isArray } from '../util' ;
17+ import { cloneFormatData , formatNodeSizeToNumber } from '../util' ;
1818import { handleSingleNodeGraph } from '../util/common' ;
19+ import type { Size } from '../util/size' ;
1920import Body from './body' ;
2021import Quad from './quad' ;
2122import QuadTree from './quad-tree' ;
@@ -57,7 +58,7 @@ type CalcGraph = GGraph<OutNodeData, EdgeData>;
5758
5859/**
5960 * <zh/> Atlas2 力导向布局
60- *
61+ *
6162 * <en/> Force Atlas 2 layout
6263 */
6364export class ForceAtlas2Layout implements Layout < ForceAtlas2LayoutOptions > {
@@ -82,7 +83,7 @@ export class ForceAtlas2Layout implements Layout<ForceAtlas2LayoutOptions> {
8283 * To directly assign the positions to the nodes.
8384 */
8485 async assign ( graph : Graph , options ?: ForceAtlas2LayoutOptions ) {
85- await this . genericForceAtlas2Layout ( true , graph , options ) ;
86+ await this . genericForceAtlas2Layout ( true , graph , options ) ;
8687 }
8788
8889 private async genericForceAtlas2Layout (
@@ -122,7 +123,7 @@ export class ForceAtlas2Layout implements Layout<ForceAtlas2LayoutOptions> {
122123 nodes : calcNodes ,
123124 edges : calcEdges ,
124125 } ) ;
125- const sizes : SizeMap = this . getSizes ( calcGraph , graph , nodeSize ) ;
126+ const sizes : SizeMap = this . getSizes ( calcGraph , nodeSize ) ;
126127
127128 this . run ( calcGraph , graph , maxIteration , sizes , assign , mergedOptions ) ;
128129
@@ -164,41 +165,18 @@ export class ForceAtlas2Layout implements Layout<ForceAtlas2LayoutOptions> {
164165 * Init the node positions if there is no initial positions.
165166 * And pre-calculate the size (max of width and height) for each node.
166167 * @param calcGraph graph for calculation
167- * @param graph origin graph
168168 * @param nodeSize node size config from layout options
169169 * @returns {SizeMap } node'id mapped to max of its width and height
170170 */
171171 private getSizes (
172172 calcGraph : CalcGraph ,
173- graph : Graph ,
174- nodeSize ?: number | number [ ] | ( ( d ?: Node ) => number ) ,
173+ nodeSize ?: Size | ( ( d ?: Node ) => Size ) ,
175174 ) : SizeMap {
176175 const nodes = calcGraph . getAllNodes ( ) ;
177176 const sizes : SizeMap = { } ;
178177 for ( let i = 0 ; i < nodes . length ; i += 1 ) {
179- const { id, data } = nodes [ i ] ;
180- sizes [ id ] = 10 ;
181- if ( isNumber ( data . size ) ) {
182- sizes [ id ] = data . size ;
183- } else if ( isArray ( data . size ) ) {
184- if ( ! isNaN ( data . size [ 0 ] ) ) sizes [ id ] = Math . max ( data . size [ 0 ] ) ;
185- if ( ! isNaN ( data . size [ 1 ] ) ) sizes [ id ] = Math . max ( data . size [ 1 ] ) ;
186- } else if ( isObject ( data . size ) ) {
187- // @ts -ignore
188- sizes [ id ] = Math . max ( data . size . width , data . size . height ) ;
189- } else if ( isFunction ( nodeSize ) ) {
190- const originNode = graph . getNode ( id ) ;
191- const size = nodeSize ( originNode ) ;
192- if ( isArray ( size ) ) {
193- sizes [ id ] = Math . max ( ...size ) ;
194- } else {
195- sizes [ id ] = size ;
196- }
197- } else if ( isArray ( nodeSize ) ) {
198- sizes [ id ] = Math . max ( ...nodeSize ) ;
199- } else if ( isNumber ( nodeSize ) ) {
200- sizes [ id ] = nodeSize ;
201- }
178+ const node = nodes [ i ] ;
179+ sizes [ node . id ] = formatNodeSizeToNumber ( nodeSize , undefined ) ( node ) ;
202180 }
203181 return sizes ;
204182 }
0 commit comments