首页 > 其他分享 >使用joinjs绘制流程图(七)-实战-绘制流程图+节点设置样式+节点添加事件

使用joinjs绘制流程图(七)-实战-绘制流程图+节点设置样式+节点添加事件

时间:2024-05-01 22:23:28浏览次数:15  
标签:style const 流程图 element joint div node2 绘制 节点

效果图

原理

joinjs中通过svg来绘制流程图,然后我们可以使用localToClientRect这个方法对节点(element)复制,它会在原来的element在svg位置上生成一个html元素,但是这样会造成原来的element节点监听的点击事件无法触发,我们可以使用原生来操作对这个元素(比如样式的设置和事件的监听)

代码

<template>
  <div class="app">
    <div ref="myholder" id="paper"></div>
  </div>
</template>

<script>
import * as joint from '@joint/core'
import $ from 'jquery'
export default {
  data() {
    return {
      graph: null,
    }
  },
  mounted() {
    const namespace = joint.shapes
    var graph = new joint.dia.Graph({}, { cellNamespace: namespace })
    this.graph = graph

    var paper = new joint.dia.Paper({
      el: this.$refs.myholder,
      model: this.graph,
      width: 800,
      height: 300,
      cellViewNamespace: namespace,
      drawGrid: true,
      gridSize: 10,
      background: {
        color: 'rgba(0, 255, 0, 0.3)',
      },
      // 禁止交互
      interactive: false, // disable default interaction (e.g. dragging)
      // 当mousemove事件的数量超过clickThreshold时,在mouseup之后不会触发pointerclick事件
      clickThreshold: 10,
    })

    this.paper = paper

    // const element = new joint.shapes.standard.Rectangle()
    // element.position(100, 50)
    // element.resize(100, 40)
    // element.addTo(this.graph)

    // Draw an HTML rectangle above the element.
    // var bbox = element.getBBox()
    // var clientRect1 = paper.localToClientRect(bbox)

    // var div = document.createElement('div')
    // div.style.position = 'fixed'
    // div.style.background = 'red'
    // div.style.left = clientRect1.x + 'px'
    // div.style.top = clientRect1.y + 'px'
    // div.style.width = clientRect1.width + 'px'
    // div.style.height = clientRect1.height + 'px'
    // div.innerHTML = `<span class='yellow'>哈哈哈</span>`
    // $(div).click(() => {
    //   console.log('发生了点击')
    // })
    // paper.el.appendChild(div)

    // 整合
    const node1 = this.drawRect({ x: 50, y: 30 }, '流程-1')
    const node2 = this.drawRect({ x: 200, y: 30 }, '流程-2')
    const node3 = this.drawRect({ x: 350, y: 30 }, '流程-3')
    const node4 = this.drawRect({ x: 500, y: 30 }, '流程-4')
    const node5 = this.drawRect({ x: 50, y: 100 }, '流程-1.1')
    const node6 = this.drawRect({ x: 350, y: 100 }, '流程-2.1')
    const node7 = this.drawRect({ x: 350, y: 150 }, '流程-2.2')

    const node2_to_node6_vetices = [new joint.g.Point(250, 100)]
    const node2_to_node7_vetices = [new joint.g.Point(250, 150)]

    this.drawLine(node1, node2)
    this.drawLine(node2, node3)
    this.drawLine(node3, node4)
    this.drawLine(node1, node5)
    this.drawLine(node2, node6, node2_to_node6_vetices)
    this.drawLine(node2, node7, node2_to_node7_vetices)
  },
  methods: {
    drawRect({ x, y }, text) {
      var rect = new joint.shapes.standard.Rectangle()
      rect.position(x, y)
      rect.resize(100, 40)
      rect.attr({
        body: {
          fill: '#2c3e50',
        },
        label: {
          text,
          fill: '#3498DB',
          fontSize: 18,
          fontWeight: 'bold',
          fontVariant: 'Microsoft YaHei',
        },
      })
      rect.addTo(this.graph)

      this.transformHtml(rect, text)
      return rect
    },
    drawLine(node1, node2, vertices) {
      var link = new joint.shapes.standard.Link()
      link.source(node1)
      link.target(node2)
      link.addTo(this.graph)

      if (vertices) {
        link.vertices(vertices)
        link.router('orthogonal')
        // link.connector('rounded')
      }
      // link.vertices([
      //   new joint.g.Point(250, 100),
      //   new joint.g.Point(280, 100),
      //   new joint.g.Point(300, 120),
      // ])
      //
      link.attr({
        line: {
          stroke: 'gray',
        },
      })
    },
    transformHtml(element, text) {
      var bbox = element.getBBox()
      // NOTE:重点方法  绘制一个html元素在element元素之上
      //  Draw an HTML rectangle above the element.
      var clientRect1 = this.paper.localToClientRect(bbox)
      var div = document.createElement('div')
      div.style.position = 'fixed'
      div.style.background = 'red'
      div.style.left = clientRect1.x + 'px'
      div.style.top = clientRect1.y + 'px'
      div.style.width = clientRect1.width + 'px'
      div.style.height = clientRect1.height + 'px'
      div.innerHTML = `<span class='yellow'>${text}</span>`
      $(div).click(function () {
        console.log(this)
      })
      this.paper.el.appendChild(div)
    },
  },
}
</script>

