Skip to content

Commit 575d072

Browse files
committed
refactor: adapt force-atlas2 node size
1 parent d14d315 commit 575d072

File tree

2 files changed

+11
-32
lines changed

2 files changed

+11
-32
lines changed

packages/layout/src/force-atlas2/index.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Graph as GGraph } from '@antv/graphlib';
2-
import { isFunction, isNumber, isObject } from '@antv/util';
2+
import { isNumber } from '@antv/util';
33
import 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';
1818
import { handleSingleNodeGraph } from '../util/common';
19+
import type { Size } from '../util/size';
1920
import Body from './body';
2021
import Quad from './quad';
2122
import 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
*/
6364
export 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
}

packages/layout/src/util/function.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export function formatSizeFn<T extends Node>(
8888
export const formatNodeSizeToNumber = (
8989
nodeSize: Size | ((node: Node) => Size) | undefined,
9090
nodeSpacing: number | ((node: Node) => number) | undefined,
91+
defaultNodeSize: number = 10,
9192
): ((node: Node) => number) => {
9293
let nodeSizeFunc;
9394
const nodeSpacingFunc =
@@ -103,7 +104,7 @@ export const formatNodeSizeToNumber = (
103104
return [dataSize.width, dataSize.height];
104105
return dataSize;
105106
}
106-
return 10;
107+
return defaultNodeSize;
107108
};
108109
} else if (Array.isArray(nodeSize)) {
109110
nodeSizeFunc = (d: Node) => nodeSize;

0 commit comments

Comments
 (0)