vue项目里引用Gojs画可拖拽流程图

项目需求
实现可拖拽,可链接,自定义流程图,双击可修改文字
项目效果截图
在这里插入图片描述
vue引入GoJS
1.安装
npm install --save gojs
2.全局引入——在main.js中引入
import gojs from ‘gojs’
Vue.prototype.go = gojs
3.单页面引入
import go from ‘gojs’

drawFlow.vue页面元素

<template>
  <div class="o-hidden">
  	<!--拖拽区域-->
    <div id="myPaletteDiv" class="fl"></div>
    <!--画布区域-->
    <div id="myDiagramDiv" class="fl"></div>
    <Button type="info"  class="btnSave" @click="save()">保存</Button>
  </div>
</template>

封装公共节点绑定样式函数

 function nodeStyle() {
          return [
            new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
            {locationSpot: go.Spot.Center}
          ];
        }

封装文字绑定样式函数

function textStyle() {
          return {
            font: "bold 11pt Helvetica, Arial, sans-serif",
            stroke: "whitesmoke",
            editable: true,
            margin: 3
          }
        }

封装连接点端口函数

function makePort(name, align, spot, output, input) {
          var horizontal = align.equals(go.Spot.Top) || align.equals(go.Spot.Bottom);
          return $(go.Shape,
            {
              fill: "transparent",
              strokeWidth: 0,
              width: horizontal ? NaN : 8,
              height: !horizontal ? NaN : 8,
              alignment: align,
              stretch: (horizontal ? go.GraphObject.Horizontal : go.GraphObject.Vertical),
              portId: name,
              fromSpot: spot,
              fromLinkable: output,
              toSpot: spot,
              toLinkable: input,
              cursor: "pointer",
              mouseEnter: function (e, port) {
                if (!e.diagram.isReadOnly) port.fill = "rgba(255,0,255,0.5)";
              },
              mouseLeave: function (e, port) {
                port.fill = "transparent";
              }
            });
        }

拖拽区域palette设置

 _this.configData.myPalette =
          $(go.Palette, "myPaletteDiv",
            {
              scrollsPageOnFocus: false,
              //节点模板是画布区域的模板
              nodeTemplateMap: _this.configData.myDiagram.nodeTemplateMap,
              model: new go.GraphLinksModel([
                {category: "Start", text: "Start"},
                {text: "Step"},
                {category: "Conditional", text: "???"},
                {category: "End", text: "End"}
              ])
            });

点击保存按钮,浏览器下载json文件,可供以后导入使用

this.configData.mySaveModel = this.configData.myDiagram.model.toJson();
        exportRaw('drawFlow.json', this.configData.mySaveModel);

        //生成json
        function fakeClick(obj) {
          var ev = document.createEvent("MouseEvents");
          ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
          obj.dispatchEvent(ev);
        }

        function exportRaw(name, data) {
          var urlObject = window.URL || window.webkitURL || window;
          var export_blob = new Blob([data]);
          var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
          save_link.href = urlObject.createObjectURL(export_blob);
          save_link.download = name;
          fakeClick(save_link);
        }

附上具体代码
(需要注意的是this指向问题)

<template>
  <div class="o-hidden">
    <div id="myPaletteDiv" class="fl"></div>
    <div id="myDiagramDiv" class="fl"></div>
    <Button type="info"  class="btnSave" @click="save()">保存</Button>
  </div>
</template>

