采用Mxgraph在Vue中开发流程图

采用Mxgraph在Vue中开发流程图

适用于可视化的展示树状图,以及当前选择的节点路径

1.参考代码

<template>
  <div ref="graph_container" style="width: 100%; height: 600px;" className="graph_container"></div>
</template>

<script>
import {mxGraph, mxConstants, mxRectangle, mxGeometry, mxPoint, mxConnectionConstraint} from 'mxgraph-js';

export default {
  name: 'HelloWorld',
  mounted() {
    const treeData = [
      {
        label: '节点1',
        value: '1',
        isDelete: true,
        children: [
          {
            label: '节点1.1',
            value: '1.1',
            children: [],
          },
          {
            label: '节点1.2',
            value: '1.2',
            isDelete: true,
            children: [
              {
                label: '节点1.2.1',
                value: '1.2.1',
                isDelete: true,
                children: [],
              },
              {
                label: '节点1.2.2',
                value: '1.2.2',
                children: [],
              },
            ],
          },
        ],
      },
    ];

    // Create canvas
    const container = this.$refs.graph_container;
    const graph = new mxGraph(container);
    const parent = graph.getDefaultParent();
    graph.setCellsEditable(false);
    // Start updating the canvas
    graph.getModel().beginUpdate();
    try {
      // Draw the tree
      const drawTree = function (node, parentVertex, xOffset, yOffset) {
        let fillColor1, fillColor2, strokeColor;
        if (node.isDelete) {
          fillColor1 = '#F8F8FB'; // Red color for true
          fillColor2 = '#F8E3E5'; // Red color for true
          strokeColor = '#ff0000'; // Red color for true
        } else {
          fillColor1 = '#F8F8FB'; // Blue color for false
          fillColor2 = '#D1E1FC'; // Red color for true
          strokeColor = '#0D6EFF'; // Red color for true
        }

        const vertex = graph.insertVertex(
          parent,
          null,
          node.label,
          xOffset,
          yOffset,
          80,
          30,
          `fillColor=${fillColor1};gradientColor=${fillColor2};gradientDirection=west;strokeColor=${strokeColor};strokeWidth=1;rounded=1;`
        );

        if (parentVertex) {
          const edge = graph.insertEdge(parent, null, '', parentVertex, vertex, `strokeColor=${strokeColor}`);
          // Set edge style to be straight
          graph.setCellStyle(mxConstants.STYLE_EDGE + '=straight', [edge]);
          // Disable edge editing
          edge.setConnectable(false);
          // Set edge geometry to make sure it connects to the center of the vertex
          const geo = new mxGeometry();
          geo.relative = true;
          geo.sourcePoint = new mxPoint(0.5, 1);
          geo.targetPoint = new mxPoint(0.5, 0);
          graph.getModel().setGeometry(edge, geo);

          // Set connection constraints to prevent edges from being disconnected
          const sourceConstraint = new mxConnectionConstraint(new mxPoint(0.5, 1), false);
          const targetConstraint = new mxConnectionConstraint(new mxPoint(0.5, 0), false);
          graph.getConnectionConstraint(edge, parentVertex, true, sourceConstraint);
          graph.getConnectionConstraint(edge, vertex, false, targetConstraint);

          // Set edge color to match the vertex stroke color
          graph.setCellStyles(mxConstants.STYLE_STROKECOLOR, strokeColor, [edge]);
        }
        if (node.children && node.children.length > 0) {
          const childXOffset = xOffset + 100;
          const childYOffsetTop = yOffset - 60;
          const childYOffsetBottom = yOffset + 60;
          for (let i = 0; i < node.children.length; i++) {
            const childYOffset = i % 2 === 0 ? childYOffsetTop : childYOffsetBottom;
            drawTree(node.children[i], vertex, childXOffset, childYOffset);
          }
        }
        return vertex;
      };

      // Use data to draw the tree with the root node in the center
      const rootXOffset = 200;
      const rootYOffset = 200;
      const rootVertex = drawTree(treeData[0], null, rootXOffset, rootYOffset);

      // Auto-adjust the tree height and width
      const bounds = graph.getGraphBounds();
      const height = bounds.y + bounds.height + 20;
      const width = bounds.x + bounds.width + 20;
      graph.view.setGraphBounds(new mxRectangle(0, 0, width, height));
    } finally {
      // End canvas update
      graph.getModel().endUpdate();
    }
  },
};
</script>

<style>
</style>

2.效果图

相关推荐

  1. Vue使用websocket的流程

    2023-12-23 13:58:02       8 阅读
  2. mxgraph实现json、png、xml上传代码

    2023-12-23 13:58:02       35 阅读
  3. Vue创建生产和开发环境

    2023-12-23 13:58:02       19 阅读
  4. vue项目开发流程

    2023-12-23 13:58:02       11 阅读
  5. 如何Vue3实现无缝热重载:提升你的开发效率

    2023-12-23 13:58:02       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-23 13:58:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-23 13:58:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-23 13:58:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-23 13:58:02       18 阅读

热门阅读

  1. 视频姿态估计:DeciWatch

    2023-12-23 13:58:02       42 阅读
  2. Pytorch:torch.sum()函数用法

    2023-12-23 13:58:02       45 阅读
  3. Python实现逐行读取文本文件的几种方法

    2023-12-23 13:58:02       41 阅读
  4. centos 安装 Miniconda

    2023-12-23 13:58:02       45 阅读
  5. Python:Scrapy+Selenium相关依赖包记录

    2023-12-23 13:58:02       36 阅读
  6. 面向LLM的App架构——技术维度

    2023-12-23 13:58:02       32 阅读
  7. k8s中Chart的命名模板

    2023-12-23 13:58:02       37 阅读
  8. 19-二分-值域二分-有序矩阵中第 K 小的元素

    2023-12-23 13:58:02       38 阅读