index.vue 5.96 KB
<template>
  <div>
    <div class="tool-style">
      图例:
      <el-tag type="success">待机</el-tag>
      <el-divider direction="vertical"></el-divider>
      <el-tag>正常运行</el-tag>
      <el-divider direction="vertical"></el-divider>
      <el-tag type="warning">非自动</el-tag>
      <el-divider direction="vertical"></el-divider>
      <el-tag type="danger">异常</el-tag>
      <el-divider direction="vertical"></el-divider>
      <el-button type="primary" size="mini" icon="el-icon-refresh" @click="reset">复位</el-button>
    </div>
    <div id="flow" class="canvas-style">
      <div id="container"></div>
    </div>
  </div>
</template>

<script>
import FlowGraph from './flow/graph';
import graphData from './flow/graph/data/test-data';
// import graphData from './flow/graph/data/data-custom-3';
import { getGUID } from '@/utils/index.js';
import { $, getContainerSize } from './flow/index.js';
import { conveyor_left, conveyor_center, conveyor_right } from './flow/graph/images';

export default {
  name: 'MapModule',
  data() {
    return {
      canvas: null,
      graph: null,
      initialTranslate: { x: 0, y: 0 },
      initialScale: 1,
      dataJson: graphData
    };
  },
  props: {
    isActive: {
      type: Boolean,
      default: false
    }
  },
  mounted() {
    this.getFlowJson()
  },
  destroyed() {
    const { graph } = FlowGraph
    // 销毁画布,资源回收
    if(graph) graph.dispose()
    // 移除监听
    window.removeEventListener('resize', this.resizeFn)
  },
  methods: {
    // 去后台拿json
    getFlowJson() {
      setTimeout(() => {
        const graphJson = this.dataJson;//JSON.parse(window.localStorage.getItem('graphJson'))
        // console.log(graphJson)
        if(graphJson) {
          for (let i = 0; i < 8; i++) {
            var x = 150;
            var y = 150;
            switch (i) {
              case 1:
                y = 180;
                break;
              case 2:
                y = 270;
                break; 
              case 3:
                y = 300;
                break; 
              case 4:
                y = 360;
                break; 
              case 5:
                y = 390;
                break; 
              case 6:
                y = 480;
                break; 
              case 7:
                y = 510;
                break; 
            }
            for (let j = 0; j < 28; j++) {
              graphJson.cells.push({
                position: {
                  x: x,
                  y: y
                },
                attrs: {
                  text: {
                  text: j + 1
                  }
                },
                size: {
                  width: 30,
                  height: 30
                },
                shape: 'hh-storage',
                id: getGUID(),
                zIndex: 1
              })
              x += 30;
            }
          }
          
          this.initFlowImage(graphJson)
        } 
      }, 300)
    },
    // 根据json渲染
    initFlowImage(graphJson) {
      // 初始化画板
      const graph = FlowGraph.init($('#container'), $('#container').getBoundingClientRect().width, $('#container').getBoundingClientRect().height, false)
      // 渲染操作
      graph.fromJSON(graphJson)

      // // 定义新节点的数据
      // const newNode = {
      //   position: { x: 500, y: 500 },
      //   size: { width: 80, height: 42 },
      //   attrs: {
      //     text: { text: '新节点' },
      //     body: { rx: 24, ry: 24 }
      //   },
      //   visible: true,
      //   shape: 'flow-chart-rect',
      //   id: 'new-node-id',
      //   zIndex: 24,
      //   data: { status: 0, pointCode: '53', fieldName: 'newFieldName' }
      // };
      // // 添加新节点到图中
      // graph.addNode(newNode);

      // 监听数据改变事件
      graph.getNodes().forEach(node => {
        if(node.getData()) {
          var data = node.getData();
          if (data.code.startsWith("P") && data.type != undefined) {
            console.log(data)
            if (data.type == "left") {
              node.attr('image/xlink:href', conveyor_left)
            } else if (data.type == "right") {
              node.attr('image/xlink:href', conveyor_right)
              // node.attr('body/fill','green')
              // node.attr('text/fill', '#080808')
            } else if (data.type == "center") {
              node.attr('image/xlink:href', conveyor_center)
              // node.attr('body/fill','green')
              // node.attr('text/fill', '#080808')
            }
          }
          node.on("change:data", ({ cell, current }) => {
            // current 就是我们绑定的 业务属性data
            if(current.status == 0) {
              cell.attr('body/fill', 'red')
              cell.attr('text/fill', '#fff')
              cell.attr('text/text', '100℃')
            } else {
              cell.attr('body/fill', 'green')
              cell.attr('text/fill', '#080808')
              cell.attr('text/text', '150℃')
            }
          })
          // const newData = { ...node.getData(), status: node.getData().status != 0 ? 0 : 1 };
          // node.setData(newData);
        }
      })

      window.addEventListener('resize', this.resizeFn)
    },    

    resizeFn() {
      setTimeout(() => {
        const { graph } = FlowGraph;
        const { width, height } = getContainerSize($('#flow'));
        graph.resize(width, height);
      }, 100);
    },

    reset(){
      const { graph } = FlowGraph;
      if (graph) {
        console.log(this.initialScale);
        console.log(this.initialTranslate);
        // 恢复初始缩放比例
        graph.scale(1, 1);
        // 恢复初始位置
        graph.translate(0, 0);
      }
    },
  }
}
</script>

<style scoped>
.tool-style{
  position: absolute;
  /* left: 0px;
  top: 0px; */
  margin: 10px;
  z-index: 99999;
}
.canvas-style{
  border: 1px solid #EBEEF5;
  width: 100vw;
  height: 100vh
}

</style>
<style>
.el-main {
  padding-top: 0px;
}
</style>