@agilehub/vue-knowledge-graph 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +315 -3
- package/dist/components/GraphToolbar.vue.d.ts +21 -0
- package/dist/components/GraphVisualization.vue.d.ts +26 -0
- package/dist/index.d.ts +14 -0
- package/dist/module/g6Graph.d.ts +60 -0
- package/dist/types/index.d.ts +54 -0
- package/dist/vue-knowledge-graph.cjs +4 -0
- package/dist/vue-knowledge-graph.cjs.map +1 -0
- package/dist/vue-knowledge-graph.mjs +517 -0
- package/dist/vue-knowledge-graph.mjs.map +1 -0
- package/package.json +80 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 lytdev
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,315 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @agilehub/vue-knowledge-graph
|
|
2
|
+
|
|
3
|
+
Vue 3 知识图谱可视化组件,基于 AntV G6。支持力导向、环形、同心圆和层级树形布局,以及节点聚焦和交互式图谱调试页。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @agilehub/vue-knowledge-graph @antv/g6 vue
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 依赖
|
|
12
|
+
|
|
13
|
+
- Vue 3.2+
|
|
14
|
+
- @antv/g6 ^5.1.1
|
|
15
|
+
|
|
16
|
+
这两个包是 peer dependencies,使用本库的项目需要自行安装。
|
|
17
|
+
|
|
18
|
+
## 快速开始
|
|
19
|
+
|
|
20
|
+
```vue
|
|
21
|
+
<template>
|
|
22
|
+
<div style="width: 100%; height: 600px;">
|
|
23
|
+
<GraphVisualization
|
|
24
|
+
:nodes="nodes"
|
|
25
|
+
:edges="edges"
|
|
26
|
+
/>
|
|
27
|
+
</div>
|
|
28
|
+
</template>
|
|
29
|
+
|
|
30
|
+
<script setup lang="ts">
|
|
31
|
+
import { ref } from 'vue'
|
|
32
|
+
import { GraphVisualization } from '@agilehub/vue-knowledge-graph'
|
|
33
|
+
|
|
34
|
+
const nodes = ref([
|
|
35
|
+
{ id: '1', label: '人工智能', type: '概念' },
|
|
36
|
+
{ id: '2', label: '机器学习', type: '概念' },
|
|
37
|
+
{ id: '3', label: '深度学习', type: '概念' }
|
|
38
|
+
])
|
|
39
|
+
|
|
40
|
+
const edges = ref([
|
|
41
|
+
{ source: '1', target: '2', label: '包含', relation_type: '所属' },
|
|
42
|
+
{ source: '2', target: '3', label: '属于', relation_type: '所属' }
|
|
43
|
+
])
|
|
44
|
+
</script>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 组件
|
|
48
|
+
|
|
49
|
+
### GraphVisualization
|
|
50
|
+
|
|
51
|
+
知识图谱可视化组件。
|
|
52
|
+
|
|
53
|
+
#### Props
|
|
54
|
+
|
|
55
|
+
| 属性 | 类型 | 默认值 | 说明 |
|
|
56
|
+
|------|------|--------|------|
|
|
57
|
+
| nodes | `GraphNode[]` | 必填 | 节点数据 |
|
|
58
|
+
| edges | `GraphEdge[]` | 必填 | 关系数据,`source` 和 `target` 必须引用已有节点 ID |
|
|
59
|
+
| width | `number` | `0` | 画布宽度,0 表示自适应容器 |
|
|
60
|
+
| height | `number` | `0` | 画布高度,0 表示自适应容器 |
|
|
61
|
+
| layoutType | `LayoutType` | `'force'` | 当前布局,变化时自动切换 |
|
|
62
|
+
|
|
63
|
+
#### Events
|
|
64
|
+
|
|
65
|
+
| 事件名 | 参数 | 说明 |
|
|
66
|
+
|--------|------|------|
|
|
67
|
+
| nodeClick | `GraphNode` | 点击节点时触发,返回被点击的节点数据 |
|
|
68
|
+
|
|
69
|
+
```vue
|
|
70
|
+
<GraphVisualization
|
|
71
|
+
:nodes="nodes"
|
|
72
|
+
:edges="edges"
|
|
73
|
+
@node-click="handleNodeClick"
|
|
74
|
+
/>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import type { GraphNode } from '@agilehub/vue-knowledge-graph'
|
|
79
|
+
|
|
80
|
+
const handleNodeClick = (node: GraphNode) => {
|
|
81
|
+
console.log(node.id)
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
#### 节点数据类型 (GraphNode)
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
interface GraphNode {
|
|
89
|
+
id: string // 唯一标识
|
|
90
|
+
label: string // 显示文本
|
|
91
|
+
type?: string // 节点类型(用于配色)
|
|
92
|
+
size?: number // 节点大小
|
|
93
|
+
style?: {
|
|
94
|
+
fill?: string // 填充色
|
|
95
|
+
stroke?: string // 边框色
|
|
96
|
+
lineWidth?: number // 边框宽度
|
|
97
|
+
}
|
|
98
|
+
x?: number // 初始 X 坐标
|
|
99
|
+
y?: number // 初始 Y 坐标
|
|
100
|
+
[key: string]: unknown // 自定义属性,值可以是字符串、数字、对象等
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
自定义属性会保留在 `nodeClick` 事件参数和 G6 节点 `data` 中。需要强类型访问时可以扩展 `GraphNode`:
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import type { GraphNode } from '@agilehub/vue-knowledge-graph'
|
|
108
|
+
|
|
109
|
+
interface ArticleNode extends GraphNode {
|
|
110
|
+
score: number
|
|
111
|
+
bookCode: string
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const nodes: ArticleNode[] = [
|
|
115
|
+
{
|
|
116
|
+
id: '1',
|
|
117
|
+
label: '金融资产',
|
|
118
|
+
type: '概念',
|
|
119
|
+
score: 98,
|
|
120
|
+
bookCode: 'b087892de9ef4108ae952267adf8e0db',
|
|
121
|
+
},
|
|
122
|
+
]
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### 边数据类型 (GraphEdge)
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
interface GraphEdge {
|
|
129
|
+
id?: string // 可选;未提供时组件自动生成
|
|
130
|
+
source: string // 源节点 ID
|
|
131
|
+
target: string // 目标节点 ID
|
|
132
|
+
label?: string // 关系标签
|
|
133
|
+
relation_type?: string // 关系类型(用于配色)
|
|
134
|
+
style?: {
|
|
135
|
+
stroke?: string // 线条颜色
|
|
136
|
+
lineWidth?: number // 线条宽度
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
#### 公开方法
|
|
142
|
+
|
|
143
|
+
`GraphVisualization` 通过模板 ref 暴露 `fitView()`、`resetLayout()`、`switchLayout(type)`、`getLayoutType()` 和 `focusNode(nodeId)`。`type` 使用导出的 `LayoutType`,例如 `graphRef.value?.switchLayout('mindmap')`。组件挂载后再调用这些方法。
|
|
144
|
+
|
|
145
|
+
### GraphToolbar
|
|
146
|
+
|
|
147
|
+
图谱工具栏组件,提供布局切换等功能。
|
|
148
|
+
|
|
149
|
+
#### Props
|
|
150
|
+
|
|
151
|
+
| 属性 | 类型 | 默认值 | 说明 |
|
|
152
|
+
|------|------|--------|------|
|
|
153
|
+
| layoutType | `LayoutType` | `'force'` | 当前布局类型 |
|
|
154
|
+
|
|
155
|
+
#### Events
|
|
156
|
+
|
|
157
|
+
| 事件名 | 说明 |
|
|
158
|
+
|--------|------|
|
|
159
|
+
| fitView | 点击"适应画布"时触发 |
|
|
160
|
+
| resetLayout | 点击"重置布局"时触发 |
|
|
161
|
+
| switchLayout | 下拉框切换布局时触发,参数为 `LayoutType` |
|
|
162
|
+
| switchToForce | 兼容事件;切换到力导向时触发 |
|
|
163
|
+
| switchToCircular | 兼容事件;切换到环形时触发 |
|
|
164
|
+
|
|
165
|
+
#### 使用示例
|
|
166
|
+
|
|
167
|
+
```vue
|
|
168
|
+
<template>
|
|
169
|
+
<div style="width: 100%; height: 600px; position: relative;">
|
|
170
|
+
<GraphVisualization
|
|
171
|
+
ref="graphRef"
|
|
172
|
+
:nodes="nodes"
|
|
173
|
+
:edges="edges"
|
|
174
|
+
:layout-type="layoutType"
|
|
175
|
+
/>
|
|
176
|
+
<GraphToolbar
|
|
177
|
+
:layout-type="layoutType"
|
|
178
|
+
@fit-view="handleFitView"
|
|
179
|
+
@reset-layout="handleResetLayout"
|
|
180
|
+
@switch-layout="handleSwitchLayout"
|
|
181
|
+
/>
|
|
182
|
+
</div>
|
|
183
|
+
</template>
|
|
184
|
+
|
|
185
|
+
<script setup lang="ts">
|
|
186
|
+
import { ref } from 'vue'
|
|
187
|
+
import { GraphVisualization, GraphToolbar, type LayoutType } from '@agilehub/vue-knowledge-graph'
|
|
188
|
+
|
|
189
|
+
const nodes = ref([
|
|
190
|
+
{ id: '1', label: '人工智能', type: '概念' },
|
|
191
|
+
{ id: '2', label: '机器学习', type: '概念' }
|
|
192
|
+
])
|
|
193
|
+
const edges = ref([
|
|
194
|
+
{ source: '1', target: '2', label: '包含', relation_type: '所属' }
|
|
195
|
+
])
|
|
196
|
+
const graphRef = ref<InstanceType<typeof GraphVisualization> | null>(null)
|
|
197
|
+
const layoutType = ref<LayoutType>('force')
|
|
198
|
+
|
|
199
|
+
const handleFitView = () => graphRef.value?.fitView()
|
|
200
|
+
const handleResetLayout = () => graphRef.value?.resetLayout()
|
|
201
|
+
const handleSwitchLayout = (type: LayoutType) => {
|
|
202
|
+
layoutType.value = type
|
|
203
|
+
}
|
|
204
|
+
</script>
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## 交互功能
|
|
208
|
+
|
|
209
|
+
- **缩放画布**: 鼠标滚轮
|
|
210
|
+
- **拖拽节点**: 拖动节点可移动位置,相邻节点会跟随移动
|
|
211
|
+
- **拖动画布**: 在画布空白区域按住鼠标左键拖动
|
|
212
|
+
- **悬停高亮**: 鼠标悬停在节点上时,相关节点和边高亮,其他节点变暗
|
|
213
|
+
- **点击选中**: 点击节点选中并高亮相关节点,再次点击取消
|
|
214
|
+
- **点击空白**: 点击空白处取消所有高亮
|
|
215
|
+
|
|
216
|
+
## 布局说明
|
|
217
|
+
|
|
218
|
+
组件不限定数据语义,所有布局都消费相同的 `nodes + edges`。`GRAPH_LAYOUT_OPTIONS` 导出完整布局清单,可用于自定义选择器。
|
|
219
|
+
|
|
220
|
+
- **网络布局**: `force`、`circular`、`concentric`
|
|
221
|
+
- **层级布局**: `antv-dagre`、`indented`、`mindmap`
|
|
222
|
+
|
|
223
|
+
树形布局更适合每个子节点只有一个父节点的数据;普通网络或存在多父节点、环路的图谱优先使用力导向、环形或同心圆布局。
|
|
224
|
+
|
|
225
|
+
## 节点/边颜色
|
|
226
|
+
|
|
227
|
+
根据类型自动配色:
|
|
228
|
+
|
|
229
|
+
### 节点类型颜色
|
|
230
|
+
|
|
231
|
+
| 类型 | 颜色 |
|
|
232
|
+
|------|------|
|
|
233
|
+
| 人物 | #ff4d4f |
|
|
234
|
+
| 地点 | #52c41a |
|
|
235
|
+
| 组织 | #1890ff |
|
|
236
|
+
| 概念 | #faad14 |
|
|
237
|
+
| 事件 | #722ed1 |
|
|
238
|
+
| 其他 | #13c2c2 |
|
|
239
|
+
|
|
240
|
+
### 关系类型颜色
|
|
241
|
+
|
|
242
|
+
| 类型 | 颜色 |
|
|
243
|
+
|------|------|
|
|
244
|
+
| 因果 | #e74c3c |
|
|
245
|
+
| 所属 | #2ecc71 |
|
|
246
|
+
| 引用 | #3498db |
|
|
247
|
+
| 相关 | #9b59b6 |
|
|
248
|
+
|
|
249
|
+
## 作为 Vue 插件使用
|
|
250
|
+
|
|
251
|
+
```js
|
|
252
|
+
import { createApp } from 'vue'
|
|
253
|
+
import KnowledgeGraph from '@agilehub/vue-knowledge-graph'
|
|
254
|
+
import App from './App.vue'
|
|
255
|
+
|
|
256
|
+
const app = createApp(App)
|
|
257
|
+
app.use(KnowledgeGraph)
|
|
258
|
+
app.mount('#app')
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
然后在组件中直接使用:
|
|
262
|
+
|
|
263
|
+
```vue
|
|
264
|
+
<template>
|
|
265
|
+
<GraphVisualization :nodes="nodes" :edges="edges" />
|
|
266
|
+
</template>
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## 本地开发
|
|
270
|
+
|
|
271
|
+
开发调试页使用 Vite,建议 Node.js 20.19+,并使用仓库锁文件对应的 pnpm。
|
|
272
|
+
|
|
273
|
+
```bash
|
|
274
|
+
pnpm install
|
|
275
|
+
pnpm dev
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
打开终端输出的本地地址(通常是 `http://localhost:5173/`)。调试页直接引用 `src/index.ts`,修改组件源码后可通过热更新查看效果。
|
|
279
|
+
|
|
280
|
+
### 调试页操作
|
|
281
|
+
|
|
282
|
+
1. **加载数据**:选择“知识网络”“人物关系”或“空画布”。
|
|
283
|
+
2. **添加节点和第一条关系**:填写节点名称、类型;在“连接到现有节点”中选择目标,填写关系名称和类型,再选择箭头方向,点击“添加节点”。选择“不建立关系”可创建孤立节点。节点 ID 由调试页自动生成,格式为 `demo-1`、`demo-2` 等。
|
|
284
|
+
3. **为节点添加更多关系**:在“当前节点”中选起点,在“为已有节点添加关系”中选目标、关系名称和类型,点击“添加关系”。关系从当前节点指向目标节点,可重复操作。删除节点会同时删除与它相连的关系。
|
|
285
|
+
4. **查看图谱行为**:可聚焦节点,并通过图谱工具栏适应画布、重置布局或切换力导向与环形布局。画布支持滚轮缩放、拖动节点和在空白区域按住鼠标左键拖动画布。
|
|
286
|
+
5. **调试自己的数据**:在 `GraphData JSON` 编辑器中粘贴 `nodes` 和 `edges`,点击“应用 JSON”。编辑器会检查 JSON 格式、节点 ID 是否重复,以及关系端点是否存在;校验失败时保留当前图谱。
|
|
287
|
+
|
|
288
|
+
例如,可粘贴下面的数据,验证节点和关系的渲染:
|
|
289
|
+
|
|
290
|
+
```json
|
|
291
|
+
{
|
|
292
|
+
"nodes": [
|
|
293
|
+
{ "id": "a", "label": "知识工程", "type": "概念" },
|
|
294
|
+
{ "id": "b", "label": "知识图谱", "type": "概念" }
|
|
295
|
+
],
|
|
296
|
+
"edges": [
|
|
297
|
+
{ "source": "a", "target": "b", "label": "应用于", "relation_type": "相关" }
|
|
298
|
+
]
|
|
299
|
+
}
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
修改完成后运行:
|
|
303
|
+
|
|
304
|
+
```bash
|
|
305
|
+
pnpm check
|
|
306
|
+
pnpm build
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
## License
|
|
310
|
+
|
|
311
|
+
[Apache-2.0](./LICENSE)
|
|
312
|
+
|
|
313
|
+
## 作者
|
|
314
|
+
|
|
315
|
+
lytdev
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { LayoutType } from '../module/g6Graph';
|
|
2
|
+
interface Props {
|
|
3
|
+
layoutType?: LayoutType;
|
|
4
|
+
}
|
|
5
|
+
declare const __VLS_export: import('vue').DefineComponent<Props, {}, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {
|
|
6
|
+
switchLayout: (type: LayoutType) => any;
|
|
7
|
+
fitView: () => any;
|
|
8
|
+
resetLayout: () => any;
|
|
9
|
+
switchToForce: () => any;
|
|
10
|
+
switchToCircular: () => any;
|
|
11
|
+
}, string, import('vue').PublicProps, Readonly<Props> & Readonly<{
|
|
12
|
+
onSwitchLayout?: ((type: LayoutType) => any) | undefined;
|
|
13
|
+
onFitView?: (() => any) | undefined;
|
|
14
|
+
onResetLayout?: (() => any) | undefined;
|
|
15
|
+
onSwitchToForce?: (() => any) | undefined;
|
|
16
|
+
onSwitchToCircular?: (() => any) | undefined;
|
|
17
|
+
}>, {
|
|
18
|
+
layoutType: LayoutType;
|
|
19
|
+
}, {}, {}, {}, string, import('vue').ComponentProvideOptions, false, {}, any>;
|
|
20
|
+
declare const _default: typeof __VLS_export;
|
|
21
|
+
export default _default;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { GraphNode, GraphEdge } from '../types';
|
|
2
|
+
import { LayoutType } from '../module/g6Graph';
|
|
3
|
+
interface Props {
|
|
4
|
+
nodes: GraphNode[];
|
|
5
|
+
edges: GraphEdge[];
|
|
6
|
+
width?: number;
|
|
7
|
+
height?: number;
|
|
8
|
+
layoutType?: LayoutType;
|
|
9
|
+
}
|
|
10
|
+
declare const __VLS_export: import('vue').DefineComponent<Props, {
|
|
11
|
+
fitView: () => void;
|
|
12
|
+
resetLayout: () => void;
|
|
13
|
+
switchLayout: (type: LayoutType) => void;
|
|
14
|
+
getLayoutType: () => LayoutType;
|
|
15
|
+
focusNode: (nodeId: string) => void;
|
|
16
|
+
}, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {
|
|
17
|
+
nodeClick: (node: GraphNode) => any;
|
|
18
|
+
}, string, import('vue').PublicProps, Readonly<Props> & Readonly<{
|
|
19
|
+
onNodeClick?: ((node: GraphNode) => any) | undefined;
|
|
20
|
+
}>, {
|
|
21
|
+
width: number;
|
|
22
|
+
height: number;
|
|
23
|
+
layoutType: LayoutType;
|
|
24
|
+
}, {}, {}, {}, string, import('vue').ComponentProvideOptions, false, {}, any>;
|
|
25
|
+
declare const _default: typeof __VLS_export;
|
|
26
|
+
export default _default;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { App } from 'vue';
|
|
2
|
+
import { default as GraphVisualization } from './components/GraphVisualization.vue';
|
|
3
|
+
import { default as GraphToolbar } from './components/GraphToolbar.vue';
|
|
4
|
+
import { GRAPH_LAYOUT_OPTIONS, KnowledgeGraph } from './module/g6Graph';
|
|
5
|
+
export type { GraphNode, GraphEdge } from './types';
|
|
6
|
+
export type { GraphInitOptions } from './module/g6Graph';
|
|
7
|
+
export type { GraphLayoutCategory, GraphLayoutOption, LayoutType, } from './module/g6Graph';
|
|
8
|
+
declare const install: (app: App) => void;
|
|
9
|
+
export { install, GRAPH_LAYOUT_OPTIONS, GraphVisualization, GraphToolbar, KnowledgeGraph, };
|
|
10
|
+
declare const _default: {
|
|
11
|
+
install: (app: App) => void;
|
|
12
|
+
version: string;
|
|
13
|
+
};
|
|
14
|
+
export default _default;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Graph, GraphOptions } from '@antv/g6';
|
|
2
|
+
import { GraphEdge, GraphNode } from '../types';
|
|
3
|
+
export type LayoutType = 'force' | 'circular' | 'concentric' | 'antv-dagre' | 'indented' | 'mindmap';
|
|
4
|
+
export type GraphLayoutCategory = 'network' | 'hierarchy';
|
|
5
|
+
export interface GraphLayoutOption {
|
|
6
|
+
value: LayoutType;
|
|
7
|
+
label: string;
|
|
8
|
+
description: string;
|
|
9
|
+
category: GraphLayoutCategory;
|
|
10
|
+
}
|
|
11
|
+
export declare const GRAPH_LAYOUT_OPTIONS: readonly GraphLayoutOption[];
|
|
12
|
+
export interface GraphInitOptions {
|
|
13
|
+
container: HTMLElement;
|
|
14
|
+
width?: number;
|
|
15
|
+
height?: number;
|
|
16
|
+
layoutType?: LayoutType;
|
|
17
|
+
onNodeClick?: (nodeId: string) => void;
|
|
18
|
+
}
|
|
19
|
+
/** 负责 G6 实例的生命周期和通用视口操作。 */
|
|
20
|
+
export declare class BaseG6Graph {
|
|
21
|
+
protected graph: Graph | null;
|
|
22
|
+
protected container: HTMLElement | null;
|
|
23
|
+
private resizeHandler;
|
|
24
|
+
protected createGraph(config: Omit<GraphOptions, 'container' | 'width' | 'height'>, options: GraphInitOptions): void;
|
|
25
|
+
private setupResizeHandler;
|
|
26
|
+
private cleanupResizeHandler;
|
|
27
|
+
protected run(action: Promise<unknown>): void;
|
|
28
|
+
hasGraph(): boolean;
|
|
29
|
+
resize(width: number, height: number): void;
|
|
30
|
+
fitView(_padding?: number): void;
|
|
31
|
+
destroy(): void;
|
|
32
|
+
}
|
|
33
|
+
/** 知识图谱实现:实体/关系配色、布局切换和图谱交互。 */
|
|
34
|
+
export declare class KnowledgeGraph extends BaseG6Graph {
|
|
35
|
+
private currentLayoutType;
|
|
36
|
+
private nodeClickHandler;
|
|
37
|
+
private canvasElement;
|
|
38
|
+
private canvasPointerDownHandler;
|
|
39
|
+
private canvasPointerMoveHandler;
|
|
40
|
+
private canvasPointerUpHandler;
|
|
41
|
+
private canvasPointerId;
|
|
42
|
+
private lastCanvasPoint;
|
|
43
|
+
private isCanvasDragging;
|
|
44
|
+
private canvasDefaultCursor;
|
|
45
|
+
private getLayoutConfig;
|
|
46
|
+
private getColorByType;
|
|
47
|
+
private getColorByRelation;
|
|
48
|
+
init(options: GraphInitOptions, nodes: GraphNode[], edges: GraphEdge[]): void;
|
|
49
|
+
update(nodes: GraphNode[], edges: GraphEdge[]): void;
|
|
50
|
+
resetLayout(): void;
|
|
51
|
+
switchLayout(type: LayoutType): void;
|
|
52
|
+
getLayoutType(): LayoutType;
|
|
53
|
+
focusNode(nodeId: string): void;
|
|
54
|
+
private getCurrentLayout;
|
|
55
|
+
private processData;
|
|
56
|
+
private scheduleFitView;
|
|
57
|
+
private setupEvents;
|
|
58
|
+
private setupCanvasDrag;
|
|
59
|
+
destroy(): void;
|
|
60
|
+
}
|