Skip to content

Commit a9af1af

Browse files
committed
refactor: adapt force node size
1 parent 575d072 commit a9af1af

File tree

1 file changed

+9
-38
lines changed

1 file changed

+9
-38
lines changed

packages/layout/src/force/index.ts

+9-38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Graph as IGraph } from '@antv/graphlib';
2-
import { isFunction, isNumber, isObject } from '@antv/util';
2+
import { isNumber } from '@antv/util';
33
import type {
44
Edge,
55
ForceLayoutOptions,
@@ -11,7 +11,7 @@ import type {
1111
OutNode,
1212
Point,
1313
} from '../types';
14-
import { formatNumberFn, isArray } from '../util';
14+
import { formatNodeSizeToNumber, formatNumberFn } from '../util';
1515
import { forceNBody } from './force-n-body';
1616
import {
1717
CalcEdge,
@@ -42,7 +42,7 @@ const DEFAULTS_LAYOUT_OPTIONS: Partial<ForceLayoutOptions> = {
4242

4343
/**
4444
* <zh/> 力导向布局
45-
*
45+
*
4646
* <en/> Force-directed layout
4747
*/
4848
export class ForceLayout implements LayoutWithIterations<ForceLayoutOptions> {
@@ -85,7 +85,7 @@ export class ForceLayout implements LayoutWithIterations<ForceLayoutOptions> {
8585
* To directly assign the positions to the nodes.
8686
*/
8787
async assign(graph: Graph, options?: ForceLayoutOptions) {
88-
await this.genericForceLayout(true, graph, options);
88+
await this.genericForceLayout(true, graph, options);
8989
}
9090

9191
/**
@@ -292,12 +292,7 @@ export class ForceLayout implements LayoutWithIterations<ForceLayoutOptions> {
292292
graph: Graph,
293293
): FormatedOptions {
294294
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;
301296

302297
// === formating width, height, and center =====
303298
formattedOptions.width =
@@ -326,34 +321,10 @@ export class ForceLayout implements LayoutWithIterations<ForceLayoutOptions> {
326321
}
327322

328323
// === 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+
);
357328

358329
// === formating node / edge strengths =====
359330
const linkDistanceFn = options.linkDistance

0 commit comments

Comments
 (0)