This repository has been archived by the owner on Jul 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual.js
411 lines (332 loc) · 9.26 KB
/
visual.js
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
var width = 1015,
height = 561,
colors = d3.scale.category10();
var svg = d3.select('.graph-container')
.append('svg')
.attr('width',width)
.attr('height',height)
.attr('style', "background-image:url(regions.png);");
// Init the list of nodes and edges with some nodes?
// >>>>>>>>>>>>>>>>
var nodes = [
// {id : 0, x : 100, y : 100 },
// {id : 1, x : 200, y : 200 },
// {id : 2, x : 300, y : 300 }
],
links = [
// {source : nodes[0], target : nodes[1], weight : 1},
// {source : nodes[1], target : nodes[2], weight : 1 }
];
var drag_line = svg.append('svg:path')
.attr('class', 'link dragline hidden')
.attr('d', 'M0,0L0,0');
// <<<<<<<<<<<<<<<<<<<<
var path;
var circle;
var weight;
var selected_node = null,
selected_link = null,
mousedown_link = null,
mousedown_node = null,
mouseup_node = null;
function resetMouseVars() {
mousedown_node = null;
mouseup_node = null;
mousedown_link = null;
}
function restart()
{
reListVertices();
reListEdges();
// redraw everything
svg.selectAll('g').remove();
path = svg.append('svg:g').selectAll('path'),
circle = svg.append('svg:g').selectAll('g');
weight = svg.append('svg:g').selectAll('text');
circle = circle.data(nodes, function(d) { return d.id; });
circle.selectAll('circle')
.style('fill', function(d) { return (d === selected_node) ? d3.rgb(colors(d.id)).brighter().toString() : colors(d.id); });
var g = circle.enter().append('svg:g');
g.append('svg:circle')
.attr('class','node')
.attr('r',12)
.attr('cx', function (d) { return d.x; })
.attr('cy', function (d) { return d.y; })
.style('fill', function(d) { return (d === selected_node) ? d3.rgb(colors(d.id)).brighter().toString() : colors(d.id); })
.style('stroke', function(d) { return d3.rgb(colors(d.id)).darker().toString(); })
.on('mousedown', function(d) {
if (d3.event.ctrlKey) return;
mousedown_node = d;
if (mousedown_node === selected_node) selected_node = null;
else selected_node = mousedown_node;
selected_link = null;
// reposition drag line
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);
restart();
})
.on('mouseup', function(d) {
if (!mousedown_node) return;
drag_line
.classed('hidden', true)
/*
.style('marker-end', '');
*/
// check for drag-to-self
mouseup_node = d;
if(mouseup_node === mousedown_node) { resetMouseVars(); return; }
// add link to graph (update if exists)
// NB: links are strictly source < target; arrows separately specified by booleans
var source, target, direction;
if(mousedown_node.id < mouseup_node.id) {
source = mousedown_node;
target = mouseup_node;
//direction = 'right';
} else {
source = mouseup_node;
target = mousedown_node;
//direction = 'left';
}
var link;
link = links.filter(function(l) {
return (l.source === source && l.target === target);
})[0];
if(link) {
//link[direction] = true;
} else {
var dist = parseInt(Math.sqrt(Math.pow(source.x - target.x,2) + Math.pow(source.y - target.y,2)));
//////////////////////////////////////////////
/// We do not support the edge weights. Remove the command line below
/// to support the edge weights
//////////////////////////////////////////////
dist = 1;
link = {source: source, target: target, weight: dist};
//link[direction] = true;
links.push(link);
}
addEdgeToGraph(mousedown_node, mouseup_node);
// select new link
selected_link = link;
selected_node = null;
restart();
})
;
g.append('svg:text')
.attr('x', function(d) { return d.x; })
.attr('y', function(d) { return d.y; })
//.attr('y', function (d) { return 4; })
.attr('class','id')
.text(function(d) { return d.id; });
//circle.exit().remove();
// drawing paths
path = path.data(links);
path.classed('selected', function(d) { return d === selected_link; });
path.enter().append('svg:path')
.attr('class','link')
.classed('selected',function(d) {return d === selected_link; })
.attr('d', function (d)
{
var deltaX = d.target.x - d.source.x,
deltaY = d.target.y - d.source.y,
dist = Math.sqrt(deltaX * deltaX + deltaY * deltaY),
normX = deltaX / dist,
normY = deltaY / dist,
/*
sourcePadding = d.left ? 17 : 12,
targetPadding = d.right ? 17 : 12,
*/
sourcePadding = 12;
targetPadding = 12;
sourceX = d.source.x + (sourcePadding * normX),
sourceY = d.source.y + (sourcePadding * normY),
targetX = d.target.x - (targetPadding * normX),
targetY = d.target.y - (targetPadding * normY);
return 'M' + sourceX + ',' + sourceY + 'L' + targetX + ',' + targetY;
})
.on('mousedown', function(d) {
if(d3.event.ctrlKey) return;
// select link
mousedown_link = d;
if(mousedown_link === selected_link) selected_link = null;
else selected_link = mousedown_link;
selected_node = null;
restart();
})
;
// start weight display
weight = weight.data(links);
weight.enter().append('svg:text')
.attr('class','weight')
.attr('x', function(d)
{
var x = weightXY(d.source.x,d.source.y,d.target.x,d.target.y).x;
// console.log("X = " + x);
return x;
})
.attr('y', function(d)
{
var y = weightXY(d.source.x,d.source.y,d.target.x,d.target.y).y;
// console.log("Y = " + y);
return y;
})
.text(function(d) { return d.weight; })
;
}
function weightXY(x1,y1,x2,y2)
{
var dist = Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
//console.log(dist);
var x2 = (x1 + x2)/2;
var y2 = (y1 + y2)/2;
if (x1 === x2) return {x : midX + 10, y: midY};
var m1 = (y2 - y1)/(x2-x1);
//console.log(m1);
var c1 = y1 - m1*x1;
//console.log(c1);
var m2 = -1 / m1;
//console.log(m2);
var c2 = y2 - m2*x2;
//console.log(c2);
var d = Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
//console.log(d);
var v = 16;
d = d*d + v*v;
var D = d;
//console.log(D);
var z1 = c2 - y1;
var a = 1 + m2*m2;
var b = 2*m2*z1 - 2*x1;
var c = x1*x1 + z1*z1 - D;
var delta = b*b - 4*a*c;
delta = Math.sqrt(delta);
var x_1 = (-b + delta)/(2*a);
var y_1 = m2*x_1 + c2;
return {x : x_1, y: y_1};
}
function mousedown() {
svg.classed('active', true);
if(d3.event.ctrlKey || mousedown_node || mousedown_link) return;
// insert new node at point
var point = d3.mouse(this);
var node = {id: getColorByCoord(point[0], point[1]), x: point[0], y: point[1]};
if (isNodeOnGraph(node)) {
console.warn("Region with a color #"+node.id+" on graph already");
return;
}
nodes.push(node);
addNodeToGraph(node);
restart();
}
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]);
restart();
}
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 spliceLinksForNode(node) {
var toSplice = links.filter(function(l) {
return (l.source === node || l.target === node);
});
toSplice.map(function(l) {
links.splice(links.indexOf(l), 1);
});
}
var lastKeyDown = -1;
var drag = d3.behavior.drag()
.on("drag", function (d)
{
//console.log("dragging");
var dragTarget = d3.select(this).select('circle');
// console.log(this);
// console.log(dragTarget);
var new_cx, new_cy;
// console.log(d);
dragTarget
.attr("cx", function()
{
new_cx = d3.event.dx + parseInt(dragTarget.attr("cx"));
return new_cx;
})
.attr("cy", function()
{
new_cy = d3.event.dy + parseInt(dragTarget.attr("cy"));
return new_cy;
});
// return;
d.x = new_cx;
d.y = new_cy;
// console.log(d.x + " " + d.y);
addNodeToGraph(d);
restart();
});
function move()
{
}
function keydown() {
d3.event.preventDefault();
//if(lastKeyDown !== -1) return;
lastKeyDown = d3.event.keyCode;
// ctrl
if(d3.event.keyCode === 17) {
circle.call(drag);
svg.classed('ctrl', true);
}
if(!selected_node && !selected_link) return;
switch(d3.event.keyCode) {
//case 8: // backspace
case 46: // delete
if(selected_node)
{
nodes.splice(nodes.indexOf(selected_node), 1);
spliceLinksForNode(selected_node);
} else if(selected_link) {
links.splice(links.indexOf(selected_link), 1);
}
selected_link = null;
selected_node = null;
restart();
break;
case 13: //enter
if (selected_link)
{
var newWeight = prompt("Enter new weight : ");
var idx = links.indexOf(selected_link);
links[idx].weight = newWeight;
}
restart();
break;
}
}
function keyup() {
lastKeyDown = -1;
// ctrl
if(d3.event.keyCode === 17) {
circle
.on('mousedown.drag', null)
.on('touchstart.drag', null);
svg.classed('ctrl', false);
}
}
svg.on('mousedown', mousedown)
.on('mousemove', mousemove)
.on('mouseup', mouseup);
d3.select(window)
.on('keydown',keydown)
.on('keyup',keyup);
restart();