<style lang="less" scoped>
#paper {
  border: 1px solid;
}

/deep/.yellow {
  color: yellow;
}
</style>

标签:style,const,流程图,element,joint,div,node2,绘制,节点
From: https://www.cnblogs.com/it774274680/p/18169723

相关文章

  • [python] 基于PyWaffle库绘制华夫饼图
    华夫饼图Wafflechart是一种独特而直观的图表,用于表示分类数据。它采用网格状排列的等大小方格或矩形,每个方格或矩形分配不同的颜色或阴影来表示不同的类别。这种可视化方法有效地传达了每个类别在整个数据集中的相对比例。本文介绍如何使用基于Python的PyWaffle库绘制华夫饼图。P......
  • uniapp将图片base64绘制到画布中
     html<viewclass="content"><canvascanvas-id="myCanvas"style="width:300px;height:300px;"></canvas></view> js//获取图片的完整base64this.qrurl=res.data......
  • 用Mermaid画流程图、序列图、类图、甘特图
    左手编程,右手年华。大家好,我是一点,关注我,带你走入编程的世界。公众号:一点sir,关注领取编程资料Mermaid简介Mermaid是一种基于文本的图表定义语言,它允许开发者使用文本和代码的形式来创建图表。这种语言的设计初衷是为了方便版本控制和多人协作,同时简化图表的维护和更新过程。......
  • 大厂50万节点监控系统架构设计&Prometheus底层源码级剖析
    大厂50万节点监控系统架构设计&Prometheus底层源码级剖析 设计和实现一个大规模监控系统需要深入考虑架构设计、可伸缩性、性能优化等方面。下面是一个关于大规模监控系统架构设计的简要指南,以及有关Prometheus底层源码的剖析:大规模监控系统架构设计:1.架构设计原......
  • DOM的节点类型和新增节点
    nodeType:nodeNamenodeTypenodeValue元素1标签元素文本3标签内的文本注释8注释新增节点:vartest=function(){vardiv=document.creatElement("div");div.setAttribute=("style","color:red");div.innerHTML="这是d......
  • ROS2学习-节点名随记
    1.节点名定义:主函数中的node=WriterNode("he")定义了该节点的名称defmain(args=None):"""ros2运行该节点的入口函数,可配置函数名称"""rclpy.init(args=args)#初始化rclpynode=WriterNode("he")#新建一个节点rclpy.spin(nod......
  • 获取html中的节点对象以及设置标签的属性
    1.设置了三种方法来根据不同的场合获取不同的节点对象:vartest=function(){//获取id的值vard1=document.getElementById("d1");alert(d1.innerHTML)}vartest1=function(){//获取标签的值vardivs=document.getElementsByTagName("div");for(vari=0;i<......
  • python读取xml,添加节点
    采用minidom读取,在dom上创建新节点,dom.createElement('item')再将节点挂在对应节点下byCardNo.appendChild(item)将修改后的dom重新写入,建议换一个文件名再测试,避免覆盖defadd(filename):#创建dom文档dom=minidom.parse(filename)root=dom......
  • Godot中设置Sprite2D节点透明度
    Godot中设置Sprite2D节点透明度该方法可以用于所有CanvasItem​类及其子类···#假设在`CancasItem`节点自身脚本中执行#设置范围0~1##设置自己及子节点modulate.a=0.5##仅设置自己节点的透明度self_modulate.a=0.5#设置范围(0~255)8bit##设置自己及子节点mod......
  • ROS2学习--OOP方法编写python节点
    1.创建工作空间mkdir-ptown_ws/srccdtown_ws/src2.创建功能包ros2pkgcreatevillage_li--build-typeament_python--dependenciesrclpypkgcreate是创建包的意思--build-type用来指定该包的编译类型,一共有三个可选项ament_python、ament_cmake、cmake--dependen......