detail.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <div class="index">
  3. <div id="flow-image">
  4. <el-button
  5. style="position: absolute; top: 2px; right: 2px; z-index: 9999"
  6. @click="$router.push('/index')">
  7. 返回
  8. </el-button>
  9. <!-- <el-button
  10. style="position: absolute; top: 2px; right: 60px; z-index: 9999"
  11. @click="editFlowData"
  12. >Edit Data</el-button
  13. > -->
  14. <div id="flow-container"></div>
  15. </div>
  16. </div>
  17. </template>
  18. <script setup lang="ts">
  19. import { Graph } from '@antv/x6'
  20. import FlowGraph from '../graph'
  21. import graphData from '../graph/data/data'
  22. import { $, getContainerSize } from '@/utils'
  23. const router = useRouter()
  24. const route = useRoute()
  25. let graph: Graph
  26. onMounted(() => {
  27. getFlowJson()
  28. })
  29. onUnmounted(() => {
  30. // 销毁画布,资源回收
  31. if (graph) graph.dispose()
  32. // 移除监听
  33. window.removeEventListener('resize', resizeFn)
  34. })
  35. // 去后台拿json
  36. const getFlowJson = () => {
  37. // 使用setTimeout模拟一下
  38. setTimeout(() => {
  39. const id = route.params.id as string
  40. const graphJson = graphData[id].cells || '{}'
  41. if (graphJson && JSON.stringify(graphJson) !== '{}') {
  42. initFlowImage(graphJson)
  43. } else {
  44. router.push('/flow')
  45. }
  46. }, 200)
  47. }
  48. // 根据json渲染
  49. function initFlowImage(graphJson: any) {
  50. // 初始化画板
  51. graph = FlowGraph.init(
  52. $('#flow-container'),
  53. $('#flow-container').getBoundingClientRect().width,
  54. $('#flow-container').getBoundingClientRect().height,
  55. false
  56. )
  57. // 渲染操作
  58. graph.fromJSON(graphJson)
  59. // 监听数据改变事件
  60. graph.getNodes().forEach(node => {
  61. if (node.getData()) {
  62. node.on('change:data', ({ cell, current }) => {
  63. // current 就是我们绑定的 业务属性data
  64. if (current.status == 0) {
  65. cell.attr('body/fill', 'red')
  66. cell.attr('text/fill', '#fff')
  67. cell.attr('text/text', '100℃')
  68. } else {
  69. cell.attr('body/fill', 'green')
  70. cell.attr('text/fill', '#080808')
  71. cell.attr('text/text', '150℃')
  72. }
  73. })
  74. }
  75. })
  76. window.addEventListener('resize', resizeFn)
  77. }
  78. // 改变状态
  79. // function editFlowData() {
  80. // graph.getNodes().forEach(node => {
  81. // if (node.getData()) {
  82. // let curStatus = node.getData().status
  83. // node.setData({
  84. // status: curStatus == 0 ? 1 : 0
  85. // })
  86. // }
  87. // })
  88. // }
  89. const resizeFn = () => {
  90. setTimeout(() => {
  91. const { width, height } = getContainerSize($('#flow-image'))
  92. graph.resize(width, height)
  93. }, 100)
  94. }
  95. </script>
  96. <style scoped>
  97. .index {
  98. width: 100%;
  99. height: 100%;
  100. display: flex;
  101. justify-content: center;
  102. align-items: center;
  103. }
  104. #flow-image {
  105. width: 80%;
  106. height: 90%;
  107. position: relative;
  108. }
  109. #flow-container {
  110. width: 100%;
  111. height: 100%;
  112. position: absolute;
  113. top: 0;
  114. left: 0;
  115. }
  116. </style>