1. 效果展示
2. 使用dagre-d3
需要安装dagre-d3库,d3的流程图库。
3. 代码示例(封装组件)
主要代码讲解:
//得到流程图绘制对象
this.graph = new dagreD3.graphlib.Graph().setGraph({
// 控制方向
rankdir: this.direction
}).setDefaultEdgeLabel(function () { return {} })
//绘制节点
this.graph.setNode()
//绘制连接
this.graph.setEdge()
温馨提示:详细使用可以去官网查询
<template lang="pug">
div.dagre-graph-container(:id="containerId")
div.zoom-div
button(v-for="item in directions" :key="item.prop" @click="direction = item.prop") {
{item.label}}
button.zoom(@click="zoomCtrl('in')") 缩小
button(@click="zoomCtrl('out')") 扩大
svg.dagre
g.container
</template>
<script>
/**
* 流程图
*/
import * as d3 from 'd3'
import dagreD3 from 'dagre-d3'
let container = null
export default {
name: 'DagreGraph',
props: ['nodes', 'edges'],
data() {
return {
id: '',
renderer: null,
graph: null,
direction: 'LR',
directions: [
{
prop: 'LR',
label: '从左至右'
},
{
prop: 'RL',
label: '从右至左'
},
{
prop: 'TB',
label: '从上至下'
},
{
prop: 'BT',
label: '从下至上'
}
],
zoom: null,
containerId: '',
width: 0,
height: 0
}
},
created() {
this.containerId = this.uuid()
this.graph = new dagreD3.graphlib.Graph().setGraph({
rankdir: this.direction
}).setDefaultEdgeLabel(function () { return {} })
},
methods: {
uuid () {
function s4 () {
return Math.floor((1 + Math.random()) * 0