采用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.效果图

相关推荐
黄尚圈圈3 小时前
Vue 中引入 ECharts 的详细步骤与示例
前端·vue.js·echarts
vvvae12347 小时前
分布式数据库
数据库
雪域迷影8 小时前
PostgreSQL Docker Error – 5432: 地址已被占用
数据库·docker·postgresql
一路向前的月光8 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   8 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d
bug菌¹9 小时前
滚雪球学Oracle[4.2讲]:PL/SQL基础语法
数据库·oracle
逸巽散人9 小时前
SQL基础教程
数据库·sql·oracle
Jiaberrr9 小时前
Element UI教程:如何将Radio单选框的圆框改为方框
前端·javascript·vue.js·ui·elementui
月空MoonSky9 小时前
Oracle中TRUNC()函数详解
数据库·sql·oracle
momo小菜pa9 小时前
【MySQL 06】表的增删查改
数据库·mysql