mGraph.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <template>
  2. <div
  3. style="height:100%; position: relative; font-size: 16px;"
  4. v-loading="loading"
  5. class="d-flex align-center justify-center"
  6. >
  7. <div class="legend d-flex pa-3 justify-space-between">
  8. <div class="select pointerEvents">
  9. <v-select
  10. v-model="type"
  11. :items="items"
  12. dense
  13. outlined
  14. hide-details
  15. style="background-color: #fff;"
  16. @change="changeType"
  17. ></v-select>
  18. </div>
  19. <div class="d-flex align-center">
  20. <div
  21. v-for="item in legend"
  22. :key="item.title"
  23. class="d-flex ml-5"
  24. >
  25. <div class="pa-3 mr-3 rounded-circle" :style="`background-color: ${item.color};`"></div>
  26. {{ item.title }}
  27. </div>
  28. <v-switch
  29. class="pointerEvents mt-0 ml-3"
  30. hide-details
  31. v-model="showMeta"
  32. :false-value="false"
  33. :true-value="true"
  34. label="显示元数据"
  35. @change="handleChangeMeta"
  36. ></v-switch>
  37. </div>
  38. </div>
  39. <MEmpty v-if="empty"></MEmpty>
  40. <relation-graph
  41. v-show="!empty"
  42. ref="graphRef"
  43. :options="graphOptions"
  44. :on-node-click="onNodeClick"
  45. :on-line-click="onLineClick"
  46. >
  47. <template #node="{node}">
  48. <div @contextmenu.prevent="handleContextmenu($event, node)">
  49. <div
  50. :style="{ width: node.width + 'px', height: node.height + 'px' }"
  51. :class="{ 'node-active': menu.activeId === +node.id }"
  52. class="rounded-circle"
  53. >
  54. </div>
  55. <div
  56. :style="{ 'background-color': node.color + '44', width: 20 * node.text.length + 'px' }"
  57. class="node-text"
  58. :class="{ 'node-active': menu.activeId === +node.id }"
  59. >
  60. {{ node.text }}
  61. </div>
  62. </div>
  63. </template>
  64. <template #graph-plug>
  65. <v-menu v-model="menu.show" attach :position-x="menu.x" :position-y="menu.y" absolute offset-y min-width="200">
  66. <v-list dense>
  67. <v-list-item v-for="(k, i) in menu.items" :key="i" @click="k.handle">
  68. <v-list-item-title>{{ k.title }}</v-list-item-title>
  69. </v-list-item>
  70. </v-list>
  71. </v-menu>
  72. </template>
  73. </relation-graph>
  74. </div>
  75. </template>
  76. <script>
  77. import RelationGraph from 'relation-graph'
  78. import MEmpty from '@/components/Common/empty'
  79. import { api } from '@/api/dataGovernance'
  80. const defaultNodeColor = 'rgba(238, 178, 94, 1)'
  81. const NODES_SIZE = {
  82. width: 30,
  83. height: 30
  84. }
  85. const LINE_COLOR_MAP = {
  86. 包含: '#F44336', // red
  87. 影响: '#E91E63', // pink
  88. 依赖: '#3F51B5', // indigo
  89. 来源: '#4CAF50', // green
  90. 引用: '#9C27B0', // purple
  91. 继承: '#673AB7', // deep-purple
  92. 标记: '#2196F3', // blue
  93. 使用: '#03A9F4', // light-blue
  94. 关联: '#00BCD4', // cyan
  95. 拥有: '#009688', // teal
  96. 下级: '#8BC34A', // light-green
  97. 上级: '#CDDC39', // lime
  98. 联动: '#FF9800' // orange
  99. }
  100. export default {
  101. name: 'details-graph',
  102. components: { RelationGraph, MEmpty },
  103. props: {
  104. toApi: {
  105. type: Function,
  106. default: api.getResourceGraph
  107. },
  108. meta: {
  109. type: Boolean,
  110. default: false
  111. },
  112. query: {
  113. type: Object,
  114. default: () => ({})
  115. }
  116. },
  117. data () {
  118. return {
  119. menu: {
  120. x: 0,
  121. y: 0,
  122. show: false,
  123. items: [
  124. { title: '查看', handle: this.handleView }
  125. ],
  126. activeId: +this.$route.params.id,
  127. item: {}
  128. },
  129. empty: true,
  130. items: [
  131. { text: '全链关系', value: 'all' },
  132. { text: '血缘关系', value: 'kinship' },
  133. { text: '影响关系', value: 'impact' }
  134. ],
  135. type: 'all',
  136. loading: false,
  137. graphOptions: {
  138. defaultJunctionPoint: 'lr',
  139. // 这里可以参考"Graph 图谱"中的参数进行设置 https://www.relation-graph.com/#/docs/graph
  140. debug: false, // 是否开始调试模式,调试模式下会在控制台打印额外的日志信息
  141. showDebugPanel: false, // 是否显示调试按钮,通过此按钮可以打印配置、数据等
  142. backgroundImage: '', // 图谱水印url,如:https://ssl.relation-graph.com/images/relatioon-graph-canvas-bg.png
  143. downloadImageFileName: '', // 下载图片时,图片的名称
  144. disableZoom: false, // 是否禁用图谱的缩放功能
  145. disableDragNode: false, // 是否禁用图谱中节点的拖动
  146. moveToCenterWhenRefresh: true, // 当图谱刷新后(调用setJsonData或refresh方法都会触发),让图谱根据节点居中(图片会默认将根节点作为中心展示,此选项会根据节点分布寻找中心)
  147. zoomToFitWhenRefresh: true, // 当图谱刷新后(调用setJsonData或refresh方法都会触发),是否让图谱缩放到适合可见区域大小,此选项不适用于fixed和force布局
  148. useAnimationWhenRefresh: true, // 当图谱刷新后(调用setJsonData或refresh方法都会触发),使用动画让图居中、缩放
  149. useAnimationWhenExpanded: true,
  150. defaultFocusRootNode: true, // 默认为根节点添加一个被选中的样式
  151. disableNodeClickEffect: false, // 是否禁用节点默认的点击效果(选中、闪烁)
  152. disableLineClickEffect: false, // 是否禁用线条默认的点击效果(选中、闪烁)
  153. allowShowZoomMenu: true, // 是否在右侧菜单栏显示放大缩小的按钮,此设置和disableZoom不冲突
  154. allowAutoLayoutIfSupport: true, // 是否在工具栏中显示【自动布局】按钮(只有在布局支持且此选项为true时才会显示的按钮)
  155. allowShowRefreshButton: true, // 是否在工具栏中显示【刷新】按钮
  156. allowShowDownloadButton: true, // 是否在工具栏中显示【下载图片】按钮
  157. backgroundImageNoRepeat: false, // 只在右下角显示水印,不重复显示水印
  158. allowSwitchLineShape: true, // 是否在工具栏中显示切换线条形状的按钮
  159. allowSwitchJunctionPoint: true, // 是否在工具栏中显示切换连接点位置的按钮
  160. isMoveByParentNode: false, // 是否在拖动节点后让子节点跟随
  161. defaultExpandHolderPosition: 'hide', // 默认的节点展开/关闭按钮位置(left/top/right/bottom/hide)
  162. defaultNodeColor, // 默认的节点背景颜色
  163. checkedLineColor: '#FD8B37', // 当线条被选中时的颜色
  164. defaultNodeFontColor: '#ffffff', // 默认的节点文字颜色
  165. defaultNodeBorderColor: '#90EE90', // 默认的节点边框颜色
  166. defaultNodeBorderWidth: 0, // 默认的节点边框粗细(像素)
  167. defaultLineColor: '#cccccc', // 默认的线条颜色
  168. defaultLineWidth: 2, // 默认的线条粗细(像素)
  169. defaultLineShape: 2, // 默认的线条样式(1:直线/2:样式2/3:样式3/4:折线/5:样式5/6:样式6)使用示例
  170. defaultNodeShape: 0, // 默认的节点形状,0:圆形;1:矩形
  171. defaultShowLineLabel: true, // 默认是否显示连线文字,v2版本此选项已无效,主要是这个选项没什么用
  172. hideNodeContentByZoom: true, // 是否根据缩放比例隐藏节点内容
  173. // disableDragCanvas: false,
  174. // lineUseTextPath: false,
  175. defaultLineMarker: { // 默认的线条箭头样式,示例参考:配置工具中的选项:连线箭头样式
  176. markerWidth: 24,
  177. markerHeight: 24,
  178. refX: 6,
  179. refY: 6,
  180. data: 'M2,2 L10,6 L2,10 L6,6 L2,2'
  181. },
  182. layouts: [
  183. {
  184. label: '自动布局',
  185. layoutName: 'tree', // 布局方式(tree树状布局/center中心布局/force自动布局)
  186. from: 'left',
  187. maxLayoutTimes: 20,
  188. layoutClassName: 'seeks-layout-force',
  189. useLayoutStyleOptions: false,
  190. defaultNodeColor: '#FFC5A6',
  191. defaultNodeFontColor: '#000000',
  192. defaultNodeBorderColor: '#efefef',
  193. defaultNodeBorderWidth: 1,
  194. defaultLineColor: '#FD8B37',
  195. defaultLineWidth: 1,
  196. defaultShowLineLabel: true,
  197. defaultLineMarker: {
  198. markerWidth: 12,
  199. markerHeight: 12,
  200. refX: 6,
  201. refY: 6,
  202. data: 'M2,2 L10,6 L2,10 L6,6 L2,2'
  203. }
  204. }
  205. ]
  206. },
  207. config: {
  208. DataResource: { // 资源
  209. color: '#9FA8DA',
  210. title: '数据资源',
  211. className: 'sourceNode',
  212. ...NODES_SIZE
  213. },
  214. DataModel: { // 模型
  215. color: '#EF9A9A',
  216. title: '数据模型',
  217. className: 'modelNode',
  218. ...NODES_SIZE
  219. },
  220. DataMetric: { // 指标
  221. color: '#00BCD4',
  222. title: '数据指标',
  223. className: 'metricNode',
  224. ...NODES_SIZE
  225. },
  226. // standard: { // 标准
  227. // color: '#009688',
  228. // title: '数据标准',
  229. // className: 'standardNode',
  230. // ...NODES_SIZE
  231. // },
  232. DataLabel: { // 标签
  233. color: '#9C27B0',
  234. title: '数据标签',
  235. className: 'labelNode',
  236. ...NODES_SIZE
  237. },
  238. DataMeta: { // 元数据
  239. color: defaultNodeColor,
  240. title: '元数据',
  241. className: '',
  242. ...NODES_SIZE
  243. }
  244. },
  245. showMeta: this.meta
  246. }
  247. },
  248. computed: {
  249. legend () {
  250. return Object.values(this.config)
  251. }
  252. },
  253. mounted () {
  254. this.init()
  255. },
  256. methods: {
  257. handleContextmenu (v, node) {
  258. const { left, top } = this.$refs.graphRef.$el.getBoundingClientRect()
  259. this.menu.x = v.clientX - left
  260. this.menu.y = v.clientY - top
  261. this.menu.item = node
  262. this.menu.show = true
  263. },
  264. handleView () {
  265. this.draw({ id: +this.menu.item.id }, () => {
  266. this.menu.activeId = +this.menu.item.id
  267. })
  268. },
  269. handleChangeMeta (val) {
  270. this.showMeta = val
  271. this.init()
  272. },
  273. changeType () {
  274. this.init()
  275. },
  276. handleClick (node) {
  277. console.log(node)
  278. },
  279. async init () {
  280. const query = {
  281. ...this.query
  282. }
  283. if (this.$route.params.id) {
  284. Object.assign(query, {
  285. id: +this.$route.params.id
  286. })
  287. }
  288. this.draw(query)
  289. },
  290. async draw (query, successCallback = () => {}) {
  291. // 清空再渲染
  292. this.loading = true
  293. this.empty = false
  294. try {
  295. const { data } = await this.toApi({
  296. ...query,
  297. type: this.type,
  298. meta: this.showMeta
  299. })
  300. if (!data.nodes || !data.nodes.length) {
  301. this.empty = true
  302. this.loading = false
  303. return
  304. }
  305. this.graphOptions.downloadImageFileName = data.rootId ?? ''
  306. data.nodes.forEach(ele => {
  307. if (!this.config[ele.node_type]) {
  308. return
  309. }
  310. Object.assign(ele, this.config[ele.node_type])
  311. })
  312. data.lines.forEach(ele => {
  313. ele.color = LINE_COLOR_MAP[ele.text]
  314. })
  315. this.$nextTick(() => {
  316. this.$refs.graphRef.setOptions(this.graphOptions, async (graphInstance) => {
  317. this.$refs.graphRef.setJsonData(data, async (_graphInstance) => {
  318. await _graphInstance.setZoom(75)
  319. successCallback()
  320. this.loading = false
  321. })
  322. })
  323. })
  324. } catch (error) {
  325. this.empty = true
  326. this.$snackbar.error(error)
  327. }
  328. },
  329. onNodeClick (nodeObject, $event) {
  330. console.log('onNodeClick:', nodeObject)
  331. },
  332. onLineClick (lineObject, $event) {
  333. console.log('onLineClick:', lineObject)
  334. }
  335. }
  336. }
  337. </script>
  338. <style lang="scss" scoped>
  339. .legend {
  340. pointer-events: none;
  341. width: 100%;
  342. user-select: none;
  343. -moz-user-select: none;
  344. -webkit-user-select: none;
  345. -ms-user-select: none;
  346. position: absolute;
  347. top: 0;
  348. z-index: 10;
  349. color: #666;
  350. .rounded-circle {
  351. width: 24px;
  352. height: 24px;
  353. }
  354. }
  355. .pointerEvents {
  356. pointer-events: auto;
  357. }
  358. // ::v-deep .sourceNode .rel-node-checked {
  359. // box-shadow: 0 0 0 8px #C5CAE9 !important;
  360. // }
  361. // ::v-deep .modelNode .rel-node-checked {
  362. // box-shadow: 0 0 0 8px #ffd9d9 !important;
  363. // }
  364. // ::v-deep .metricNode .rel-node-checked {
  365. // box-shadow: 0 0 0 8px #a7f3fd !important;
  366. // }
  367. // ::v-deep .standardNode .rel-node-checked {
  368. // box-shadow: 0 0 0 8px #58c1b7 !important;
  369. // }
  370. // ::v-deep .labelNode .rel-node-checked {
  371. // box-shadow: 0 0 0 8px #f4bdff !important;
  372. // }
  373. ::v-deep .rel-node-checked {
  374. box-shadow: unset !important;
  375. }
  376. .node-active {
  377. box-shadow: 0 0 0px 6px #f3f900 !important;
  378. // background: #000 !important;
  379. // border: 2px solid #000;
  380. }
  381. .node-text {
  382. color: #000;
  383. font-size: 16px;
  384. position: absolute;
  385. height:25px;
  386. transform: translate(-50%, 0);
  387. line-height: 25px;
  388. left: 50%;
  389. margin-top:5px;
  390. text-align: center;
  391. }
  392. ::v-deep .rel-toolbar {
  393. background-color: #f39930;
  394. color: #ffffff;
  395. .c-current-zoom {
  396. color: #ffffff;
  397. }
  398. }
  399. </style>