1
1
import { Graph as GGraph } 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
EdgeData ,
@@ -14,8 +14,9 @@ import type {
14
14
OutNodeData ,
15
15
PointTuple ,
16
16
} from '../types' ;
17
- import { cloneFormatData , isArray } from '../util' ;
17
+ import { cloneFormatData , formatNodeSizeToNumber } from '../util' ;
18
18
import { handleSingleNodeGraph } from '../util/common' ;
19
+ import type { Size } from '../util/size' ;
19
20
import Body from './body' ;
20
21
import Quad from './quad' ;
21
22
import QuadTree from './quad-tree' ;
@@ -57,7 +58,7 @@ type CalcGraph = GGraph<OutNodeData, EdgeData>;
57
58
58
59
/**
59
60
* <zh/> Atlas2 力导向布局
60
- *
61
+ *
61
62
* <en/> Force Atlas 2 layout
62
63
*/
63
64
export class ForceAtlas2Layout implements Layout < ForceAtlas2LayoutOptions > {
@@ -82,7 +83,7 @@ export class ForceAtlas2Layout implements Layout<ForceAtlas2LayoutOptions> {
82
83
* To directly assign the positions to the nodes.
83
84
*/
84
85
async assign ( graph : Graph , options ?: ForceAtlas2LayoutOptions ) {
85
- await this . genericForceAtlas2Layout ( true , graph , options ) ;
86
+ await this . genericForceAtlas2Layout ( true , graph , options ) ;
86
87
}
87
88
88
89
private async genericForceAtlas2Layout (
@@ -122,7 +123,7 @@ export class ForceAtlas2Layout implements Layout<ForceAtlas2LayoutOptions> {
122
123
nodes : calcNodes ,
123
124
edges : calcEdges ,
124
125
} ) ;
125
- const sizes : SizeMap = this . getSizes ( calcGraph , graph , nodeSize ) ;
126
+ const sizes : SizeMap = this . getSizes ( calcGraph , nodeSize ) ;
126
127
127
128
this . run ( calcGraph , graph , maxIteration , sizes , assign , mergedOptions ) ;
128
129
@@ -164,41 +165,18 @@ export class ForceAtlas2Layout implements Layout<ForceAtlas2LayoutOptions> {
164
165
* Init the node positions if there is no initial positions.
165
166
* And pre-calculate the size (max of width and height) for each node.
166
167
* @param calcGraph graph for calculation
167
- * @param graph origin graph
168
168
* @param nodeSize node size config from layout options
169
169
* @returns {SizeMap } node'id mapped to max of its width and height
170
170
*/
171
171
private getSizes (
172
172
calcGraph : CalcGraph ,
173
- graph : Graph ,
174
- nodeSize ?: number | number [ ] | ( ( d ?: Node ) => number ) ,
173
+ nodeSize ?: Size | ( ( d ?: Node ) => Size ) ,
175
174
) : SizeMap {
176
175
const nodes = calcGraph . getAllNodes ( ) ;
177
176
const sizes : SizeMap = { } ;
178
177
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 ) ;
202
180
}
203
181
return sizes ;
204
182
}
0 commit comments