VUE3+X6流程图实现数据双向绑定的方案

news/2024/5/19 0:09:42 标签: 流程图, javascript, vue.js

代码很简单,看看就可以了,不用多说。
定义组件:实现html节点与数据初始化、更新

<script setup lang="ts">javascript">
import { Graph, Shape } from "@antv/x6";
import { onMounted, reactive, ref, watch } from "vue";
import useFlowModel from "./useFlowModel";
import flowdata from "./flowdata";

const container: any = ref(null);

Shape.HTML.register({
  shape: "custom-html",
  effect: ["data"],
  html(cell: any) {
    const { bgcolor, label } = cell.getData();
    return `<div style="background:${bgcolor};width:100%;height:100%;">${label}</div>`;
  },
});

const flowModel: any = reactive({
  graph: null,

  datatype: 0,
  modeldata: flowdata,
});
const { graphToModelData, modelDataToGraph, addNode, addNodeData } =
  useFlowModel(flowModel);

onMounted(() => {
  flowModel.graph = new Graph({
    container: container.value,
    // 设置画布背景颜色
    background: {
      color: "#F2F7FA",
    },
    height: 300,
    width: 600,
    grid: {
      visible: true,
      type: "doubleMesh",
      args: [
        {
          color: "#eee", // 主网格线颜色
          thickness: 1, // 主网格线宽度
        },
        {
          color: "#ddd", // 次网格线颜色
          thickness: 1, // 次网格线宽度
          factor: 4, // 主次网格线间隔
        },
      ],
    },
  });

  flowModel.graph.fromJSON(flowModel.modeldata); // 渲染元素
  flowModel.graph.centerContent(); // 居中显示

  flowModel.graph.on("cell:changed", (val: any) => {
    graphToModelData(val);
  });

  watch(
    () => flowModel.modeldata.cells.map((el: any) => JSON.stringify(el.data)),
    (newVal, oldVal) => {
      modelDataToGraph(newVal, oldVal);
    },
    { deep: true }
  );
});
</script>

<template>
  <template
    v-if="flowModel.modeldata?.cells && flowModel.modeldata?.cells[0]?.position"
  >
    <input type="number" v-model="flowModel.modeldata.cells[0].position.x" />
    <input type="text" v-model="flowModel.modeldata.cells[0].data.label" />
    <input type="text" v-model="flowModel.modeldata.cells[0].data.bgcolor" />
  </template>
  【{{ flowModel.modeldata?.cells && flowModel.modeldata?.cells[0].position }}】

  <button @click="addNode()">加G</button>
  <button @click="addNodeData()">加A</button>
  <button
    @click="console.log(666.1001, flowModel.modeldata.cells[0].x, flowModel)"
  >
    实时
  </button>
  <div id="container" ref="container"></div>
  <div class="as-show-model">{{ flowModel.modeldata }}</div>
</template>

<style scoped>
html,
body {
  margin: 0;
  padding: 0;
}
.as-show-model {
  display: inline-block;
  white-space: pre-wrap;
  overflow: auto;
  height: 300px;
  width: 300px;
}
.custom-html {
  outline: 1px dashed bisque;
}
#container {
  display: inline-block;
  background-color: rgb(248, 255, 240);
  margin-right: 8px;
  margin-left: 8px;
  border-radius: 5px;
  box-shadow: 0 12px 5px -10px rgb(0 0 0 / 10%), 0 0 4px 0 rgb(0 0 0 / 10%);
}
</style>

基本函数处理

export default (flowModel: any) => {
  function modelDataToGraph(newVal: any, oldVal: any) {
    // console.log(666.333, oldVal, newVal);
    if (flowModel.datatype) {
      flowModel.datatype = 0;
    } else if (
      !oldVal?.length ||
      !newVal?.length ||
      oldVal?.length !== newVal?.length
    ) {
      flowModel.graph.fromJSON(flowModel.modeldata);
    } else {
      const nowCells: any = flowModel.graph.getCells();
      newVal.forEach((el: any, index: number) => {
        if (!oldVal.includes(el)) {
          flowModel.datatype = 2;
          nowCells[index].setProp({
            data: JSON.parse(el),
          });
        }
      });
    }
  }

  function graphToModelData(val: any) {
    // const { cell, options } = val;
    if (flowModel.datatype) {
      flowModel.datatype = 0;
    } else {
      flowModel.datatype = 1;
      flowModel.modeldata = flowModel.graph.toJSON();
    }
  }

  function addNodeData() {
    flowModel.modeldata.cells.push({
      position: {
        x: 30,
        y: 40,
      },
      size: {
        width: 100,
        height: 40,
      },
      data: {
        bgcolor: "#8f8f8f",
        label: "我是节点",
        color: "#333232",
        a: { B: 1 },
      },
      attrs: {
        text: {
          text: "我是节点",
        },
        body: {
          stroke: "#8f8f8f",
          strokeWidth: 1,
          fill: "#fff",
          rx: 6,
          ry: 6,
        },
      },
      visible: true,
      shape: "custom-html",
      id: "node1" + Date.now(),
      zIndex: 1,
    });
  }
  function addNode() {
    flowModel.graph.addNode({
      shape: "custom-html",
      x: 60,
      y: 100,
      width: 80,
      height: 40,
      data: {
        bgcolor: "#8f8f8f",
        label: "我是节点",
      },
    });
  }

  return { graphToModelData, modelDataToGraph, addNode, addNodeData };
};

