-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo5.html
802 lines (733 loc) · 28.3 KB
/
demo5.html
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
<!DOCTYPE html>
<html style="overflow: hidden;">
<head>
<meta charset="UTF-8">
<title>流程设计工具 d3.js</title>
<link href="https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet">
<!--<link rel="stylesheet" type="text/css" href="index.css">-->
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdn.bootcss.com/d3/4.11.0/d3.min.js"></script>
<script src="https://cdn.bootcss.com/d3-transform/1.0.4/d3-transform.min.js"></script>
<style>
.left-wrapper {
width: 250px;
position: absolute;
top: 0px;
bottom: 0px;
left: 0px;
background: #f0f0f0;
border-right: solid 1px #e7e7e7;
}
.middle-wrapper {
position: absolute;
top: 0px;
bottom: 0px;
left: 250px;
right: 250px;
}
.right-wrapper {
position: absolute;
width: 250px;
top: 0px;
bottom: 0px;
right: 0px;
background: #f5f5f5;
border-left: solid 1px #e7e7e7;
}
.sidebar-nav,
.sidebar-nav ul {
list-style: none;
padding: 0px;
}
.sidebar-nav > li:nth-child(odd),
.sidebar-nav ul li:nth-child(odd) {
background: #f0f0f0;
}
.sidebar-nav > li:nth-child(even),
.sidebar-nav ul > li:nth-child(even) {
background: #fff;
}
.sidebar-nav li {
text-indent: 4px;
line-height: 30px;
}
.sidebar-nav li li {
text-indent: 15px;
}
.sidebar-nav li li li {
text-indent: 25px;
}
.sidebar-nav li a {
display: block;
text-decoration: none;
color: #333;
}
.sidebar-nav li.active a {
background-color: #3e99ff;
}
.sidebar-nav li a > i + span {
margin-left: 3px;
}
.sidebar-nav li a:hover {
text-decoration: none;
background: rgba(255, 255, 255, 0.2);
}
.sidebar-nav li a:active, .sidebar-nav li a:focus {
text-decoration: none;
}
.middle-wrapper h4,
.right-wrapper h4 {
font-size: 1em;
height: 40px;
border-bottom: solid 1px #e7e7e7;
text-align: center;
line-height: 40px
}
.bpmn .node rect {
width: 180px;
height: 36px;
cursor: pointer;
stroke: #333;
stroke-width: 2;
fill: #fff;
}
.bpmn .node.active rect,
.bpmn .node.active circle {
stroke: lightblue;
}
.bpmn .node circle {
stroke: #333;
stroke-width: 2px;
fill: #fff;
cursor: crosshair;
}
.bpmn .node circle.end {
fill: orange;
}
.bpmn .cable {
stroke: #333;
stroke-width: 2px;
fill: none;
cursor: pointer;
}
.bpmn .deleteLine {
cursor: pointer;
}
</style>
</head>
<body>
<div class="container-fuild">
<!--左侧栏-->
<div id="left-wrapper" class="left-wrapper">
<ul class="sidebar-nav">
<li>
<a class="open">
<i class="fa fa-folder-o"></i>
<span>源/目标</span>
</a>
<ul>
<li class="node" data-id="101">
<a href="">
<i class="fa fa-database"></i>
<span>读数据</span>
</a>
</li>
<li class="node" data-id="102">
<a href="">
<i class="fa fa-database"></i>
<span>写数据</span>
</a>
</li>
</ul>
</li>
<li>
<a>
<i class="fa fa-folder-o"></i>
<span>数据预处理</span>
</a>
<ul>
<li class="node" data-id="211">
<a href="">
<i class="fa fa-crosshairs" aria-hidden="true"></i>
<span>类型转换</span>
</a>
</li>
<li class="node" data-id="212">
<a href="">
<i class="fa fa-crosshairs" aria-hidden="true"></i>
<span>拆分</span>
</a>
</li>
<li class="node" data-id="213">
<a href="">
<i class="fa fa-crosshairs" aria-hidden="true"></i>
<span>缺失值填充</span>
</a>
</li>
<li class="node" data-id="214">
<a href="">
<i class="fa fa-crosshairs" aria-hidden="true"></i>
<span>归一化</span>
</a>
</li>
<li class="node" data-id="215">
<a href="">
<i class="fa fa-crosshairs" aria-hidden="true"></i>
<span>标准化</span>
</a>
</li>
</ul>
</li>
<li>
<a>
<i class="fa fa-folder-o"></i>
<span>特征工程</span>
</a>
</li>
<li>
<a>
<i class="fa fa-folder-o"></i>
<span>机器学习</span>
</a>
<ul>
<li>
<a>
<i class="fa fa-folder-o"></i>
<span>二分类</span>
</a>
<ul>
<li class="node">
<a href="">
<i class="fa fa-circle-o"></i>
<span>GBDT二分类</span>
</a>
</li>
<li class="node">
<a href="">
<i class="fa fa-circle-o"></i>
<span>PS-SMART</span>
</a>
</li>
<li class="node">
<a href="">
<i class="fa fa-circle-o"></i>
<span>线性支持向量机</span>
</a>
</li>
<li class="node">
<a href="">
<i class="fa fa-circle-o"></i>
<span>逻辑回归二分类</span>
</a>
</li>
</ul>
</li>
<li>
<a>
<i class="fa fa-folder-o"></i>
<span>聚类</span>
</a>
<ul>
<li class="node">
<a href="">
<i class="fa fa-circle-o"></i>
<span>K均值聚类</span>
</a>
</li>
</ul>
</li>
<li>
<a>
<i class="fa fa-folder-o"></i>
<span>回归</span>
</a>
<ul>
<li class="node">
<a href="">
<i class="fa fa-circle-o"></i>
<span>GBDT回归</span>
</a>
</li>
<li class="node">
<a href="">
<i class="fa fa-circle-o"></i>
<span>线性回归</span>
</a>
</li>
<li class="node">
<a href="">
<i class="fa fa-circle-o"></i>
<span>PS_SMART回归</span>
</a>
</li>
<li class="node">
<a href="">
<i class="fa fa-circle-o"></i>
<span>PS线性回归</span>
</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div class="middle-wrapper">
<h4>实验名称</h4>
<div id="idsw-bpmn" class="bpmn" style="position: relative; width: 100%; height: 100%;">
<svg width="100%" height="100%">
<defs>
<marker id="arrowhead" viewBox="0 0 10 10" refX="10" refY="5" markerWidth="6" markerHeight="6"
orient="auto">
<path d="M 0 0 L 10 5 L 0 10 z" stroke-width="0" stroke="#333"></path>
</marker>
<pattern id="deleteLine" width="100%" height="100%" patternContentUnits="objectBoundingBox">
<image width="1" height="1" xlink:href="./delete.png"/>
</pattern>
</defs>
</svg>
</div>
<div style="height: 40px; border-top: solid 1px #e7e7e7; text-align: center; line-height: 40px; position: absolute;bottom: 2px; width: 100%">
<a class="btn btn-link" href="#"><i class="fa fa-play-circle-o" aria-hidden="true"></i> 运行</a>
<a class="btn btn-link" href="#"><i class="fa fa-cloud-upload" aria-hidden="true"></i> 部署</a>
<a class="btn btn-link" href="#"><i class="fa fa-share-alt" aria-hidden="true"></i> 分享</a>
</div>
</div>
<div class="right-wrapper">
<h4>实验属性</h4>
</div>
</div>
</body>
<script>
//最终的数据集
var workflow = {
nodes: [],
lines: []
};
//初始化渲染
function newTreeChart(svg,data) {
data.nodes.forEach(function (item, i) {
//添加节点
addNode(svg,item);
});
data.lines.forEach(function (item, i) {
//添加线
addLine(svg,item,data.nodes);
});
// addLine(svg,data.lines);
}
//生成线
function addLine(svg, line,nodes) {
svg.append("path")
.attr("class", "cable")
.attr("from", line.startId)
.attr("to", line.endId)
.attr("marker-end", "url(#arrowhead)")
.attr("d", line.d)
.attr("id", line.id)
.attr("start", line.start)
.attr("end", line.end);
}
$(function () {
var svg = d3.select("svg");
// 绑定拖拽
$('#left-wrapper .node').draggable({
helper: "clone",
addClass: false,
connectToSortable: "#idsw-bpmn",
start: function (e, ui) {
console.log("start: ", ui);
ui.helper.addClass("ui-draggable-helper");
},
stop: function (e, ui) {
console.log("stop: ", ui);
var node = {
id: new Date().getTime(),
dataId: ui.helper.attr('data-id'),
x: ui.position.left - 250,
y: ui.position.top - 40,
text: ui.helper.text(),
childrenMsg: {},
inputs: 1,
outputs: 2
};
if (node.dataId == 101) {
node.inputs = 0;
node.outputs = 1;
} else if (node.dataId == 102) {
node.inputs = 1;
node.outputs = 0;
} else {
node.inputs = 1;
node.outputs = 1;
}
// 记录在数据集中
workflow.nodes.push(node);
var g = addNode(svg, node);
// g.call(
// d3.drag()
// .on("start", dragstarted)
// .on("drag", dragged)
// .on("end", dragended)
// );
//
// g.selectAll("circle.output").call(
// d3.drag()
// .on("start", linestarted)
// .on("drag", linedragged)
// .on("end", lineended)
// );
//
// g.selectAll("circle.input")
// .on("mouseover", function () {
// if (drawLine) {
//
// //判断线数组中fromid=入口
// var inLineLength = workflow.lines.filter(function (en) {
// return en.endId == d3.select(this.parentNode).attr("id");
// }.bind(this)).length;
// if (inLineLength >= 1) {
// alert("入口只能连一条线");
// activeLine.remove();
// return false;
// }
// d3.selectAll("circle.end").classed("end", false);
// d3.select(this).classed("end", true);
//
//
// }
// });
console.log("workflow:", workflow);
}
});
//根据数据集转换成图
var testData = {
"nodes": [{
"id": 1554803426987,
"dataId": "101",
"x": 538,
"y": 26,
"text": "\n \n \n 读数据\n \n ",
"childrenMsg": {
"left": "1554803428446"
},
"inputs": 0,
"outputs": 1
}, {
"id": 1554803428446,
"dataId": "102",
"x": 570,
"y": 145,
"text": "\n \n \n 写数据\n \n ",
"childrenMsg": {},
"inputs": 1,
"outputs": 0
}],
"lines": [{
"id": "1554803430741",
"d": "M598,72 C598,108.5 658,108.5 658,145",
"start": "60, 46",
"end": "90, 0",
"startId": "1554803426987",
"startDataId": "101",
"endId": "1554803428446",
"endDataId": "102"
}]
};
newTreeChart(svg, testData);
});
var activeLine = null;
var points = [];
var translate = null;
var drawLine = false;
function linestarted() {
drawLine = false;
// 当前选中的circle
var anchor = d3.select(this);
// 当前选中的节点
var node = d3.select(this.parentNode);
//判断线数组中fromid=入口
var outLineLength = workflow.lines.filter(function (en) {
return en.startId == node.attr("id");
}).length;
if (outLineLength >= 2) {
alert("只能连两条线");
return false;
}
var rect = node.node().getBoundingClientRect();
var dx = 0;
if (anchor.attr("outputPos") == 0) {
dx = rect.width / 3;
} else {
dx = rect.width * 2 / 3;
}
var dy = rect.height;
var transform = node.attr("transform");
translate = getTranslate(transform);
points.push([dx + translate[0], dy + translate[1]]);
activeLine = d3.select("svg")
.append("path")
.attr("class", "cable")
.attr("from", node.attr("id"))
.attr("start", dx + ", " + dy)
.attr("output", d3.select(this).attr("outputPos"))
.attr("marker-end", "url(#arrowhead)");
}
function linedragged() {
drawLine = true;
points[1] = [d3.event.x + translate[0], d3.event.y + translate[1]];
activeLine.attr("d", function () {
return "M" + points[0][0] + "," + points[0][1]
+ " C" + points[0][0] + "," + (points[0][1] + points[1][1]) / 2
+ " " + points[1][0] + "," + (points[0][1] + points[1][1]) / 2
+ " " + points[1][0] + "," + points[1][1];
});
}
function lineended() {
drawLine = false;
var anchor = d3.selectAll("circle.end");
if (anchor.empty()) {
activeLine.remove();
} else {
var pNode = d3.select(anchor.node().parentNode);
var input = pNode.node().getBoundingClientRect().width / 2;
anchor.classed("end", false);
activeLine.attr("to", pNode.attr("id"));
activeLine.attr("input", anchor.attr("input"));
activeLine.attr("end", input + ", 0");
activeLine.attr("id", new Date().getTime());
//记录节点输出的个数
var startNode = d3.select(this.parentNode);
for (var i = 0, len = workflow.nodes.length; i < len; i++) {
if (workflow.nodes[i].id == startNode.attr("id")) {
if (d3.select(this).attr("outputPos") == 0) {
workflow.nodes[i].childrenMsg.left = pNode.attr("id");
} else {
workflow.nodes[i].childrenMsg.right = pNode.attr("id");
}
}
}
workflow.lines.push({
id: activeLine.attr("id"),//线的id
d: activeLine.attr("d"),//线的路径
start:activeLine.attr("start"),
end:activeLine.attr("end"),
startId: startNode.attr("id"),//起始节点id
startDataId: startNode.attr("data-id"),//起始节点data-id
endId: pNode.attr("id"),//结束节点id
endDataId: pNode.attr("data-id")//结束节点data-id
});
console.log("workflow", JSON.stringify(workflow));
//点击线出现删除按钮
activeLine.on("click", function (e) {
var pathParam = d3.select(this).attr("d").split(" ");
var x = (parseFloat(pathParam[0].split(",")[0].substr(1)) + parseFloat(pathParam[2].split(",")[0])) / 2;
var y = pathParam[2].split(",")[1];
d3.select("svg")
.append("circle")
.attr("class", "deleteLine")
.attr("cx", x)
.attr("cy", y)
.attr("r", 10)
.attr("stroke", "#000")
.attr("lineId", d3.select(this).attr("id"))
.attr("fill", "url(#deleteLine)")
.on("click", function () {
var lineId = d3.select(this).attr("lineId");
d3.select("path[id='" + lineId + "']").remove();
this.remove();
//删除对应数
for (var i = 0, len = workflow.lines.length; i < len; i++) {
if (workflow.lines[i].id == lineId) {
workflow.lines.splice(i, 1);
break;
}
}
console.log("workflow:", workflow);
d3.event.stopPropagation();
});
d3.event.stopPropagation();
});
}
activeLine = null;
points.length = 0;
translate = null;
}
function getTranslate(transform) {
var arr = transform.substring(transform.indexOf("(") + 1, transform.indexOf(")")).split(",");
return [+arr[0], +arr[1]];
}
//点击body去除浮动按钮
d3.select("svg").on("click", function () {
d3.select(".deleteLine").remove();
});
var dx = 0;
var dy = 0;
var dragElem = null;
function dragstarted() {
var transform = d3.select(this).attr("transform");
var translate = getTranslate(transform);
dx = d3.event.x - translate[0];
dy = d3.event.y - translate[1];
dragElem = d3.select(this);
//清空删除线按钮
d3.select(".deleteLine").remove();
}
function dragged() {
dragElem.attr("transform", "translate(" + (d3.event.x - dx) + ", " + (d3.event.y - dy) + ")");
updateCable(dragElem);
}
function updateCable(elem) {
var bound = elem.node().getBoundingClientRect();
var width = bound.width;
var height = bound.height;
var id = elem.attr("id");
var transform = elem.attr("transform");
var t1 = getTranslate(transform);
// 更新输出线的位置
d3.selectAll('path[from="' + id + '"]')
.each(function () {
var start = d3.select(this).attr("start").split(",");
console.log("start[0]:", start[0]);
start[0] = +start[0] + t1[0];
start[1] = +start[1] + t1[1];
var path = d3.select(this).attr("d");
var end = path.substring(path.lastIndexOf(" ") + 1).split(",");
end[0] = +end[0];
end[1] = +end[1];
d3.select(this).attr("d", function () {
return "M" + start[0] + "," + start[1]
+ " C" + start[0] + "," + (start[1] + end[1]) / 2
+ " " + end[0] + "," + (start[1] + end[1]) / 2
+ " " + end[0] + "," + end[1];
});
});
// 更新输入线的位置
d3.selectAll('path[to="' + id + '"]')
.each(function () {
var path = d3.select(this).attr("d");
var start = path.substring(1, path.indexOf("C")).split(",");
start[0] = +start[0];
start[1] = +start[1];
var end = d3.select(this).attr("end").split(",");
end[0] = +end[0] + t1[0];
end[1] = +end[1] + t1[1];
d3.select(this).attr("d", function () {
return "M" + start[0] + "," + start[1]
+ " C" + start[0] + "," + (start[1] + end[1]) / 2
+ " " + end[0] + "," + (start[1] + end[1]) / 2
+ " " + end[0] + "," + end[1];
});
});
}
function dragended() {
dx = dy = 0;
dragElem = null;
}
/**
* 增加节点
* @param svg
* @param node
* @returns {*}
*/
function addNode(svg, node) {
var g = svg.append("g")
.attr("class", "node")
.attr("data-id", node.dataId)
.attr("id", node.id)
.attr("transform", 'translate(' + node.x + ', ' + node.y + ')');
var rect = g.append("rect")
.attr("rx", 5)
.attr("ry", 5)
.attr("stroke-width", 2)
.attr("stroke", "#333")
.attr("fill", "#fff");
var bound = rect.node().getBoundingClientRect();
var width = bound.width;
var height = bound.height;
// text 中间文本
g.append("text")
.text(node.text)
.attr("x", width / 2)
.attr("y", height / 2)
.attr("dominant-baseline", "central")
.attr("text-anchor", "middle");
// left icon
g.append('text')
.attr("x", 18)
.attr("y", height / 2)
.attr("dominant-baseline", "central")
.attr("text-anchor", "middle")
.attr('font-family', 'FontAwesome')
.text('\uf1c0');
// right icon
g.append('text')
.attr("x", width - 18)
.attr("y", height / 2)
.attr("dominant-baseline", "central")
.attr("text-anchor", "middle")
.attr('font-family', 'FontAwesome')
.text('\uf00c');
// input circle 输入标识
var inputs = node.inputs || 0;
// g.attr("inputs", inputs);
g.append("circle")
.attr("class", "input")
.attr("input", 1)
.attr("cx", width / 2)
.attr("cy", 0)
.attr("r", 5);
// output circle 输出标识
var outputs = node.outputs || 0;
// g.attr("outputs", outputs);
for (i = 0; i < outputs; i++) {
g.append("circle")
.attr("outputPos", 0) //outputPos : 1:左 2:右
.attr("class", "output leftOutput")
.attr("cx", width / 3)
.attr("cy", height)
.attr("r", 5);
g.append("circle")
.attr("outputPos", 1)
.attr("class", "output rightOutput")
.attr("cx", width * 2 / 3)
.attr("cy", height)
.attr("r", 5);
}
g.call(
d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
);
g.selectAll("circle.output").call(
d3.drag()
.on("start", linestarted)
.on("drag", linedragged)
.on("end", lineended)
);
g.selectAll("circle.input")
.on("mouseover", function () {
if (drawLine) {
//判断线数组中fromid=入口
var inLineLength = workflow.lines.filter(function (en) {
return en.endId == d3.select(this.parentNode).attr("id");
}.bind(this)).length;
if (inLineLength >= 1) {
alert("入口只能连一条线");
activeLine.remove();
return false;
}
d3.selectAll("circle.end").classed("end", false);
d3.select(this).classed("end", true);
}
});
return g;
}
</script>
</html>