KnowledgeGraphView.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <script setup lang="ts">
  2. import { computed, onMounted, ref, type Ref } from "vue";
  3. import G6, { type Graph, type Item, type NodeConfig } from '@antv/g6';
  4. import type { SearchResult } from "@/types/search.types";
  5. import type { Request } from "@/types/knowledgeGraph.types";
  6. import SearchResultList from "../search/components/SearchResultList.vue";
  7. import PropertyComponent from "./components/PropertyComponent.vue";
  8. import * as knowledgeGraphService from "@/services/knowledgeGraph.service"
  9. import * as searchService from "@/services/search.service"
  10. import { GraphDataManager } from "@/models/knowledgeGraph.model";
  11. import { message } from "ant-design-vue";
  12. import SpinComponent from "@/components/SpinComponent.vue";
  13. // 搜索结果
  14. const searchResult: Ref<SearchResult | null> = ref(null);
  15. const currentPage = ref(1)
  16. const searchLoading = ref(false)
  17. const searchNodeName = ref<string | null>(null)
  18. // 节点搜索统计
  19. const propertyData = computed(() => {
  20. return {
  21. name: searchNodeName.value || '',
  22. docCount: searchResult.value?.total || 0,
  23. keywords: searchResult.value?.aggs["KW"] || [],
  24. }
  25. })
  26. const keyword = ref('建筑工程 工程设计');
  27. let graph: Graph | null = null;
  28. const graphDataManager = new GraphDataManager();
  29. function find(request: Request) {
  30. const msgHandler = message.loading("正在查找关联节点...", 60)
  31. knowledgeGraphService.find(request).then((nodes) => {
  32. graphDataManager.addSubjectNodes(nodes);
  33. if (graph && nodes.length > 0) {
  34. graph.read(graphDataManager.getGraphData())
  35. }
  36. msgHandler()
  37. }).catch(() => {
  38. msgHandler()
  39. })
  40. }
  41. function findParents(id: number) {
  42. find({name: '', id, size: 10, findParents: true, findChildren: false})
  43. }
  44. function findChildren(id: number) {
  45. find({name: '', id, size: 10, findParents: false, findChildren: true})
  46. }
  47. function findName(name: string) {
  48. find({name, size: 10, findParents: false, findChildren: true})
  49. }
  50. function searchSubject(name: string) {
  51. searchLoading.value = true
  52. const from = Math.max(currentPage.value - 1, 0) * 10
  53. const request = {
  54. "query": `TP=(${name})`,
  55. from,
  56. size: 10,
  57. aggs: {
  58. "KW": {
  59. "sort": "count/desc"
  60. }
  61. }
  62. }
  63. searchService.search(request).then((resp) => {
  64. searchResult.value = resp;
  65. searchLoading.value = false
  66. }).catch(() => {
  67. message.warning('搜索学科分类遇到问题,请稍后再试');
  68. searchLoading.value = false
  69. })
  70. }
  71. function onPaginationChange(p: number) {
  72. currentPage.value = p
  73. if ( searchNodeName.value) {
  74. searchSubject(searchNodeName.value)
  75. }
  76. }
  77. const menu = new G6.Menu({
  78. offsetX: 6,
  79. offsetY: 10,
  80. itemTypes: ['node'],
  81. getContent() {
  82. const outDiv = document.createElement('div');
  83. outDiv.style.width = '100px';
  84. outDiv.innerHTML = `<ul class='graph-context-menu'>
  85. <li name='parent' class='graph-context-menu-item'>上级学科</li>
  86. <li name='child' class='graph-context-menu-item'>下级学科</li>
  87. <li name='search' class='graph-context-menu-item'>搜索</li>
  88. </ul>`
  89. return outDiv
  90. },
  91. handleMenuClick(target: HTMLElement, item: Item) {
  92. const menuName = target.getAttribute("name")
  93. const model = item.getModel() as NodeConfig
  94. if (model.label) {
  95. if (menuName == 'parent') {
  96. if (model.nodeId) findParents(model.nodeId as number)
  97. } else if (menuName == 'child') {
  98. if (model.nodeId) findChildren(model.nodeId as number)
  99. } else if (menuName == 'search') {
  100. searchNodeName.value = model.label as string
  101. searchSubject(searchNodeName.value)
  102. }
  103. }
  104. },
  105. });
  106. function initGraph(width: number, height: number) {
  107. const graph = new G6.Graph({
  108. container: 'graph',
  109. width: width,
  110. height: height,
  111. layout: {
  112. type: 'force',
  113. preventOverlap: true,
  114. linkDistance: 150,
  115. nodeStrength: 150,
  116. nodeSize: 80,
  117. },
  118. modes: {
  119. // 允许拖拽画布、放缩画布、拖拽节点
  120. default: ['drag-canvas', 'zoom-canvas', 'drag-node', 'click-select'],
  121. },
  122. nodeStateStyles: {
  123. selected: {
  124. stroke: '#00FA9A',
  125. fill: '#00FA9A',
  126. },
  127. },
  128. defaultNode: {
  129. size: 64,
  130. type: 'circle',
  131. style: {
  132. stroke: '#90EE90',
  133. fill: '#90EE90',
  134. },
  135. // 选中颜色为 #FBEECD
  136. labelCfg: {
  137. position: 'center',
  138. style: {
  139. fontSize: 11,
  140. },
  141. },
  142. },
  143. defaultEdge: {
  144. label: '下级学科',
  145. size: 1,
  146. color: '#B6BBC5',
  147. style: {
  148. endArrow: {
  149. path: 'M 0,0 L 8,4 L 8,-4 Z',
  150. fill: '#B6BBC5',
  151. },
  152. },
  153. },
  154. plugins: [menu],
  155. });
  156. // // 读取数据源到图上
  157. // graph.data(data as any);
  158. // // 渲染图
  159. // graph.render();
  160. // 监听节点点击事件
  161. // graph.on('node:click', event => {
  162. // const nodes = data['nodes']
  163. // const index = nodes.findIndex(n => n.id == event.item?.getID())
  164. // if (index > -1) {
  165. // const node = nodes[index];
  166. // searchSubject(node.label);
  167. // kgGraphStore.search(node.label);
  168. // }
  169. // });
  170. return graph;
  171. }
  172. function onSearch() {
  173. graphDataManager.pruge();
  174. findName(keyword.value);
  175. }
  176. onMounted(() => {
  177. const rect = document.getElementById("graph")?.getBoundingClientRect()
  178. if (rect) {
  179. graph = initGraph(rect.width, rect.height)
  180. findName(keyword.value)
  181. }
  182. });
  183. </script>
  184. <template>
  185. <div class="search-box">
  186. <span class="search-box-subjectName">学科名称</span>
  187. <a-input-search
  188. v-model:value="keyword"
  189. placeholder="请输入学科名称"
  190. enter-button
  191. size="large"
  192. @search="onSearch"
  193. />
  194. </div>
  195. <a-row :gutter="16" type="flex" class="graph-container">
  196. <a-col :span="18">
  197. <div class="graph-wrap">
  198. <div id="graph" class="graph"></div>
  199. </div>
  200. </a-col>
  201. <a-col :span="6">
  202. <div class="property-wrap">
  203. <PropertyComponent :data="propertyData" />
  204. </div>
  205. </a-col>
  206. </a-row>
  207. <div class="articles-wrap">
  208. <SpinComponent :spinning="searchLoading">
  209. <div v-if="searchResult">
  210. <SearchResultList :data="searchResult" class="articles" />
  211. <a-pagination
  212. v-model:current="currentPage"
  213. :total="searchResult.total"
  214. :show-total="(total: number) => `共 ${total} 条`"
  215. :show-size-changer="false"
  216. class="pagination"
  217. @change="onPaginationChange"
  218. />
  219. </div>
  220. <a-empty class="empty" v-else description="暂无数据,请点击学科导航节点" />
  221. </SpinComponent>
  222. </div>
  223. </template>
  224. <style scoped lang="scss">
  225. @import "@/assets/variables.scss";
  226. .search-box {
  227. width: 65%;
  228. margin: 0 auto;
  229. margin-top: 20px;
  230. display: flex;
  231. align-items: center;
  232. .search-box-subjectName {
  233. display: block;
  234. width: 6rem;
  235. }
  236. }
  237. .graph-container {
  238. margin-top: 20px;
  239. .graph-wrap,
  240. .property-wrap {
  241. border: 1px solid $border-color;
  242. background-color: white;
  243. padding: 10px;
  244. }
  245. .graph-wrap {
  246. height: 500px;
  247. .graph {
  248. width: 100%;
  249. height: 100%;
  250. }
  251. }
  252. .property-wrap {
  253. height: 100%;
  254. }
  255. :deep(ul.graph-context-menu) {
  256. list-style: none;
  257. margin: 0;
  258. padding: 0;
  259. li.graph-context-menu-item {
  260. font-size: 1.25em;
  261. padding: 5px 10px;
  262. margin-top: 6px;
  263. cursor: pointer;
  264. &:first-child {
  265. margin-top: 0;
  266. }
  267. &:hover {
  268. background-color: $bg-color-gray-light;
  269. }
  270. }
  271. }
  272. }
  273. .articles-wrap {
  274. margin-top: 20px;
  275. .articles {
  276. padding: 10px 20px;
  277. background-color: white;
  278. border: 1px solid $border-color;
  279. }
  280. .empty {
  281. background-color: white;
  282. border: 1px solid $border-color;
  283. margin: 0;
  284. padding: 30px 0;
  285. }
  286. .pagination {
  287. float: right;
  288. margin-top: 20px;
  289. ::after {
  290. clear: both;
  291. }
  292. }
  293. }
  294. </style>