演示数据

export default {
  cells: [
    {
      position: {
        x: 30,
        y: 40,
      },
      size: {
        width: 120,
        height: 40,
      },
      data: {
        bgcolor: "#8f8f8f",
        label: "我是节点",
        color: "#333232",
        a: { B: 1 },
      },
      attrs: {},
      visible: true,
      shape: "custom-html",
      id: "node1",
      zIndex: 1,
    },
    {
      position: {
        x: 160,
        y: 180,
      },
      size: {
        width: 111,
        height: 40,
      },
      attrs: {
        text: { text: "世界你好" },
        body: {
          stroke: "#8f8f8f",
          strokeWidth: 1,
          fill: "#fff",
          rx: 6,
          ry: 6,
        },
      },
      visible: true,
      shape: "rect",
      id: "node2",
      zIndex: 1,
    },
    {
      position: {
        x: 190,
        y: 100,
      },
      size: {
        width: 160,
        height: 40,
      },
      data: {
        bgcolor: "#8f8f8f",
        label: "my test",
        color: "#333232",
        a: { B: 1 },
      },
      attrs: {},
      visible: true,
      shape: "custom-html",
      id: "node3",
      zIndex: 1,
    },
    {
      shape: "edge",
      attrs: {
        label: {
          text: "x6",
        },
        line: {
          stroke: "#8f8f8f",
          strokeWidth: 1,
        },
      },
      id: "0d9f0031-f018-413c-9631-1cd21d2f8c1f",
      source: {
        cell: "node1",
      },
      target: {
        cell: "node2",
      },
      labels: [
        {
          attrs: {
            label: {
              text: "x6",
            },
          },
        },
      ],
      zIndex: 1,
    },
  ],
};


http://www.niftyadmin.cn/n/5414946.html

相关文章

机器学习是什么?如何从入门到精通?

机器学习&#xff08;Machine Learning&#xff09;是一种从数据中自动学习模式和规律&#xff0c;并用于做出预测和决策的领域。它利用统计学、数学和计算机科学的方法&#xff0c;让计算机从大量数据中学习并不断优化模型&#xff0c;以实现自动化的决策和预测。 要从入门到…

解决QT cc1plus.exe: error: out of memory allocating

QT中增加资源文件过大时&#xff0c;会编译不过&#xff0c;报错&#xff1a; cc1plus.exe: out of memory allocating 1073745919 bytes 使用qrc资源文件&#xff0c;也就是在QT的工程中添加资源文件&#xff0c;就是添加的资源文件&#xff08;如qrc.cpp&#xff09;会直接被…

【Golang星辰图】从文件读写到Excel处理:深入探索Go语言中的文件和I/O处理技术

深入探索Go语言中的文件操作和I/O处理&#xff1a;完整指南和实用示例代码 前言 文件操作和I/O处理是编程中常见的任务之一&#xff0c;对于任何语言而言&#xff0c;都是必不可少的。在Go语言中&#xff0c;为了处理文件和进行输入输出操作&#xff0c;我们可以使用多个标准…

【仿真总结】基于matlab的传递函数计算与绘图

前言 在DC-DC电路控制算法中&#xff0c;PID控制是最常见且实用的&#xff0c;但实现前提有二&#xff0c;一是需要手算电路传递函数&#xff0c;二是需要将实际电路元件数值代入计算&#xff0c;第一步无法避免&#xff0c;但是在进行第二步时&#xff0c;存在大量基础、细致的…

PostgreSQL教程(二十四):服务器管理(六)之数据库角色

PostgreSQL使用角色的概念管理数据库访问权限。一个角色可以被看成是一个数据库用户或者是一个数据库用户组&#xff0c;这取决于角色被怎样设置。角色可以拥有数据库对象&#xff08;例如&#xff0c;表和函数&#xff09;并且能够把那些对象上的权限赋予给其他角色来控制谁能…

Java后端八股笔记

Java后端八股笔记 Redis八股 上两种都有可能导致脏数据 所以使用两次删除缓存的技术&#xff0c;延时是因为数据库有主从问题需要更新&#xff0c;无法达到完全的强一致性&#xff0c;只能达到控制一致性。 一般放入缓存中的数据都是读多写少的数据 业务逻辑代码&#x1f44…

单片机为什么被认为是一门简单的技术?

做了单片机开发时间越长&#xff0c;越感觉这个领域的东西&#xff0c;10年都学不完。 之前我分享过特斯拉超跑Roadster的开源资料&#xff0c;很多老铁说没啥用&#xff0c;都是过时的东西。 我发现&#xff0c;说这些话的&#xff0c;都是些初学者&#xff0c;或者从业不久的…

node项目通过.env文件配置环境变量

https://www.npmjs.com/package/dotenv require(dotenv).config()console.log(process.env, process.env.apiKeyOnServer)我开发的chatgpt项目&#xff1a; https://chat.xutongbao.top