Skip to content

Commit

Permalink
feat:fix mock type method
Browse files Browse the repository at this point in the history
  • Loading branch information
pomelo-nwu committed Nov 20, 2019
1 parent 210cfcb commit c54484e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
5 changes: 5 additions & 0 deletions packages/graphin/src/controller/layout/defaultLayouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ const defaultLayouts = (graphin: Graphin, prevProps: GraphinProps) => {
defSpringLen: 200,
/** repulsion 斥力,这里指代 库伦常量Ke */
repulsion: 200.0 * 5,
/** 向心力 */
centripetalOptions: {
leaf: 2,
single: 2,
},
/** 速度的减震因子,其实就是阻尼系数 */
damping: 0.9,
/** 最小能量阈值,当粒子运动,有阻尼系数的存在,最终会将初始的能量消耗殆尽 */
Expand Down
74 changes: 60 additions & 14 deletions packages/graphin/src/layout/force/ForceLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type ForceNodeType = Node;

type ForceEdgeType = Edge;

const getBaseLog = (x: number, y: number) => {
return Math.log(y) / Math.log(x);
};

interface Map<K, V> {
clear(): void;
delete(key: K): boolean;
Expand All @@ -32,6 +36,23 @@ class ForceLayout {
defSpringLen: 200;
/** repulsion 斥力,这里指代 库伦常量Ke */
repulsion: number;
/** 向心力 */
centripetalOptions: {
/** 叶子节点的施加力的因子 */
leaf?: number;
/** 孤立节点的施加力的因子 */
single?: number;
/** 其他节点的施加力的因子 */
others?: number;
/** 向心力的中心点,默认为画布的中心 */
center?: (
node: NodeType,
) => {
x: number;
y: number;
};
};

/** https://www.khanacademy.org/science/ap-physics-1/ap-electric-charge-electric-force-and-voltage/coulombs-law-and-electric-force-ap/a/coulombs-law-and-electric-force-ap-physics-1 */
/** volocity damping factor 速度的减震因子,其实就是阻尼系数 */
damping: number;
Expand Down Expand Up @@ -97,6 +118,10 @@ class ForceLayout {
stiffness: 200.0,
defSpringLen: 200,
repulsion: 200.0 * SPEED,
centripetalOptions: {
leaf: 2,
single: 2,
},
damping: 0.9,
minEnergyThreshold: 0.1,
maxSpeed: 1000,
Expand Down Expand Up @@ -382,29 +407,50 @@ class ForceLayout {
}

attractToCentre() {
const implementForce = (node: Node, center: Vector, radio = 100) => {
const implementForce = (node: Node, center: Vector, radio = 2) => {
const point = this.nodePoints.get(node.id);
const direction = point.p.subtract(center);
point.updateAcc(direction.scalarMultip(-this.props.repulsion / radio));
};
const getBaseLog = (x: number, y: number) => {
return Math.log(y) / Math.log(x);
};

point.updateAcc(direction.scalarMultip(-radio));
};
this.nodes.forEach(node => {
// 默认的向心力指向画布中心
const degree = (node.data && node.data.layout && node.data.layout.degree) as number;
const leafNode = degree === 1;
const singleNode = degree === 0;
const MaxRadio = 600;
if (singleNode || leafNode) {
const center = new Vector(this.props.width / 2, this.props.height / 2);
implementForce(node, center, MaxRadio);
} else {
const radio = MaxRadio / 2 / getBaseLog(2, degree);
const center = new Vector(this.props.width / 2, this.props.height / 2);
implementForce(node, center, radio);
/** 默认的向心力配置 */
const defaultRadio = {
left: 2,
single: 2,
others: 1 / getBaseLog(2, degree),
center: () => {
return {
x: this.props.width / 2,
y: this.props.height / 2,
};
},
};

const { leaf, single, others, center } = { ...defaultRadio, ...this.props.centripetalOptions };
const { x, y } = center(node);
const centerVector = new Vector(x, y);

/** 如果radio为0,则认为忽略向心力 */
if (leaf === 0 || single === 0 || others === 0) {
return;
}

if (singleNode) {
implementForce(node, centerVector, single);
return;
}

if (leafNode) {
implementForce(node, centerVector, leaf);
return;
}
/** others */
implementForce(node, centerVector, others);
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/graphin/src/utils/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class Mock {
};

type = (nodeType: string) => {
this.nodes.map(node => {
this.nodes = this.nodes.map(node => {
return {
...node,
type: nodeType,
Expand Down

0 comments on commit c54484e

Please sign in to comment.