Skip to content

Commit e1ea819

Browse files
committed
feat: fix layout issue
1 parent 30696fb commit e1ea819

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed

packages/studio-graph/src/components/ClearCanvas/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const ClearCanvas: React.FunctionComponent<IReportProps> = props => {
1414
draft.data = { nodes: [], edges: [] };
1515
draft.source = { nodes: [], edges: [] };
1616
draft.combos = [];
17-
draft.enableCombo = false;
17+
draft.layout = {
18+
type: 'force',
19+
options: {},
20+
};
1821
});
1922
if (graph) {
2023
graph.zoomToFit();

packages/studio-graph/src/graph/custom-combo/combo-force.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { get } from './utils';
55
* @param strength strength 是一个可调参数,默认值为 0.1
66
* @returns
77
*/
8-
export function forceRadial(groupMap, clusterKey, strength = 0.1) {
8+
export function forceRadial(groupMap, clusterKey, strength = 0.05) {
99
let nodes;
1010

1111
function force(alpha) {

packages/studio-graph/src/graph/custom-combo/render.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ export function renderCombo(graph: ForceGraphInstance, combos: ICombo[]) {
2121
const textX = x; // 文本的 x 坐标(与圆心 x 坐标对齐)
2222
const textY = y - r - 8; // 文本的 y 坐标(在圆的上方)
2323

24-
const w = Math.max(fontSize * String(text).length * 0.6, 50);
25-
const h = fontSize;
26-
ctx.strokeRect(textX - w / 2, textY - h / 2, w, h);
24+
// const w = Math.max(fontSize * String(text).length * 0.6, 50);
25+
// const h = fontSize;
26+
// ctx.strokeRect(textX - w / 2, textY - h / 2, w, h);
2727

2828
ctx.font = `${fontSize}px Arial`;
2929
ctx.fillStyle = color;

packages/studio-graph/src/graph/custom-combo/useComboLayout.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import { useContext } from '../../hooks/useContext';
2-
import { getGroups, get } from './utils';
2+
33
import { ForceGraphInstance } from 'force-graph';
44
import {
55
forceSimulation as d3ForceSimulation,
66
forceLink as d3ForceLink,
77
forceManyBody as d3ForceManyBody,
88
forceCenter as d3ForceCenter,
99
forceRadial as d3ForceRadial,
10+
forceCollide as d3ForceCollide,
1011
} from 'd3-force-3d';
1112
import * as d3Force from 'd3-force';
1213
import { handleStyle } from '../handleStyle';
1314

1415
import { forceCluster, forceRadial } from './combo-force';
15-
import { useEffect, useRef } from 'react';
16+
import { useEffect } from 'react';
1617
import { renderCombo } from './render';
1718

1819
const useComboLayout = () => {
1920
const { store } = useContext();
2021
const { graph, render, nodeStyle, combos, layout } = store;
21-
const linkForceRef = useRef<any>(null);
2222

2323
useEffect(() => {
2424
if (render === '3D') {
@@ -44,9 +44,6 @@ const useComboLayout = () => {
4444
combos.forEach(group => {
4545
combosMap.set(group.id, group);
4646
});
47-
if (linkForceRef.current === null) {
48-
linkForceRef.current = graph.d3Force('link');
49-
}
5047

5148
graph.d3Force('charge', null);
5249
graph.d3Force('center', null);
@@ -69,18 +66,6 @@ const useComboLayout = () => {
6966
if (reheatSimulation && graph) {
7067
graph.d3ReheatSimulation();
7168
}
72-
} else {
73-
/** 关闭聚类 */
74-
graph.d3Force('charge', d3ForceManyBody());
75-
graph.d3Force('center', d3ForceCenter());
76-
graph.d3Force('link', linkForceRef.current);
77-
78-
graph.d3Force('cluster', null);
79-
graph.d3Force('radial', null);
80-
81-
if ((graph as ForceGraphInstance).onRenderFramePost) {
82-
(graph as ForceGraphInstance).onRenderFramePost(() => {});
83-
}
8469
}
8570
}, [layout, combos, render, graph]);
8671
};

packages/studio-graph/src/graph/custom-combo/useCombos.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,8 @@ const useCombos = () => {
3535
updateStore(draft => {
3636
draft.combos = [];
3737
draft.layout = {
38-
type: 'force-combo',
39-
options: {
40-
enable: false,
41-
groupBy: '',
42-
reheatSimulation: false,
43-
},
38+
type: 'force',
39+
options: {},
4440
};
4541
});
4642
};

packages/studio-graph/src/graph/useGraph/useLayout.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ import { handleStyle } from '../handleStyle';
77
import { dagreLayout } from '../layout/dagre';
88
import { calculateRenderTime } from './useData';
99

10-
import { forceCenter as d3ForceCenter, forceCollide as d3ForceCollide } from 'd3-force-3d';
10+
import {
11+
forceSimulation as d3ForceSimulation,
12+
forceLink as d3ForceLink,
13+
forceManyBody as d3ForceManyBody,
14+
forceCenter as d3ForceCenter,
15+
forceRadial as d3ForceRadial,
16+
forceCollide as d3ForceCollide,
17+
} from 'd3-force-3d';
1118

1219
export const useLayout = () => {
1320
const { store } = useContext();
@@ -24,17 +31,28 @@ export const useLayout = () => {
2431
graph.cooldownTime(renderTime);
2532
graph.cooldownTicks(Infinity);
2633
graph.graphData(Utils.fakeSnapshot({ nodes: data.nodes, links: data.edges }));
34+
35+
/** 关闭聚类力导 */
36+
graph.d3Force('cluster', null);
37+
graph.d3Force('radial', null);
38+
/** 关闭聚类绘图 */
39+
if ((graph as ForceGraphInstance).onRenderFramePost) {
40+
(graph as ForceGraphInstance).onRenderFramePost(() => {});
41+
}
42+
2743
if (type === 'force') {
2844
if (render === '2D') {
2945
(graph as ForceGraphInstance)
46+
.d3Force('center', d3ForceCenter().strength(1))
47+
.d3Force('charge', d3ForceManyBody())
48+
.d3Force('link', d3ForceLink())
3049
.d3Force(
3150
'collide',
3251
d3ForceCollide().radius(node => {
3352
return handleStyle(node, nodeStyle).size + 5;
3453
}),
3554
)
36-
.d3Force('center', d3ForceCenter().strength(1));
37-
graph.d3ReheatSimulation();
55+
.d3ReheatSimulation();
3856
}
3957
}
4058
if (type === 'force-dagre') {

0 commit comments

Comments
 (0)