GOJS+VUE实现流程图效果
前⾔及展⽰
在项⽬中需要根据传过来的数据画出流程图,采⽤了GOJS插件,功能很全⾯,可以根据⾃⼰的需要调整,不过建议简单的流程图还是⾃⼰⼿写⼀个组件,更加便于维护和变换。有⼀点需要注意的是,GOJS是需要收费的,有⽔印,虽然可以⼿动去掉,但是公司⽤的话还是需要买。GOJS的官⽹上有关于在VUE中应⽤GOJS的⼩例⼦:。推荐看⼀下,可以解决⼤部分简单需求,这个例⼦可以满⾜你并⾏步骤数⽐较固定的⼆叉树画法的流程图。
这是官⽹的例⼦,其中模块,线,箭头等画布元素都可以交互。
由于我的并⾏步骤数不固定,于是在图中加⼊了Group(组)。先展⽰⼀下成品:
其中批次中可以包含多个项⽬,表⽰并⾏的步骤。
具体实现
分为两个⽂件:
diagram.vue && stepMap.vuediagram.vue声明组件,stepMap引⽤
diagram.vue
基本声明:
<script>
import go from 'gojs';
let $ = go.GraphObject.make; // 后⾯很多⽤到该变量来初始化diagram
export default{
name: 'diagram',
props: ['modelData'], // accept model data as a parameter
data() {
return {
diagram: null,
};
}, // provide access to the GoJS Diagram
初始化diagram:
mounted: function() {
let self = this;
let myDiagram =
$(go.Diagram, this.$el,
{
'initialContentAlignment': go.Spot.Center,
'isEnabled': false, // 是否可拖拽,默认为是
// 'useWheelBehavior': go.ToolManager.WheelNone,
'allowLink': false,
'allowMove': false,
'allowRelink': false, // 由于项⽬只想展⽰数据,我禁⽤了⼤部分图像交互操作,具体可参看官⽹API
'layout': $(go.TreeLayout, {angle: 0, arrangement: go.TreeLayout.ArrangementHorizontal}),  // angle可控制图像展⽰⽅向
'undoManager.isEnabled': true,
// Model ChangedEvents get passed up to component users
'ChangedSelection': function(e) {
self.$emit('changed-selection', e);
},
});
$(go.Node, 'Auto',
$(go.Shape, // 节点形状设置
{
fill: 'white', strokeWidth: 1,
portId: '', fromLinkable: true, toLinkable: true, cursor: 'pointer',
},
new go.Binding('fill', '', deColorConverter)), // nodeColorConverter是我⾃定义函数,根据节点状态设置节点的背景颜⾊  $(go.TextBlock, // 节点提⽰⽂字设置
{margin: 16, editable: false},
new go.Binding('text').makeTwoWay())
);
myDiagram.linkTemplate =
$(go.Link,
{relinkableFrom: true, relinkableTo: true},
$(go.Shape, // 连线形状设置
{strokeWidth: 2},
new go.Binding('stroke', '', this.linkColorConverter)), // 连线的颜⾊设置
$(go.Shape, // arrowhead
{toArrow: 'Triangle', stroke: null, scale: 1.5}, // 箭头设置
new go.Binding('fill', '', this.linkColorConverter))
);
$(go.Group, 'Auto',
{ // define the group's internal layout
layout: $(go.TreeLayout,
{angle: 90, arrangement: go.TreeLayout.ArrangementVertical, isRealtime: false}),
// the group begins unexpanded;
// upon expansion, a Diagram Listener will generate contents for the group
/
/ when a group is expanded, if it contains no parts, generate a subGraph inside of it
// subGraphExpandedChanged: function(group) {
// if (unt === 0) {
//  randomGroup(group.data.key);
// }
// },
},
$(go.Shape, 'Rectangle',
{fill: null, stroke: 'gray', strokeWidth: 2}),
$(go.Panel, 'Vertical',
{defaultAlignment: go.Spot.Left, margin: 4},
$(go.Panel, 'Horizontal',
{defaultAlignment: go.Spot.Top},
$('SubGraphExpanderButton', {alignment: go.Spot.Top, margin: 5}),
// the SubGraphExpanderButton is a panel that functions as a button to expand or collapse the subGraph
$(go.TextBlock,
{
font: 'Bold 14px Sans-Serif',
margin: 10,
},
new go.Binding('text', 'text'))
),
// create a placeholder to represent the area where the contents of the group are
$(go.Placeholder,
{padding: new go.Margin(0, 10)}),
) // end Vertical Panel
); // end Group
// generate the initial model
this.diagram = myDiagram;
this.delData);
更新图中数据时需要的函数:
watch: {
modelData: function(val) {
this.updateModel(val);
},
},
vuejs流程图插件methods: {
model: function() {
return del;
},
updateModel: function(val) {
// No GoJS transaction permitted when del.
if (val instanceof go.Model) {
del = val;
} else {
let m = new go.GraphLinksModel();
if (val) {
for (let p in val) {
if (val[p]) {
m[p] = val[p];
}
}
}
del = m;
}
},
updateDiagramFromData: function() {
this.diagram.startTransaction();
// This is very general but very inefficient.
// It would be better to modify the diagramData data by calling
// Model.setDataProperty or Model.addNodeData, et al.
this.diagram.updateAllRelationshipsFromData();
this.diagram.updateAllTargetBindings();
this.diagrammitTransaction('updated');
},
},
};
</script>
声明后在stepMap调⽤,⽐较重要的是这两个⽅法:
updateDiagramFromData: function() {
this.$refs.diag.updateDiagramFromData(); // 数据变化时调⽤组件中的更新⽅法
},
changedSelection: function(e) {
let node = e.diagram.selection.first();
if (node instanceof go.Node) {
this.currentNode = node;
this.currentNodeText = ;
this.selectNode(node.data);
} else {
this.currentNode = null;
this.currentNodeText = '';
}
},
最后,将需要展⽰的数据转化为需要的格式就可以啦。
流程图所需格式如下:
⽆分组:
"nodeDataArray": [
{"key":1, "text":"Alpha", "color":"lightblue"},
{"key":2, "text":"Beta", "color":"orange"},
{"key":3, "text":"Gamma", "color":"lightgreen"},
{"key":4, "text":"Delta", "color":"pink"}
]
"linkDataArray": [
{"from":1, "to":2},
{"from":1, "to":3},
{"from":3, "to":4}
]
有分组:
var nodeDataArray = [
{ key: "Alpha" },
{ key: "Beta", group: "Omega" },
{ key: "Gamma", group: "Omega" },
{ key: "Omega", isGroup: true },
{ key: "Delta" }
];
var linkDataArray = [
{ from: "Alpha", to: "Beta" },
{ from: "Beta", to: "Gamma" },
{ from: "Omega", to: "Delta" }
];
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。