<script>
  import go from 'gojs';

  export default {
    name: "drawFlow",
    created() {
      this.configData.myDiagram.div = null;
      this.$axios({
        method: 'get',
        url: '@/../static/json/drawFlow.json'
      })
        .then((res) => {
          this.configData.mySaveModel = res.data;
          this.init();
        })
    },
    data() {
      return {
        configData: {
          myDiagram: {},//组态模板
          myPalette: {},
          mySaveModel: ''
        },
      }
    },
    methods: {
      save() {
        this.configData.mySaveModel = this.configData.myDiagram.model.toJson();
        exportRaw('drawFlow.json', this.configData.mySaveModel);

        //生成json
        function fakeClick(obj) {
          var ev = document.createEvent("MouseEvents");
          ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
          obj.dispatchEvent(ev);
        }

        function exportRaw(name, data) {
          var urlObject = window.URL || window.webkitURL || window;
          var export_blob = new Blob([data]);
          var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
          save_link.href = urlObject.createObjectURL(export_blob);
          save_link.download = name;
          fakeClick(save_link);
        }
      },
      init() {
        var $ = go.GraphObject.make;
        let _this = this;

        _this.configData.myDiagram =
          $(go.Diagram, "myDiagramDiv",
            {
              initialContentAlignment: go.Spot.Center,
              allowDrop: true,
              "LinkDrawn": showLinkLabel,
              "LinkRelinked": showLinkLabel,
              scrollsPageOnFocus: false,
              "undoManager.isEnabled": true,
              autoScale: go.Diagram.Uniform
            });

        function nodeStyle() {
          return [
            new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
            {locationSpot: go.Spot.Center}
          ];
        }

        function makePort(name, align, spot, output, input) {
          var horizontal = align.equals(go.Spot.Top) || align.equals(go.Spot.Bottom);
          return $(go.Shape,
            {
              fill: "transparent",
              strokeWidth: 0,
              width: horizontal ? NaN : 8,
              height: !horizontal ? NaN : 8,
              alignment: align,
              stretch: (horizontal ? go.GraphObject.Horizontal : go.GraphObject.Vertical),
              portId: name,
              fromSpot: spot,
              fromLinkable: output,
              toSpot: spot,
              toLinkable: input,
              cursor: "pointer",
              mouseEnter: function (e, port) {
                if (!e.diagram.isReadOnly) port.fill = "rgba(255,0,255,0.5)";
              },
              mouseLeave: function (e, port) {
                port.fill = "transparent";
              }
            });
        }

        function textStyle() {
          return {
            font: "bold 11pt Helvetica, Arial, sans-serif",
            stroke: "whitesmoke",
            editable: true,
            margin: 3
          }
        }

        _this.configData.myDiagram.nodeTemplateMap.add("",
          $(go.Node, "Table", nodeStyle(),
            $(go.Panel, "Auto",
              $(go.Shape, "Rectangle",
                {fill: "#729FDC", maxSize: new go.Size(150, 140)},
                new go.Binding("figure", "figure")),
              $(go.TextBlock, textStyle(),
                {
                  maxSize: new go.Size(140, NaN),
                  wrap: go.TextBlock.WrapFit,
                  editable: true,
                  margin: 5,
                },
                new go.Binding("text").makeTwoWay())
            ),
            makePort("T", go.Spot.Top, go.Spot.TopSide, true, true),
            makePort("L", go.Spot.Left, go.Spot.LeftSide, true, true),
            makePort("R", go.Spot.Right, go.Spot.RightSide, true, true),
            makePort("B", go.Spot.Bottom, go.Spot.BottomSide, true, true)
          ));

        _this.configData.myDiagram.nodeTemplateMap.add("Conditional",
          $(go.Node, "Table", nodeStyle(),
            $(go.Panel, "Auto",
              $(go.Shape, "Diamond",
                {fill: "#729FDC"},
                new go.Binding("figure", "figure")),
              $(go.TextBlock, textStyle(),
                {
                  maxSize: new go.Size(160, NaN),
                  wrap: go.TextBlock.WrapFit,
                  editable: true,
                  margin: new go.Margin(5, 2, 5, 2),
                },
                new go.Binding("text").makeTwoWay())
            ),
            makePort("T", go.Spot.Top, go.Spot.Top, true, true),
            makePort("L", go.Spot.Left, go.Spot.Left, true, true),
            makePort("R", go.Spot.Right, go.Spot.Right, true, true),
            makePort("B", go.Spot.Bottom, go.Spot.Bottom, true, true)
          ));

        _this.configData.myDiagram.nodeTemplateMap.add("Start",
          $(go.Node, "Table", nodeStyle(),
            $(go.Panel, "Auto",
              $(go.Shape, "Circle",
                {minSize: new go.Size(40, 40), fill: "#729FDC"}),
              $(go.TextBlock, "Start", textStyle(),
                new go.Binding("text"))
            ),
            makePort("L", go.Spot.Left, go.Spot.Left, true, true),
            makePort("R", go.Spot.Right, go.Spot.Right, true, true),
            makePort("B", go.Spot.Bottom, go.Spot.Bottom, true, true)
          ));

        _this.configData.myDiagram.nodeTemplateMap.add("End",
          $(go.Node, "Table", nodeStyle(),
            $(go.Panel, "Auto",
              $(go.Shape, "Circle",
                {minSize: new go.Size(40, 40), fill: "#729FDC"}),
              $(go.TextBlock, "End", textStyle(),
                new go.Binding("text"))
            ),
            makePort("L", go.Spot.Left, go.Spot.Left, true, true),
            makePort("R", go.Spot.Right, go.Spot.Right, true, true),
            // makePort("B", go.Spot.Bottom, go.Spot.Bottom, true, true)
            makePort("T", go.Spot.Top, go.Spot.Top, true, true)
          ));


        _this.configData.myDiagram.linkTemplate =
          $(go.Link,
            {
              routing: go.Link.AvoidsNodes,
              curve: go.Link.JumpOver,
              corner: 5, toShortLength: 4,
              relinkableFrom: true,
              relinkableTo: true,
              reshapable: true,
              resegmentable: true,
              mouseEnter: function (e, link) {
                link.findObject("HIGHLIGHT").stroke = "rgba(30,144,255,0.2)";
              },
              mouseLeave: function (e, link) {
                link.findObject("HIGHLIGHT").stroke = "transparent";
              },
              selectionAdorned: false
            },
            new go.Binding("points").makeTwoWay(),
            $(go.Shape,
              {isPanelMain: true, strokeWidth: 8, stroke: "transparent", name: "HIGHLIGHT"}),
            $(go.Shape,
              {isPanelMain: true, stroke: "gray", strokeWidth: 2},
              new go.Binding("stroke", "isSelected", function (sel) {
                return sel ? "dodgerblue" : "gray";
              }).ofObject()),
            $(go.Shape,
              {toArrow: "standard", strokeWidth: 0, fill: "gray"}),
            $(go.Panel, "Auto",
              {visible: false, name: "LABEL", segmentIndex: 2, segmentFraction: 0.5},
              new go.Binding("visible", "visible").makeTwoWay(),
              $(go.Shape, "RoundedRectangle",
                {fill: "#F8F8F8", strokeWidth: 0}),
              $(go.TextBlock, "Yes",
                {
                  textAlign: "center",
                  font: "10pt helvetica, arial, sans-serif",
                  stroke: "#333333",
                  editable: true
                },
                new go.Binding("text").makeTwoWay())
            )
          );

        function showLinkLabel(e) {
          var label = e.subject.findObject("LABEL");
          if (label !== null) label.visible = (e.subject.fromNode.data.category === "Conditional");
        }

        _this.configData.myDiagram.toolManager.linkingTool.temporaryLink.routing = go.Link.Orthogonal;
        _this.configData.myDiagram.toolManager.relinkingTool.temporaryLink.routing = go.Link.Orthogonal;

        _this.configData.myPalette =
          $(go.Palette, "myPaletteDiv",
            {
              scrollsPageOnFocus: false,
              nodeTemplateMap: _this.configData.myDiagram.nodeTemplateMap,
              model: new go.GraphLinksModel([
                {category: "Start", text: "Start"},
                {text: "Step"},
                {category: "Conditional", text: "???"},
                {category: "End", text: "End"}
              ])
            });

        _this.configData.myDiagram.model = go.Model.fromJson(_this.configData.mySaveModel);

      }
    }
  }
