chart.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. <template>
  2. <div
  3. id="chartContent"
  4. style="position:relative;height:100%"
  5. :style="{ 'min-width': minWid + 'px', 'min-height': minHei + 'px' }"
  6. >
  7. <div
  8. style="display: flex;justify-content: flex-end;height: 30px; margin-top:10px"
  9. >
  10. <a-button @click="switchOPen" v-if="enable" style="display: none;"
  11. >{{ isShow ? '隐藏图标' : '显示图标' }}
  12. </a-button>
  13. <a-button
  14. type="primary"
  15. @click="exportChartData"
  16. style="margin-left: 10px;"
  17. v-if="showExportBtn"
  18. >导出数据到控制台
  19. </a-button>
  20. <a-button
  21. type="default"
  22. @click="resetChart"
  23. v-if="showReset"
  24. style="width: 150px;"
  25. >重置
  26. </a-button>
  27. </div>
  28. <div
  29. :id="domUuid"
  30. style="height: calc(100% - 40px);width: 100%"
  31. v-if="isShowChart"
  32. ></div>
  33. <div
  34. class="flex-center"
  35. :class="isRoad ? 'road_computed_blank' : 'relevance_blank'"
  36. style="height: 100%;width: 100%;"
  37. v-else
  38. ></div>
  39. <a-dropdown
  40. :visible="dropdown.show && isShowContextMenu === true"
  41. :getPopupContainer="getPopupContainer"
  42. :trigger="['click']"
  43. style="position: absolute;z-index: 1"
  44. :style="{ left: dropdown.x, top: dropdown.y }"
  45. >
  46. <a class="ant-dropdown-link" href="#" />
  47. <a-menu slot="overlay">
  48. <a-menu-item
  49. v-if="!contextMenu && clickTarget.type === 'node'"
  50. key="1"
  51. @click="handleRemoveEntCls"
  52. >隐藏选中
  53. </a-menu-item>
  54. <a-menu-item
  55. v-if="contextMenu && contextMenu.indexOf('delete') > -1"
  56. key="2"
  57. @click="contextMenuDelete"
  58. >排除
  59. </a-menu-item>
  60. <!--<a-menu-item v-if="clickTarget.type === 'node'" key="2" @click="movePosition">移动位置</a-menu-item>-->
  61. </a-menu>
  62. </a-dropdown>
  63. </div>
  64. </template>
  65. <script>
  66. import uuid from 'uuid'
  67. import { NetChart } from '@/plugins/knowledge/zoomChart'
  68. // const NetChart = window.NetChart
  69. // const NetChart = null;
  70. const colors = ['rgba(111,82,184,1)',
  71. 'rgba(255,214,24,1)',
  72. 'rgba(47,195,47,1)',
  73. 'rgba(86,185,247,1)',
  74. 'rgba(234,180,4,1)',
  75. 'rgba(222,103,44,1)',
  76. '#FEDD00',
  77. '#00843D',
  78. '#009A44',
  79. '#4A7729',
  80. '#78BE20',
  81. '#007A33',
  82. '#BFB800',
  83. '#64A70B',
  84. '#006341',
  85. '#FDDA24',
  86. '#009639',
  87. '#43B02A',
  88. '#00205B',
  89. '#00629B',
  90. '#64CCC9',
  91. '#0093B2',
  92. '#004F71',
  93. '#006BA6',
  94. '#003DA5',
  95. '#003B5C',
  96. '#008578',
  97. '#0033A0',
  98. '#002855',
  99. '#003087',
  100. '#00778B',
  101. '#0082BA',
  102. '#00A9CE',
  103. '#002D72',
  104. '#001871',
  105. '#672146',
  106. '#AD1AAC',
  107. '#A9431E',
  108. '#D57800',
  109. '#643335',
  110. '#FFCD00',
  111. '#3E2B2E',
  112. '#BE6A14',
  113. '#A50034',
  114. '#F9423A',
  115. '#C8102E',
  116. '#E4002B',
  117. '#C5003E',
  118. '#EF3340',
  119. '#CB333B',
  120. '#FC4C02',
  121. '#FE5000',
  122. '#FF671F',
  123. '#2C2A29',
  124. '#FFA300',
  125. '#FBD872',
  126. '#333F48',
  127. '#C99700',
  128. '#ABCAE9',
  129. '#F4364C',
  130. '#7A7D81',
  131. '#C5299B',
  132. '#DB3EB1',
  133. '#B9975B',
  134. ]
  135. let g_root,
  136. chart,
  137. node_colors = {},
  138. link_colors = {}
  139. let highLightCache = {}
  140. let isShow = true
  141. let _vue
  142. let isDivisionColor = false
  143. export default {
  144. props: {
  145. nodes: {
  146. type: Array,
  147. default: () => [],
  148. },
  149. links: {
  150. type: Array,
  151. default: () => [],
  152. },
  153. enable: {
  154. type: Boolean,
  155. default: false,
  156. },
  157. isDivisionColor: {
  158. type: Boolean,
  159. default: false,
  160. },
  161. isShowChart: {
  162. type: Boolean,
  163. default: false,
  164. },
  165. isShowContextMenu: {
  166. type: Boolean,
  167. default: false,
  168. },
  169. contextMenu: {
  170. type: Array,
  171. default: () => [],
  172. },
  173. isRoad: {
  174. type: Boolean,
  175. default: false,
  176. },
  177. isShowTree: {
  178. type: Boolean,
  179. default: false,
  180. },
  181. minWid: {
  182. type: Number,
  183. default: null,
  184. },
  185. minHei: {
  186. type: Number,
  187. default: null,
  188. },
  189. showExportBtn: {
  190. type: Boolean,
  191. default: false,
  192. },
  193. showRelKey: {
  194. // 默认关系之间的名称显示,默认显示true:relClsName [true:'relClsName', false:'relName']
  195. type: Boolean,
  196. default: true,
  197. },
  198. isEntChart: {
  199. type: Boolean,
  200. default: false,
  201. },
  202. },
  203. data() {
  204. _vue = this
  205. return {
  206. domUuid: uuid(),
  207. useNavigation: false,
  208. showReset: false,
  209. isShow,
  210. dropdown: {
  211. // 下拉菜单的样式配置
  212. show: false,
  213. x: null,
  214. y: null,
  215. },
  216. chartNodes: null,
  217. chartLinks: null,
  218. clickTarget: {},
  219. //moveid:'', // 要移动的id
  220. selectedEntClsIds: [], // 选中的实体类ids
  221. }
  222. },
  223. watch: {
  224. nodes: function() {
  225. this.chartNodes = this.nodes
  226. },
  227. links: function() {
  228. this.chartLinks = this.links
  229. },
  230. contextMenu: function() {
  231. console.log(this.contextMenu)
  232. },
  233. },
  234. methods: {
  235. getPopupContainer() {
  236. return document.getElementById('chartContent')
  237. },
  238. // 导出图表的数据(json格式)
  239. exportChartData() {
  240. const nodes = chart.nodes()
  241. const links = chart.links()
  242. const json = {
  243. nodes: [],
  244. links: [],
  245. }
  246. for (let i = 0; i < nodes.length; i++) {
  247. const node = nodes[i].data
  248. node.x = nodes[i].x
  249. node.y = nodes[i].y
  250. json.nodes.push(node)
  251. }
  252. for (let l = 0; l < links.length; l++) {
  253. json.links.push(links[l].data)
  254. }
  255. // 这里显示导出的数据,不要删
  256. console.log(JSON.stringify(json))
  257. },
  258. // 切换图标显示和隐藏
  259. switchOPen() {
  260. this.isShow = isShow = !isShow
  261. this.$emit('reloadChart', this.chartNodes, this.chartLinks)
  262. // this.$emit('reloadChart')
  263. },
  264. hidePopup() {
  265. this.dropdown.show = false
  266. },
  267. movePosition() {
  268. chart.reloadData({ nodes: [{ id: this.moveid, x: 300, y: 300 }] })
  269. // chart.replaceData({ nodes: [{id: this.moveid,x: 300, y: 300}] });
  270. },
  271. // 右键菜单删除
  272. contextMenuDelete() {
  273. this.dropdown.show = false
  274. this.$emit('delete', this.selectedEntClsIds)
  275. },
  276. // 批量隐藏 实体类
  277. handleRemoveEntCls() {
  278. this.dropdown.show = false
  279. chart.removeData({ nodes: this.selectedEntClsIds })
  280. this.selectedEntClsIds = [] // 清空上次删除的ids
  281. },
  282. /**
  283. * 高亮时图表的样式
  284. * */
  285. highLightStyle(start, end, nodes) {
  286. let nodeIds = []
  287. nodes.forEach((v) => {
  288. if (nodeIds.indexOf(v.endEntVOS.entID) === -1) {
  289. nodeIds.push(v.endEntVOS.entID)
  290. }
  291. if (nodeIds.indexOf(v.startEntVo.entID) === -1) {
  292. nodeIds.push(v.startEntVo.entID)
  293. }
  294. })
  295. highLightCache.nodeIds = nodeIds
  296. highLightCache.linkIds = computePath(nodes)
  297. chart.updateStyle()
  298. },
  299. /**
  300. * 取消高亮
  301. * */
  302. unhighLightStyle() {
  303. highLightCache = {}
  304. chart.updateStyle()
  305. },
  306. /**
  307. * 重绘
  308. * */
  309. reDraw(root, nodes, links) {
  310. g_root = root
  311. if (chart) {
  312. chart.replaceData({
  313. nodes: getNodes(nodes),
  314. links: getLinks(links),
  315. })
  316. } else {
  317. this.drawChart(root, nodes, links)
  318. }
  319. },
  320. addDraw(root, nodes, links) {
  321. if (chart) {
  322. chart.addData({
  323. nodes: getNodes(nodes),
  324. links: getLinks(links),
  325. })
  326. } else {
  327. this.drawChart(root, nodes, links)
  328. }
  329. },
  330. /*
  331. * 画zoomchart
  332. * */
  333. drawChart(root, nodes, links) {
  334. console.log(root, nodes, links)
  335. const vm = this
  336. g_root = root
  337. highLightCache = {}
  338. chart = new NetChart({
  339. container: document.getElementById(this.domUuid),
  340. area: {},
  341. legend: {
  342. enabled: true,
  343. margin: 20,
  344. },
  345. layout: this.isShowTree
  346. ? {
  347. mode: 'hierarchy',
  348. nodeSpacing: 45,
  349. rowSpacing: 80,
  350. }
  351. : {},
  352. navigation: this.useNavigation
  353. ? {
  354. mode: 'focusnodes',
  355. autoZoomOnFocus: true,
  356. // initialNodes: g_root,
  357. initialNodes: nodes[0].entClsID,
  358. focusNodeExpansionRadius: 1,
  359. numberOfFocusNodes: 2,
  360. focusNodeTailExpansionRadius: 0.5,
  361. }
  362. : {},
  363. style: {
  364. link: { fillColor: 'rgba(0,0,0,0.4)' },
  365. node: {
  366. lineWidth: 2,
  367. lineColor: '#09c',
  368. imageCropping: true,
  369. },
  370. nodeLabel: {
  371. // 节点label的显示样式
  372. padding: 0,
  373. borderRadius: 4,
  374. textStyle: { font: '16px Arial', fillColor: 'black' },
  375. backgroundStyle: { fillColor: 'rgba(255,255,255,0.7)' },
  376. }, // 节点label的显示样式,
  377. linkLabel: {
  378. // 线的label显示样式
  379. padding: 0,
  380. borderRadius: 20, //make as round as possible
  381. textStyle: { font: '20px Arial', fillColor: '#aaa' },
  382. backgroundStyle: {
  383. fillColor: 'rgba(255,255,255,0.6)',
  384. lineColor: 'rgba(0,0,0,0)',
  385. },
  386. rotateWithLink: true,
  387. scaleWithSize: true,
  388. }, // 线的label显示样式
  389. nodeStyleFunction: vm.nodeStyle, // 节点样式与配置的回调
  390. linkStyleFunction: linkStyle, // 关系样式与配置的回调
  391. },
  392. info: {
  393. enabled: true,
  394. nodeContentsFunction: function(data, item) {
  395. let html
  396. if (data.entName) {
  397. // 实体
  398. html =
  399. '<div class="chart_tooltip"><h3>实体名称:' +
  400. data.entName +
  401. '</h3>'
  402. } else if (data.entClsName) {
  403. // 实体类
  404. html =
  405. '<div class="chart_tooltip"><h3>实体类名称:' +
  406. data.entClsName +
  407. '</h3>'
  408. }
  409. if (
  410. data.entName === undefined &&
  411. data.entClsName === undefined
  412. )
  413. html = ''
  414. return html
  415. },
  416. linkContentsFunction: function(data, item) {
  417. let html =
  418. '<div class="chart_tooltip"><h3>关系名称:' +
  419. data.relClsName +
  420. '</h3>'
  421. if (data.relClsName === undefined) html = ''
  422. return html
  423. },
  424. },
  425. events: {
  426. onClick: function(event, args) {
  427. if (
  428. !event.ctrlKey &&
  429. !event.shiftKey &&
  430. args.clickNode &&
  431. vm.useNavigation
  432. ) {
  433. chart.addFocusNode(args.clickNode)
  434. }
  435. vm.dropdown.show = false
  436. vm.$emit('c_click', event, args)
  437. event.preventDefault()
  438. },
  439. onSelectionChange: function(event, args) {
  440. // 选中的实体类事件
  441. vm.selectedEntClsIds = [] // 清空上一次选中
  442. for (let i = 0; i < event.selection.length; i++) {
  443. const item = {}
  444. item.id = event.selection[i].id
  445. item.label = event.selection[i].label
  446. vm.selectedEntClsIds.push(item)
  447. }
  448. },
  449. onDoubleClick: function(event, args) {
  450. // 双击事件
  451. vm.dropdown.show = false
  452. if (args.clickNode) {
  453. vm.$emit('changeRoot', args.clickNode.data.id, 1)
  454. }
  455. event.preventDefault()
  456. },
  457. onRightClick: function(event, args) {
  458. // 右键
  459. // vm.moveid = event.clickNode.id
  460. vm.dropdown.show = true
  461. vm.dropdown.x = event.x + 20 + 'px'
  462. vm.dropdown.y = event.y + 50 + 'px'
  463. if (args.clickNode) {
  464. vm.clickTarget.type = 'node'
  465. // vm.clickTarget.data = args.clickNode.data
  466. this.selectedEntClsIds = [
  467. {
  468. id: event.clickNode.id,
  469. label: event.clickNode.label,
  470. },
  471. ]
  472. } else if (args.clickLink) {
  473. vm.clickTarget.type = 'link'
  474. } else {
  475. vm.hidePopup()
  476. }
  477. event.preventDefault()
  478. },
  479. },
  480. })
  481. document.oncontextmenu = function() {
  482. return false
  483. }
  484. this.reDraw(root, nodes, links)
  485. },
  486. // 重定义变量
  487. reloadData(obj) {
  488. this.useNavigation = obj.useNavigation
  489. this.showReset = obj.showReset
  490. },
  491. resetChart() {
  492. this.$emit('resetChart')
  493. },
  494. /**
  495. * 销毁chart实例
  496. * */
  497. destroyChart() {
  498. chart = undefined
  499. },
  500. nodeStyle(node) {
  501. // 定义节点的样式
  502. const vm = this
  503. let data = node.data.data ? node.data.data : node.data
  504. if (vm.isEntChart) {
  505. node.label = data.label
  506. let r = 90
  507. let g = 150
  508. let b = 220
  509. r -= (data.relNum - 1) * 5
  510. g -= (data.relNum - 1) * 5
  511. b -= (data.relNum - 1) * 5
  512. node.fillColor = 'rgba(' + r + ',' + g + ',' + b + ',1)'
  513. node.radius = 50 + (data.relNum - 1) * 10
  514. } else {
  515. if (data.entClsIcon && isShow) {
  516. node.image = data.entClsIcon
  517. }
  518. if (data.entIcon && isShow) {
  519. node.image = data.entIcon
  520. }
  521. node.label = data.label
  522. if (data.id === g_root) {
  523. if (!node.data.backGroundColor) {
  524. node.fillColor = 'orange'
  525. } else {
  526. node.fillColor = data.backGroundColor
  527. }
  528. } else {
  529. let lineColor = node_colors[data.entClsID]
  530. if (lineColor === undefined) {
  531. lineColor = getColor()
  532. }
  533. node.lineColor = lineColor
  534. node.fillColor = 'white'
  535. if (isDivisionColor) {
  536. // 根据创建和修改的时间,标记颜色
  537. if (data.createTime && !data.updateTime) {
  538. node.lineColor = '#f5222d'
  539. } else if (data.createTime == data.updateTime) {
  540. node.lineColor = '#1890ff'
  541. } else if (data.createTime != data.updateTime) {
  542. node.lineColor = '#52c41a'
  543. }
  544. node.lineWidth = 3
  545. } else {
  546. if (!!node.data.backGroundColor) {
  547. node.fillColor = data.backGroundColor
  548. }
  549. }
  550. }
  551. }
  552. // 如果当前节点在高亮节点集合中
  553. if (
  554. highLightCache.nodeIds &&
  555. highLightCache.nodeIds.indexOf(data.id) > -1
  556. ) {
  557. node.fillColor = 'orange'
  558. }
  559. },
  560. },
  561. created() {
  562. window.isEntChart = this.isEntChart
  563. isDivisionColor = this.isDivisionColor
  564. },
  565. destroyed() {
  566. this.destroyChart()
  567. },
  568. }
  569. // 定义线的样式
  570. function linkStyle(link) {
  571. // debugger
  572. let data = link.data.data ? link.data.data : link.data
  573. // link.label = data.label ? data.label : data.relName
  574. getLinkClass(link)
  575. if (
  576. highLightCache.linkIds &&
  577. highLightCache.linkIds.indexOf(data.id) > -1
  578. ) {
  579. if (!link.data.backGroundColor) {
  580. link.fillColor = 'orange'
  581. } else {
  582. link.fillColor = data.backGroundColor
  583. }
  584. link.radius = 4
  585. link.label = ''
  586. }
  587. }
  588. // 格式化成zoomchart需要的nodes
  589. function getNodes(nodes) {
  590. if(nodes){
  591. nodes.forEach((node, index) => {
  592. if (node.entID) node.id = node.entID
  593. if (node.label === undefined) node.label = node.entName
  594. if (!node_colors[node.entClsID]) {
  595. node_colors[node.entClsID] = colors[index % nodes.length]
  596. }
  597. })
  598. return nodes
  599. } else {
  600. return []
  601. }
  602. }
  603. // 格式化成zoomchart需要的links
  604. function getLinks(links) {
  605. if(links){
  606. links.forEach((link, index) => {
  607. if (link.relID) link.id = link.relID
  608. if (link.from === undefined) link.from = link.startEntID
  609. if (link.to === undefined) link.to = link.endEntID
  610. if (!link_colors[link.relClsID]) {
  611. link_colors[link.relClsID] = colors[index % links.length]
  612. }
  613. })
  614. return links
  615. } else {
  616. return []
  617. }
  618. }
  619. // 生成随机颜色
  620. function getColor() {
  621. let r = Math.floor(Math.random() * 255)
  622. let g = Math.floor(Math.random() * 255)
  623. let b = Math.floor(Math.random() * 255)
  624. return 'rgba(' + r + ',' + g + ',' + b + ',0.8)'
  625. }
  626. // 根据关系的classType生成不同的线的样式
  627. function getLinkClass(link) {
  628. let data = link.data.data ? link.data.data : link.data
  629. if (data.classType === 'dashed') {
  630. link.toDecoration = 'arrow'
  631. link.lineDash = [10, 10, 2, 2]
  632. } else {
  633. link.toDecoration = 'arrow'
  634. link.items = [
  635. {
  636. text: _vue.showRelKey ? data.relClsName : data.relName,
  637. padding: 2,
  638. backgroundStyle: {
  639. fillColor: 'white',
  640. // lineColor: link_colors[data.relClsID]
  641. lineColor: 'black',
  642. },
  643. textStyle: {
  644. fillColor: 'black',
  645. },
  646. },
  647. ]
  648. }
  649. return link
  650. }
  651. function computePath(nodes) {
  652. let linkIds = []
  653. const currentLinks = chart.links()
  654. nodes.forEach((v) => {
  655. let startId = v.startEntVo.entID,
  656. endId = v.endEntVOS.entID
  657. currentLinks.forEach((_v) => {
  658. if (startId === _v.from.id && endId === _v.to.id) {
  659. linkIds.push(_v.id)
  660. }
  661. })
  662. })
  663. return linkIds
  664. }
  665. </script>
  666. <style>
  667. .DVSL-menu-container.DVSL-menu-workaround {
  668. display: none;
  669. }
  670. .flex-center {
  671. position: relative;
  672. }
  673. .road_computed_blank,
  674. .relevance_blank {
  675. width: auto;
  676. border-radius: 4px;
  677. background-size: cover;
  678. }
  679. .road_computed_blank {
  680. /* background-image: url('../../assets/banner (1).png'); */
  681. }
  682. .relevance_blank {
  683. background-size: 100% 100%;
  684. /* background-image: url('../../assets/banner (2).png'); */
  685. }
  686. </style>