1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+
4+ < head >
5+ < meta charset ="UTF-8 ">
6+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7+ < meta http-equiv ="X-UA-Compatible " content ="ie=edge ">
8+ < title > 流程图的简单实现</ title >
9+ < script src ="https://cdn.bootcss.com/d3/5.11.0/d3.js "> </ script >
10+ < script src ="https://cdn.bootcss.com/dagre-d3/0.6.3/dagre-d3.js "> </ script >
11+ </ head >
12+
13+ < body >
14+ < div class ="container ">
15+ < svg id ="svg-canvas " width =2000 height =900 >
16+ < g />
17+ </ svg >
18+ </ div >
19+ < script >
20+ // 1. 初始画节点
21+ var state = [
22+ { label : 'project_etl_start\n虚节点' , class : 'type-suss' } ,
23+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
24+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
25+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
26+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
27+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
28+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
29+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
30+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
31+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
32+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
33+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
34+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
35+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' } ,
36+ { label : 'project_etl_start\n虚节点' , class : 'type-TOP' }
37+ ]
38+ var edg = [
39+ { start : 1 , end : 4 , option : { } } ,
40+ { start : 1 , end : 3 , option : { } } ,
41+ { start : 1 , end : 2 , option : { } } ,
42+ { start : 6 , end : 7 , option : { } } ,
43+ { start : 5 , end : 6 , option : { } } ,
44+ { start : 9 , end : 10 , option : { } } ,
45+ { start : 8 , end : 9 , option : { } } ,
46+ { start : 11 , end : 12 , option : { } } ,
47+ { start : 8 , end : 11 , option : { } } ,
48+ { start : 5 , end : 8 , option : { } } ,
49+ { start : 1 , end : 5 , option : { } } ,
50+ { start : 13 , end : 14 , option : { } } ,
51+ { start : 1 , end : 13 , option : { } } ,
52+ { start : 0 , end : 1 , option : { } }
53+ ]
54+ // 2. 创建对象graph。
55+ var g = new dagreD3 . graphlib . Graph ( )
56+ . setGraph ( { } )
57+ . setDefaultEdgeLabel ( function ( ) { return { } ; } ) ;
58+ // 3. 声明并添加树节点和边。
59+
60+ var drawNode = function ( ) {
61+ for ( let i in state ) { //画点
62+ let el = state [ i ]
63+ g . setNode ( i , {
64+ id : i ,
65+ label : el . label ,
66+ class : el . class ,
67+ style : "stroke: #000000; fill: #ffffff;" ,
68+ } ) ;
69+ }
70+ }
71+ var drawEdg = function ( ) {
72+ for ( let i in edg ) { // 画连线
73+ let el = edg [ i ]
74+ g . setEdge ( el . start , el . end , {
75+ style : "stroke: #0fb2cc; fill: none;" ,
76+ arrowheadStyle : "fill: #0fb2cc;stroke: #0fb2cc;" ,
77+ arrowhead : 'vee'
78+ } ) ;
79+ }
80+ }
81+
82+ // 4. 渲染dag图。
83+
84+ var render = new dagreD3 . render ( ) ;
85+ var svg = d3 . select ( "#svgCanvas" ) ; //声明节点
86+ svg . select ( "g" ) . remove ( ) ; //删除以前的节点,清空画面
87+ var svgGroup = svg . append ( "g" ) ;
88+ var inner = svg . select ( "g" ) ;
89+ var zoom = d3 . zoom ( ) . on ( "zoom" , function ( ) { //添加鼠标滚轮放大缩小事件
90+ inner . attr ( "transform" , d3 . event . transform ) ;
91+ } ) ;
92+ svg . call ( zoom ) ;
93+ this . drawNode ( ) ; //画点
94+ this . drawEdg ( ) ; // 画连线
95+ render ( d3 . select ( "svg g" ) , this . g ) ; //渲染节点
96+ let max = svg . _groups [ 0 ] [ 0 ] . clientWidth > svg . _groups [ 0 ] [ 0 ] . clientHeight ? svg . _groups [ 0 ] [ 0 ] . clientWidth : svg . _groups [ 0 ] [ 0 ] . clientHeight ;
97+ var initialScale = max / 779 ; //initialScale元素放大倍数,随着父元素宽高发生变化时改变初始渲染大小
98+ var tWidth = ( svg . _groups [ 0 ] [ 0 ] . clientWidth - this . g . graph ( ) . width * initialScale ) / 2 ; //水平居中
99+ var tHeight = ( svg . _groups [ 0 ] [ 0 ] . clientHeight - this . g . graph ( ) . height * initialScale ) / 2 ; //垂直居中
100+ svg . call ( zoom . transform , d3 . zoomIdentity . translate ( tWidth , tHeight ) . scale ( initialScale ) ) ; //元素水平垂直居中
101+ // 5. 添加鼠标右键事件, 样式如图:
102+
103+ //主要代码如下:
104+ var svgCanvas = document . getElementById ( 'svg-canvas' ) ; //svg
105+ var myMenu = document . getElementById ( "myMenu" ) ; //右键菜单
106+
107+ svgCanvas . addEventListener ( 'mouseover' , function ( e ) { //监听鼠标右键
108+ e . preventDefault ( ) ;
109+ if ( e . target . tagName === 'rect' ) {
110+ myMenu . style . top = event . clientY + "px" ; //获取鼠标位置
111+ myMenu . style . left = event . clientX + "px" ;
112+ myMenu . style . display = 'block' ; //显示相应右键内容
113+ }
114+ } )
115+ document . addEventListener ( "click" , ( event ) => {
116+ myMenu . style . display = 'none' ;
117+ } ) ;
118+ </ script >
119+ </ body >
120+
121+ </ html >
0 commit comments