balibabu
commited on
Commit
Β·
6f0c45a
1
Parent(s):
4635160
feat: Alter style of ForceGraph #162 (#1774)
Browse files### What problem does this PR solve?
feat: alter style of ForceGraph #162
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
- web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/force-graph.tsx +46 -39
- web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/index.less +5 -4
- web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/modal.tsx +2 -2
- web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/util.ts +0 -3
web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/force-graph.tsx
CHANGED
@@ -1,25 +1,19 @@
|
|
1 |
-
import {
|
2 |
-
import {
|
3 |
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
4 |
-
import {
|
5 |
-
import { Converter, buildNodesAndCombos, isDataExist } from './util';
|
6 |
|
7 |
-
import { useFetchKnowledgeGraph } from '@/hooks/chunk-hooks';
|
8 |
import styles from './index.less';
|
9 |
|
10 |
-
const
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
);
|
16 |
-
console.log('π ~ nextData:', nextData);
|
17 |
-
|
18 |
-
const finalData = { ...graphData, ...nextData };
|
19 |
|
20 |
const ForceGraph = () => {
|
21 |
const containerRef = useRef<HTMLDivElement>(null);
|
22 |
-
const
|
23 |
const { data } = useFetchKnowledgeGraph();
|
24 |
|
25 |
const nextData = useMemo(() => {
|
@@ -30,12 +24,12 @@ const ForceGraph = () => {
|
|
30 |
}
|
31 |
return { nodes: [], edges: [] };
|
32 |
}, [data]);
|
33 |
-
console.log('π ~ nextData ~ nextData:', nextData);
|
34 |
|
35 |
const render = useCallback(() => {
|
36 |
const graph = new Graph({
|
37 |
container: containerRef.current!,
|
38 |
autoFit: 'view',
|
|
|
39 |
behaviors: [
|
40 |
'drag-element',
|
41 |
'drag-canvas',
|
@@ -49,16 +43,23 @@ const ForceGraph = () => {
|
|
49 |
plugins: [
|
50 |
{
|
51 |
type: 'tooltip',
|
52 |
-
|
53 |
-
|
|
|
54 |
let result = ``;
|
55 |
items.forEach((item) => {
|
56 |
-
result += `<h3>${item?.id}</h3>`;
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
if (item?.description) {
|
58 |
result += `<p>${item?.description}</p>`;
|
59 |
}
|
60 |
});
|
61 |
-
return result;
|
62 |
}
|
63 |
return undefined;
|
64 |
},
|
@@ -68,44 +69,44 @@ const ForceGraph = () => {
|
|
68 |
type: 'combo-combined',
|
69 |
preventOverlap: true,
|
70 |
comboPadding: 1,
|
71 |
-
spacing:
|
72 |
},
|
73 |
node: {
|
74 |
style: {
|
75 |
-
size:
|
76 |
labelText: (d) => d.id,
|
77 |
-
labelPadding: 30,
|
|
|
78 |
// labelOffsetX: 20,
|
79 |
-
|
80 |
labelPlacement: 'center',
|
81 |
labelWordWrap: true,
|
82 |
},
|
83 |
palette: {
|
84 |
type: 'group',
|
85 |
-
field: (d) =>
|
|
|
|
|
86 |
},
|
87 |
-
// state: {
|
88 |
-
// highlight: {
|
89 |
-
// fill: '#D580FF',
|
90 |
-
// halo: true,
|
91 |
-
// lineWidth: 0,
|
92 |
-
// },
|
93 |
-
// dim: {
|
94 |
-
// fill: '#99ADD1',
|
95 |
-
// },
|
96 |
-
// },
|
97 |
},
|
98 |
edge: {
|
99 |
style: (model) => {
|
100 |
-
const
|
|
|
101 |
return {
|
102 |
-
stroke:
|
103 |
-
lineWidth:
|
104 |
};
|
105 |
},
|
106 |
},
|
107 |
});
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
graph.setData(nextData);
|
110 |
|
111 |
graph.render();
|
@@ -117,7 +118,13 @@ const ForceGraph = () => {
|
|
117 |
}
|
118 |
}, [data, render]);
|
119 |
|
120 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
};
|
122 |
|
123 |
export default ForceGraph;
|
|
|
1 |
+
import { useFetchKnowledgeGraph } from '@/hooks/chunk-hooks';
|
2 |
+
import { ElementDatum, Graph, IElementEvent } from '@antv/g6';
|
3 |
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
4 |
+
import { buildNodesAndCombos, isDataExist } from './util';
|
|
|
5 |
|
|
|
6 |
import styles from './index.less';
|
7 |
|
8 |
+
const TooltipColorMap = {
|
9 |
+
combo: 'red',
|
10 |
+
node: 'black',
|
11 |
+
edge: 'blue',
|
12 |
+
};
|
|
|
|
|
|
|
|
|
13 |
|
14 |
const ForceGraph = () => {
|
15 |
const containerRef = useRef<HTMLDivElement>(null);
|
16 |
+
const graphRef = useRef<Graph | null>(null);
|
17 |
const { data } = useFetchKnowledgeGraph();
|
18 |
|
19 |
const nextData = useMemo(() => {
|
|
|
24 |
}
|
25 |
return { nodes: [], edges: [] };
|
26 |
}, [data]);
|
|
|
27 |
|
28 |
const render = useCallback(() => {
|
29 |
const graph = new Graph({
|
30 |
container: containerRef.current!,
|
31 |
autoFit: 'view',
|
32 |
+
autoResize: true,
|
33 |
behaviors: [
|
34 |
'drag-element',
|
35 |
'drag-canvas',
|
|
|
43 |
plugins: [
|
44 |
{
|
45 |
type: 'tooltip',
|
46 |
+
enterable: true,
|
47 |
+
getContent: (e: IElementEvent, items: ElementDatum) => {
|
48 |
+
if (Array.isArray(items)) {
|
49 |
let result = ``;
|
50 |
items.forEach((item) => {
|
51 |
+
result += `<section style="color:${TooltipColorMap[e['targetType'] as keyof typeof TooltipColorMap]};"><h3>${item?.id}</h3>`;
|
52 |
+
if (item?.entity_type) {
|
53 |
+
result += `<div style="padding-bottom: 6px;"><b>Entity type: </b>${item?.entity_type}</div>`;
|
54 |
+
}
|
55 |
+
if (item?.weight) {
|
56 |
+
result += `<div><b>Weight: </b>${item?.weight}</div>`;
|
57 |
+
}
|
58 |
if (item?.description) {
|
59 |
result += `<p>${item?.description}</p>`;
|
60 |
}
|
61 |
});
|
62 |
+
return result + '</section>';
|
63 |
}
|
64 |
return undefined;
|
65 |
},
|
|
|
69 |
type: 'combo-combined',
|
70 |
preventOverlap: true,
|
71 |
comboPadding: 1,
|
72 |
+
spacing: 100,
|
73 |
},
|
74 |
node: {
|
75 |
style: {
|
76 |
+
size: 150,
|
77 |
labelText: (d) => d.id,
|
78 |
+
// labelPadding: 30,
|
79 |
+
labelFontSize: 40,
|
80 |
// labelOffsetX: 20,
|
81 |
+
labelOffsetY: 20,
|
82 |
labelPlacement: 'center',
|
83 |
labelWordWrap: true,
|
84 |
},
|
85 |
palette: {
|
86 |
type: 'group',
|
87 |
+
field: (d) => {
|
88 |
+
return d?.entity_type as string;
|
89 |
+
},
|
90 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
},
|
92 |
edge: {
|
93 |
style: (model) => {
|
94 |
+
const weight: number = Number(model?.weight) || 2;
|
95 |
+
const lineWeight = weight * 4;
|
96 |
return {
|
97 |
+
stroke: '#99ADD1',
|
98 |
+
lineWidth: lineWeight > 10 ? 10 : lineWeight,
|
99 |
};
|
100 |
},
|
101 |
},
|
102 |
});
|
103 |
|
104 |
+
if (graphRef.current) {
|
105 |
+
graphRef.current.destroy();
|
106 |
+
}
|
107 |
+
|
108 |
+
graphRef.current = graph;
|
109 |
+
|
110 |
graph.setData(nextData);
|
111 |
|
112 |
graph.render();
|
|
|
118 |
}
|
119 |
}, [data, render]);
|
120 |
|
121 |
+
return (
|
122 |
+
<div
|
123 |
+
ref={containerRef}
|
124 |
+
className={styles.forceContainer}
|
125 |
+
style={{ width: '100%', height: '80vh' }}
|
126 |
+
/>
|
127 |
+
);
|
128 |
};
|
129 |
|
130 |
export default ForceGraph;
|
web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/index.less
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
.forceContainer {
|
2 |
-
|
3 |
-
|
|
|
4 |
}
|
5 |
|
6 |
.modalContainer {
|
7 |
-
width:
|
8 |
-
height:
|
9 |
}
|
|
|
1 |
.forceContainer {
|
2 |
+
:global(.tooltip) {
|
3 |
+
border-radius: 10px !important;
|
4 |
+
}
|
5 |
}
|
6 |
|
7 |
.modalContainer {
|
8 |
+
width: 100%;
|
9 |
+
height: 100%;
|
10 |
}
|
web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/modal.tsx
CHANGED
@@ -4,6 +4,7 @@ import React, { useEffect, useState } from 'react';
|
|
4 |
import ForceGraph from './force-graph';
|
5 |
|
6 |
import styles from './index.less';
|
|
|
7 |
|
8 |
const KnowledgeGraphModal: React.FC = () => {
|
9 |
const [isModalOpen, setIsModalOpen] = useState(false);
|
@@ -18,8 +19,7 @@ const KnowledgeGraphModal: React.FC = () => {
|
|
18 |
};
|
19 |
|
20 |
useEffect(() => {
|
21 |
-
if (data
|
22 |
-
console.log('π ~ useEffect ~ data:', data);
|
23 |
setIsModalOpen(true);
|
24 |
}
|
25 |
}, [setIsModalOpen, data]);
|
|
|
4 |
import ForceGraph from './force-graph';
|
5 |
|
6 |
import styles from './index.less';
|
7 |
+
import { isDataExist } from './util';
|
8 |
|
9 |
const KnowledgeGraphModal: React.FC = () => {
|
10 |
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
|
19 |
};
|
20 |
|
21 |
useEffect(() => {
|
22 |
+
if (isDataExist(data)) {
|
|
|
23 |
setIsModalOpen(true);
|
24 |
}
|
25 |
}, [setIsModalOpen, data]);
|
web/src/pages/add-knowledge/components/knowledge-chunk/components/knowledge-graph/util.ts
CHANGED
@@ -65,9 +65,6 @@ export const buildNodesAndCombos = (nodes: any[]) => {
|
|
65 |
if (combo && combos.every((y) => y.id !== combo)) {
|
66 |
combos.push({
|
67 |
id: combo,
|
68 |
-
data: {
|
69 |
-
label: `Combo ${combo}`,
|
70 |
-
},
|
71 |
});
|
72 |
}
|
73 |
return {
|
|
|
65 |
if (combo && combos.every((y) => y.id !== combo)) {
|
66 |
combos.push({
|
67 |
id: combo,
|
|
|
|
|
|
|
68 |
});
|
69 |
}
|
70 |
return {
|