</script>

<style scoped>
 #myPaletteDiv{
   width: 100px;
   height: 680px;
   margin-right: 2px;
   background-color: whitesmoke;
   border: 1px solid black
 }
  #myDiagramDiv{
    width: 1010px;
    height: 680px;
    border: 1px solid black;
  }
  .btnSave{
    clear: both;
    margin:20px auto;
    display: block;
    font-size: 14px;
    position: relative;
    top:20px;
    width:150px;
  }
  .o-hidden{
    width: 1112px;
    margin: 20px auto;
  }
</style>

  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
GoJS是一个用于创建交互式流程图、图表和数据可视化的JavaScript库。而Vue是一种用于构建用户界面的渐进式框架。 结合GoJS和Vue可以轻松地创建一个交互式的流程图应用。首先,我们需要在Vue项目中引入GoJS库。可以通过npm安装GoJS并引入它,或者直接在HTML文件中引入GoJS的脚本文件。 接下来,我们可以使用Vue的组件系统来构建流程图的各个组件,例如节点、连线和布等。可以将这些组件定义为Vue的单文件组件,这样可以更好地组织和管理代码。 在Vue组件中,我们可以使用GoJS的API来创建和配置流程图。可以设置节点的属性、样式和事件处理程序等。还可以定义连线的样式和事件,以便用户可以在布上拖拽节点和连线。 当用户对流程图进行更改时,我们可以通过Vue的数据响应机制来更新数据模型。这样,我们可以随时获取到流程图的最新状态,并将其保存或发送到服务器进行进一步处理。 另外,GoJS还提供了许多有用的功能,例如自动布局、图形导出和交互式编辑等。我们可以根据需要使用这些功能来增强我们的流程图应用。 总而言之,使用GoJS和Vue可以轻松地创建交互式的流程图应用。通过结合Vue的组件系统和GoJS的API,我们可以方便地构建和管理流程图的各个组件,并使用Vue的数据响应机制来实现流程图的动态更新。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值