-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11111.html
More file actions
372 lines (341 loc) · 13.4 KB
/
Copy path11111.html
File metadata and controls
372 lines (341 loc) · 13.4 KB
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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>力导图添加删除连线</title>
<!--<script src="lib/d3/3.5.5/d3.min.js"></script>
<script src="jquery-1.11.3.min.js"></script>-->
<script src="jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="d3/d3.v3.min.js"></script>
<style>
svg {
background-color: #FFF;
cursor: default;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
}
path.link {
fill: none;
stroke: #000;
stroke-width: 4px;
cursor: default;
}
path.link.selected {
stroke-dasharray: 10, 2;
}
path.link.dragline {
pointer-events: none;
}
path.link.hidden {
stroke-width: 0;
}
svg:not(.active) path.link {
cursor: pointer;
}
circle {
stroke-width: 1.5px;
cursor: pointer;
}
</style>
</head>
<body>
<button onclick="opens()">关闭连线添加/删除功能(点击连线删除)</button>
<button onclick="p()">打开连线添加</button>
<button onclick="openStart()">关闭/打开力导图效果</button>
<script type="text/javascript">
var mousedown_node = null;
var nodes = [{name: "桂林"},{name: "广州"},{name: "厦门"},
{name: "杭州"},{name: "上海"},{name: "青岛"},
{name: "天津"}];
var edges = [{source:0,target:1},{source:0,target:2},{source:0,target:3},
{source:0,target:4},{source:1,target:2},{source:1,target:5}];
var width = 1000;
var height = 1000;
/*开始绘制画布*/
var svg = d3.select("body").append("svg").attr({
width : width,
height :height
}).style({"border":"1px solid red"});
/*隐藏添加连接线*/
var drag_line = svg.append("svg:path")
.attr({
class : "link dragline hidden",
d : "M0,0L0,0"
});
/*绘制连接线箭头*/
svg.append("svg:defs").append("svg:marker")
.attr('id', 'end-arrow')
.attr('viewBox', '0 -5 10 10')
.attr('refX', 6)
.attr('markerWidth', 3)
.attr('markerHeight', 3)
.attr('orient', 'auto')
.append('svg:path')
.attr('d', 'M0,-5L10,0L0,5')
.attr('fill', '#000');
var force = d3.layout.force()
.nodes(nodes) //指定节点数组
.links(edges) //指定连线数组
.size([width, height]) //指定范围
.linkDistance(150) //指定连线长度
.charge(-400); //相互之间的作用力
force.start(); //开始作用
console.log(nodes);
console.log(edges);
//添加删除连线
var svg_edges = svg.append("g").selectAll("line")
.data(edges)
.enter()
.append("line")
.style("stroke", "#ccc")
.style("stroke-width", 5)
.on('click',dellink);
//连线删除点击事件
function dellink(d){
edges.forEach(function (a,i) {
if(d.source.index == a.source.index && d.target.index == a.target.index){
oko=edges.splice(i,1)
console.log(oko)
}
});
force.start();
svg.select("g").selectAll("line")
.data(edges)
.exit()
.remove();
//可行
svg.selectAll(".mark").filter(function(d){
if(oko[0]==d){
return true
}
}).remove();
//不可行
/* svg.selectAll(".mark")
.data(edges)
.exit()
.remove();*/
}
var color = d3.scale.category20();
//添加节点
var svg_nodes = svg.append("g").selectAll("circle") //添加原点
.data(nodes)
.enter()
.append("circle")
.attr("r","20")
.style("fill",function(d,i){
return color(i);
})
.on("mousedown", function (d){
if(d3.event.ctrlKey) return;
mousedown_node = d;
drag_line .style('marker-end', 'url(#end-arrow)')
.classed('hidden', false)
.attr('d', 'M' + mousedown_node.x + ',' + mousedown_node.y + 'L' + mousedown_node.x + ',' + mousedown_node.y);
})
.on("mouseup",function(d){
if(!mousedown_node) return;
// needed by FF
drag_line
.classed('hidden', true)
.style('marker-end', '');
mouseup_node = d;
if (mouseup_node === mousedown_node) {
resetMouseVars();
return;
}
var linkgo;
edges.filter(function (d) {
if(d.source.index == mousedown_node.index && d.target.index == mouseup_node.index){
return linkgo = 'has'
}
if (d.source.index == mouseup_node.index && d.target.index == mousedown_node.index) {
return linkgo = 'has'
}
})
if(linkgo != 'has'){
edges.push({source: mousedown_node.index, target: mouseup_node.index});
}
force.start();
svg.select('g').selectAll("line")
.data(edges)
.enter()
.append("line")
.style("stroke", "#ccc")
.style("stroke-width", 5)
.on('click',function(d,i){
edges.forEach(function(a,i){
if(d.source.index==a.source.index&&d.target.index== a.target.index){
oko=edges.splice(i,1)
}else{
}
})
force.start();
svg.select('g').selectAll("line")
.data(edges)
.exit()
.remove();
//可行
svg.selectAll(".mark").filter(function(d){
if(oko[0]==d){
return true
}
}).remove();
//不可行
/* svg.selectAll(".mark")
.data(edges)
.exit()
.remove();*/
});
flow()
});
//添加描述节点
var svg_texts = svg.selectAll("text")
.data(nodes)
.enter()
.append("text")
.style("fill","black")
.attr("dx",20)
.attr("dy",8)
.text(function (d) {
return d.name;
});
//绘制力导图动态坐标位置(监控svg很重要)
force.on("tick", function () {//对于每一个时间间隔
//更新连线坐标
d3.selectAll("line").attr("x1", function (d) {
return d.source.x;
})
.attr("y1", function (d) {
return d.source.y;
})
.attr("x2", function (d) {
return d.target.x;
})
.attr("y2", function (d) {
return d.target.y;
});
//更新节点坐标
d3.selectAll('circle').attr("cx", function (d) {
return d.x;
})
.attr("cy", function (d) {
return d.y;
});
//更新文字坐标
svg_texts.attr("x", function (d) {
return d.x;
})
.attr("y", function (d) {
return d.y;
});
/*为路线是从哪里指向哪里 比如 A到B 的动态效果 ↓*/
// svg_mark.attr("transform", function(d) {
// return "translate(" + d.source.x+ "," +d.source.y+ ")";
// });
// mpath.attr("path",function(d){
// return "M0,0L"+(d.target.x-d.source.x)+","+ (d.target.y-d.source.y);
// });
mpath.attr("path",function(d){
return "M"+d.source.x+","+d.source.y+"L"+(d.target.x)+","+ (d.target.y);
});
/*为路线是从哪里指向哪里 比如 A到B 的动态效果 ↑*/
});
svg.on('mousemove', mousemove)
.on('mouseup', mouseup);
function mousemove() {
if (!mousedown_node) return;
// update drag line
drag_line.attr('d', 'M' + mousedown_node.x + ',' + mousedown_node.y + 'L' + d3.mouse(this)[0] + ',' + d3.mouse(this)[1]);
}
function mouseup() {
if (mousedown_node) {
// hide drag line
drag_line
.classed('hidden', true)
.style('marker-end', '');
}
// because :active only works in WebKit?
svg.classed('active', false);
// clear mouse event vars
resetMouseVars();
}
//清空节点连接线
function resetMouseVars() {
mousedown_node = null;
mouseup_node = null;
mousedown_link = null;
}
var mk = null;
//关闭连线添加/删除功能(点击连线删除)
function opens() {
d3.selectAll("circle").call(force.drag)
.on('mousedown', null)
d3.selectAll('line').on('click',null)
}
//打开连线添加
function p() {
d3.selectAll("circle").on('mousedown.drag', null).on('touchstart.drag', null).on('mousedown', mousedDown)
d3.selectAll('line').on('click',dellink)
}
function mousedDown(d) {
if (d3.event.ctrlKey) return;
mousedown_node = d;
drag_line
.style('marker-end', 'url(#end-arrow)')
.classed('hidden', false)
.attr('d', 'M' + mousedown_node.x + ',' + mousedown_node.y + 'L' + mousedown_node.x + ',' + mousedown_node.y);
}
//打开关闭力导图效果
var num;
function openStart() {
if (num == true || num == undefined) {
d3.selectAll("circle").call(force.drag).attr("r", function (d) {
d.fixed = true;
num = false;
return 20;
})
} else {
d3.selectAll("circle").call(force.drag).attr("r", function (d) {
d.fixed = false;
num = true;
return 20;
})
}
}
flow()
//添加力导图流向动态效果 比如 A到B 的动态效果 ↓*/
/* var svg_mark = svg.selectAll("arrows.mark")
.data(edges)
.enter()
.append('path')
.attr('d','M2,2 L10,6 L2,10 L6,6 L2,2')
.style('fill','black')
.style('stroke','black')
.style('stroke-width','4px');
var mpath = svg_mark.append("animateMotion").attr("path",function(d){
return "M0,0L"+(d.target.x-d.source.x)+","+ (d.target.y-d.source.y);
}).attr("dur","3s").attr("begin","0s").attr("repeatCount","indefinite").attr("rotate","auto");*/
/* var mpath = svg_mark.append("animateMotion").attr("path",function(d){
return "m0,0l"+(d.target.x)+","+ (d.target.y);
}).attr("dur","3s").attr("begin","0s").attr("repeatCount","indefinite").attr("rotate","auto");*/
//添加力导图流向动态效果 比如 A到B 的动态效果 ↑*/
function flow(){
var svg_mark = svg.selectAll(".mark")
.data(edges)
.enter()
.append('path')
.attr('class','mark')
.attr('d','M2,2 L10,6 L2,10 L6,6 L2,2')
.style('fill','black')
.style('stroke','black')
.style('stroke-width','4px');
mpath = svg.selectAll(".mark").append("animateMotion").attr("path",function(d){
return "m0,0l"+(d.target.x)+","+ (d.target.y);
}).attr("dur","3s").attr("begin","0s").attr("repeatCount","indefinite").attr("rotate","auto");
}
</script>
</body>
</html>