vue使用jsplumb 流程图

news/2024/5/18 22:19:17 标签: vue.js, 流程图, javascript
  1. 安装jsPlumb库:在Vue项目中使用npm或yarn安装jsPlumb库。

npm install jsplumb

  1. 创建一个Vue组件:创建一个Vue组件来容纳jsPlumb的功能和呈现。
javascript"><template>
  <div style="margin: 20px">
    <div style="margin: 20px">
      <el-button type="primary" size="mini" @click="clearCanvas()"
        >清除连线</el-button
      >
      <el-button type="primary" size="mini" @click="startCanvas()"
        >绘制</el-button
      >
    </div>
    <div class="liucFlex" id="flowContainer">
      <div class="left">
        <div
          @click="clickTitle(item)"
          class="boxLiu"
          v-for="(item, index) in aItem"
          :key="index"
        >
          <div class="word" :class="{ isClick: activeName == item.id }">
            <div><i class="el-icon-view iconRight"></i>{{ item.name }}</div>
            <div class="date">{{ item.date }}</div>
          </div>
          <div class="status" :class="item.status" :id="item.id"></div>
        </div>
      </div>
      <div class="right">
        <div
          class="boxLiu"
          @click="clickTitle(item)"
          v-for="(item, index) in bItem"
          :key="index"
        >
          <div class="status" :class="item.status" :id="item.id"></div>
          <div class="word" :class="{ isClick: activeName == item.id }">
            <div>{{ item.name }}<i class="el-icon-view iconRight"></i></div>
            <div class="date">{{ item.date }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { jsPlumb } from "jsplumb";

export default {
  name: "table4",
  props: {},
  components: {},
  data() {
    return {
      // status: 表示连接线状态;error:红色,success:绿色,info:灰色
      aItem: [
        {
          name: "a节点1",
          date: "2023-01-01 20:46",
          id: "1",
          status: "error",
        },
        {
          name: "a节点2",
          date: "2023-01-01 20:46",
          id: "2",
          status: "success",
        },
        {
          name: "a节点3",
          date: "2023-01-01 20:46",
          id: "3",
          status: "success",
        },
        {
          name: "a节点4",
          date: "2023-01-01 20:46",
          id: "4",
          status: "success",
        },
        {
          name: "a节点5",
          id: "5",
          status: "info",
        },
        {
          name: "a节点6",
          id: "6",
        },
        {
          name: "a节点7",
          date: "2023-01-01 20:46",
          id: "7",
          status: "success",
        },
      ],
      bItem: [
        {
          name: "b节点1",
          date: "2023-01-01 20:46",
          id: "11",
          status: "error",
        },
        {
          name: "b节点2",
          date: "2023-01-01 20:46",
          id: "12",
          status: "error",
        },
        {
          name: "b节点3",
          id: "13",
        },
        {
          name: "b节点4",
          date: "2023-01-01 20:46",
          id: "14",
          status: "success",
        },
        {
          name: "b节点5",
          date: "2023-01-01 20:46",
          id: "15",
          status: "success",
        },
        {
          name: "b节点6",
          id: "16",
        },
        {
          name: "b节点7",
          id: "17",
        },
      ],
      plumbIns: null, // 折线初始化的对象
      activeName: null, // 当前选中高亮的id
      // 步骤图的默认配置
      defaultConfig: {
        // 对应上述基本概念
        anchor: ["TopCenter",[0.5, 1, 0, 0]],
        connector: ["Flowchart", { cornerRadius: 0, width: 1, curviness: 50 }],
        endpoint: "Blank",
        // 添加样式
        paintStyle: { stroke: "#E0E3EA", strokeWidth: 1, curviness: 100 }, // connector
        // 添加 overlay,如箭头
        overlays: [["Arrow", { width: 5, length: 5, location: 1 }]], // overlay
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.setPlumbIns();
  },
  activated() {
    // this.setPlumbIns();
  },
  // 路由切换的时候一定要重置setPlumbIns,防止保留上次绘制的线
  deactivated() {
    this.clearCanvas();
  },
  beforeDestroy() {
    this.clearCanvas();
  },
  methods: {
    // 点击清除连线
    clearCanvas() {
      if (this.plumbIns) this.plumbIns?.reset();
    },
    // 绘制连线
    startCanvas() {
      this.setPlumbIns();
    },
    // 点击切换事件
    clickTitle(item) {
      this.activeName = item.id;
    },
    // 初始化连线
    setPlumbIns() {
      if (!this.plumbIns)
        // 一定要指定连接线的绘制容器,该容器为设置的盒子dom的id值,要给这个css盒子设置一个position:relative属性,不然连线的位置不对,会偏移的很严重,如果不设置将以body容器进行绘制
        this.plumbIns = jsPlumb.getInstance({
          Container: "flowContainer",
        });
      let relations = [];
      // 将新数组转换成所需格式
      for (let i = 0; i < this.aItem.length - 1; i++) {
        relations.push([this.aItem[i].id, this.aItem[i + 1].id]);
      }
      // 获取right的数组
      for (let i = 0; i < this.bItem.length - 1; i++) {
        relations.push([this.bItem[i].id, this.bItem[i + 1].id]);
      }
      let aTob = [];
      // left和right节点相接的地方
      aTob.push(["4", "11"]);
      aTob.push(["15", "7"]);
      this.plumbIns.ready(() => {
        // 默认连线
        for (let item of relations) {
          this.plumbIns.connect(
            {
              source: item[0],
              target: item[1],
            },
            this.defaultConfig
          );
        }
        // a和b相交的连线
        let aTobConfig = JSON.parse(JSON.stringify(this.defaultConfig));
        // 设置a与b节点连接线的方式
        aTobConfig.anchor = ["Left", "Right"];
        for (let item of aTob) {
          this.plumbIns.connect(
            {
              source: item[0],
              target: item[1],
            },
            aTobConfig
          );
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.liucFlex {
  display: flex;
  width: 500px;
  color: #101010;
  font-size: 14px;
  position: relative;
  .word {
    width: 110px;
    height: 50px;
    cursor: pointer;
  }
  .isClick {
    color: #409eff !important;
  }
  .right,
  .left {
    flex: 1;
    margin: 0 10px;
  }
  .right {
    .iconRight {
      margin-left: 5px;
    }
    .status {
      margin-right: 10px;
    }
  }
  .left {
    .iconRight {
      margin-right: 5px;
    }
    .status {
      margin-left: 10px;
    }
    .boxLiu {
      text-align: right;
    }
  }
  .boxLiu {
    display: flex;
    margin-bottom: 20px;
  }
  .status {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background-color: #e0e3ea;
    vertical-align: top;
    margin-top: 3px;
  }

  .date {
    font-size: 12px;
    margin-top: 10px;
    color: #d0d3d9;
  }
  .error {
    background-color: #f56c6c !important;
  }
  .success {
    background-color: #7ac756 !important;
  }
  .info {
    background-color: #e0e3ea !important;
  }
}
</style>

效果图:

 

 

  1.  初始化jsPlumb一定要在mounted函数里面,要执行在dom渲染完成的时候
  2. 一定要设置绑定的容器,不然连线的容器外加入任何其他布局元素,线会偏移,需要给绑定的容器设置position:relative(原因不详,因为我不设置这个属性线偏移的很严重)
  3. 路由切换或者多容器需要连线设置,需要重置jsPlumb(this.plumbIns?.reset()),不然线会一直在
jsPlumb中一些常用的参数和API的说明
参数/方法描述
Container设置连接线的绘制容器,将连接线限制在指定的容器内绘制
Draggable将元素设置为可拖动,可以被拖动到其他位置
Endpoint定义连接线端点的样式和行为
Connector定义连接线的样式和类型
Anchors定义连接线起始点和目标点的锚点位置
PaintStyle定义连接线的绘制样式,如颜色、线宽等
hoverPaintStyle鼠标悬停在连接线上时的绘制样式
Endpoints定义连接线的起始点和目标点的端点样式
MaxConnections指定一个元素可以拥有的最大连接数
Scope用于分组连接线和元素的范围,可以控制连接线的可见性和交互性
ConnectionOverlays定义连接线上的覆盖物,如箭头、标签等
addEndpoint动态添加一个连接线的端点
connect连接两个元素,创建一条连接线
repaintEverything重新绘制所有连接线和端点,适用于当容器大小改变时需要重新布局时
bind绑定事件处理程序到连接线或元素上
unbind取消绑定事件处理程序
removeAllEndpoints移除所有元素的端点
deleteEndpoint删除指定元素的一个端点
destroy销毁jsPlumb实例,清除所有的连接线和端点


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

相关文章

系统架构设计师---2018年下午试题1分析与解答(试题四)

系统架构设计师---2018年下午试题1分析与解答 试题四 阅读以下关于分布式数据库缓存设计的叙述,在答题纸上回答问题 1 至问题 3. 【说明】 某企业是为城市高端用户提供高品质蔬菜生鲜服务的初创企业,创业初期为快速开展业务,该企业采用轻量型的开发架构(脚本语言+关系型…

Viobot ROS主从机配置

本篇介绍如何配置Viobot的ROS主从机&#xff0c;设备已经默认配好了主机的大部分设置&#xff0c;由于涉及到开机自启动&#xff0c;所以主机必须是Viobot。 以虚拟机ubuntu20.04为例。 1.从机配置 虚拟机终端输入命令,输入密码&#xff0c;按下图修改文件&#xff0c;保存…

2023全国大学生数学建模竞赛A题B题C题D题E题思路+模型+代码+论文

目录 一. 2023国赛数学建模思路&#xff1a; 赛题发布后会第一时间发布选题建议&#xff0c;思路&#xff0c;模型代码等 详细思路获取见文末名片&#xff0c;9.7号第一时间更新 二.国赛常用的模型算法&#xff1a; 三、算法简介 四.超重要&#xff01;&#xff01;&…

Android Studio:Could not initialize class org.codehaus.groovy.vmplugin.v7.Java7

原项目使用jdk8&#xff0c;升级gradle后出现的该问题。 java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.vmplugin.v7.Java7at org.codehaus.groovy.vmplugin.VMPluginFactory.<clinit>(VMPluginFactory.java:43)at org.codehaus.gro…

DAY2,ARM(特殊功能寄存器,数据操作指令,跳转指令)

1.cmp、sub、b指令的使用&#xff1b; 代码&#xff1a; .text .global _start _start:mov r0,#9mov r1,#15loop:cmp r0,r1beq stopsubcc r1,r1,r0subhi r0,r0,r1b loopstop:b stop .end结果&#xff1a; 2.汇编指令计算1~100之间和&#xff1b; 代码&#xff1a; .text .gl…

Linux网络编程(多路IO复用select函数使用)

文章目录 前言一、什么是多路IO复用二、select函数讲解三、使用select编程并发服务器四、select函数的缺点总结 前言 本篇文章带大家来学习一下多路IO复用select函数的使用。 一、什么是多路IO复用 1.多路I/O复用&#xff08;Multiplexing I/O&#xff09;是一种用于同时监视…

VM部署CentOS并且设置网络

最近在准备学习k8s&#xff0c;需要部署服务器&#xff0c;所以需要在虚拟机中部署centOS服务&#xff0c;接下来我们将一步一步到操作来&#xff0c;如何在VM中不是CentOS系统。 一&#xff1a;环境 VMware Workstation Pro 链接&#xff1a;https://pan.baidu.com/s/1hSKr…

C语言暑假刷题冲刺篇——day3

目录 一、选择题 二、编程题 &#x1f388;个人主页&#xff1a;库库的里昂 &#x1f390;CSDN新晋作者 &#x1f389;欢迎 &#x1f44d;点赞✍评论⭐收藏✨收录专栏&#xff1a;C语言每日一练✨其他专栏&#xff1a;代码小游戏C语言初阶&#x1f91d;希望作者的文章能对你有…