-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow_chart.jsp
224 lines (221 loc) · 7.5 KB
/
flow_chart.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="charset=GBK" />
<title>work flow test</title>
<style type="text/css">
div{
width: 100%;
height: 100%;
border: 1px solid red;
}
</style>
<script type="application/javascript" src="/js/jquery-1.4.2.min.js"></script>
<script type="application/javascript">
var data;
$(document).ready(function(){
$.ajax({
url:'/flowchart/flowChartAction.do',
dataType:'json',
type:'POST',
data:{'method':'toFlowChart','flowId':'<c:out value="${param.flowId}" />','recordId':'<c:out value="${param.recordId}" />'},
success:function(t){
data = t;
init();
main();
}
});
});
var canvas; //canvas Object
var ctx; //the 2d Context of the canvas
var node_width = 129; //the width of the Node image
var node_height = 46; //the height of the Node image
function init(){
canvas = document.getElementById('canvas');
if(canvas.getContext){
ctx = canvas.getContext('2d');
ctx.font = '15px 宋体';
}
}
//计算图形间的位置关系
function drawHandle(lineId){
var lineObj = data[lineId];
var pic1 = data[lineObj.PREV_ID];
var pic2 = data[lineObj.NEXT_ID];
var gap_x = Math.abs(pic1.POSITION_X - pic2.POSITION_X);
var gap_y = Math.abs(pic1.POSITION_Y - pic2.POSITION_Y);
if(gap_x == 0 && gap_y == 0){ //self loop
var img = new Image();
img.onload = function(){
ctx.drawImage(img,parseFloat(pic1.POSITION_X)+55,parseFloat(pic1.POSITION_Y)+30);
}
img.src = 'loop.gif';
}else{
var xDistance = (node_height/2)*gap_x/gap_y;
var yDistance = (node_width/2)*gap_y/gap_x;
var startX=0, startY=0;
var len = 0;
if(xDistance <= node_width/2 && gap_y != 0){ // the point of intersection on the X-axis
var line1 = Math.abs(parseFloat(pic1.POSITION_X) - parseFloat(pic2.POSITION_X));
var line2 = Math.abs(parseFloat(pic1.POSITION_Y) - parseFloat(pic2.POSITION_Y));
var angle = getAngle(line1, line2);
if(parseFloat(pic1.POSITION_Y) < parseFloat(pic2.POSITION_Y)){
startY = parseFloat(pic1.POSITION_Y) + node_height + 3;
if(parseFloat(pic1.POSITION_X) < parseFloat(pic2.POSITION_X)){
angle = Math.PI/2 - angle;
startX = parseFloat(pic1.POSITION_X) + node_width/2 + xDistance - 5;
}else{
angle = Math.PI/2 + angle;
startX = parseFloat(pic1.POSITION_X) + node_width/2 - xDistance + 5;
}
}else if(parseFloat(pic2.POSITION_Y) < parseFloat(pic1.POSITION_Y)){
startY = parseFloat(pic1.POSITION_Y) - 3;
if(parseFloat(pic1.POSITION_X) < parseFloat(pic2.POSITION_X)){
angle = -Math.PI/2 + angle;
startX = parseFloat(pic1.POSITION_X) + node_width/2 + xDistance - 5;
}else{
angle = -Math.PI/2 - angle;
if(gap_x != 0){
startX = parseFloat(pic1.POSITION_X) + node_width/2 - xDistance + 5;
}else{
startX = parseFloat(pic1.POSITION_X) + node_width/2 - xDistance - 5;
}
}
}
len = getLineLength(line1,line2,'x');
if(isNaN(len)){
len = gap_y - node_height;
}
len = len - 21;
}else if(gap_y != 0){ // the point of intersection on the Y-axis
var line1 = Math.abs(parseFloat(pic1.POSITION_Y) - parseFloat(pic2.POSITION_Y));
var line2 = Math.abs(parseFloat(pic1.POSITION_X) - parseFloat(pic2.POSITION_X));
var angle = getAngle(line1, line2);
if(parseFloat(pic1.POSITION_Y) < parseFloat(pic2.POSITION_Y)){
startY = parseFloat(pic1.POSITION_Y) + node_height/2 + yDistance;
if(parseFloat(pic1.POSITION_X) < parseFloat(pic2.POSITION_X)){
startX = parseFloat(pic1.POSITION_X) + node_width + 3;
startY = startY + 5;
}else{
angle = Math.PI - angle;
startX = parseFloat(pic1.POSITION_X) - 3;
startY = startY - 5;
}
}else{
startY = parseFloat(pic1.POSITION_Y) + node_height/2 - yDistance;
if(parseFloat(pic1.POSITION_X) < parseFloat(pic2.POSITION_X)){
angle = -angle;
startX = parseFloat(pic1.POSITION_X) + node_width + 3;
startY = startY + 5;
}else{
angle = -Math.PI + angle;
startX = parseFloat(pic1.POSITION_X) - node_width - 3;
startY = startY - 5;
}
}
len = getLineLength(line1, line2, 'y');
if(isNaN(len)){
len = gap_x - node_width;
}
len = len - 21;
}else{
if(parseFloat(pic1.POSITION_X) < parseFloat(pic2.POSITION_X)){
startY = parseFloat(pic1.POSITION_Y) + node_height/2 + 3;
startX = parseFloat(pic1.POSITION_X) + node_width + 3;
angle = 0;
}else{
startY = parseFloat(pic2.POSITION_Y) + node_height/2 - 3;
startX = parseFloat(pic1.POSITION_X) - 3;
angle = Math.PI;
}
len = Math.abs(parseFloat(pic1.POSITION_X)-parseFloat(pic2.POSITION_X));
len = len - node_width - 21;
}
console.log(pic2.COLOR);
var arrow = 'line_u.gif';
if(pic2.COLOR != 'red' && pic1.COLOR != 'red')
arrow = 'line_c.gif';
drawLine(angle, len, startX, startY, arrow);
}
}
function drawLine(angle,length,offsetx,offsety,arrow){
var arch = new Image();
arch.onload = function(){
ctx.save();
ctx.translate(offsetx,offsety);
ctx.rotate(angle);
ctx.drawImage(arch,31,0,15,17,length,-8,15,17);
ctx.lineWidth=4;
if(arrow =='line_c.gif'){
ctx.strokeStyle='rgb(255,50,50)';
}else{
ctx.strokeStyle='rgb(70,70,70)';
}
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(length,0);
ctx.stroke();
ctx.restore();
}
arch.src = arrow;
}
//绘制图形函数
function drawGraphics(obj){
if(obj.TYPE == 'picture'){ // draw the node
var img = new Image();
img.onload = function(){
ctx.drawImage(img,obj.POSITION_X,obj.POSITION_Y);
var text_posx = parseFloat(obj.POSITION_X) + (node_width-obj.TEXT.length*15)/2;
var text_posy = parseFloat(obj.POSITION_Y) + 29;
ctx.strokeText(obj.TEXT, text_posx, text_posy);
}
if(obj.COLOR == 'red')
img.src = 'node_u.gif';
else if(obj.COLOR == 'green')
img.src = 'node_f.gif';
else if(obj.COLOR == 'blue')
img.src = 'node_c.gif';
}else{ //draw the line
drawHandle(obj.ID);
}
}
function getAngle(line1, line2){
var angle = Math.atan(line1/line2);
return angle;
}
function getLineLength(line1, line2, point){
var len = 0;
var angle = getAngle(line1, line2);
if(point == 'x'){
var hypotenuse = line1/Math.sin(angle);
var _hypotenuse = node_height/2/line2*hypotenuse;
len = hypotenuse - _hypotenuse * 2;
}else{
var hypotenuse = line1/Math.sin(angle);
var _hypotenuse = node_width/2/line2*hypotenuse;
len = hypotenuse - _hypotenuse * 2;
}
return len;
}
//入口
function main(){
for(var curId in data){
var item = data[curId]; //current picture object
try{
drawGraphics(item);
}catch(err){
}
}
}
</script>
</head>
<body>
<div>
<canvas id="canvas" width="1200" height="650">
the browser is not support canvas
</canvas>
</div>
</body>
</html>