-
Notifications
You must be signed in to change notification settings - Fork 0
/
forced_graph.js
1346 lines (1171 loc) · 40.9 KB
/
forced_graph.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
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Register cb (concept browser) namespace for usage
*/
(function (cb, $, d3, undefined) {
/******************************************************************************************************
* Data source
*****************************************************************************************************/
// cb.data_source = 'data/GIS_RS.json';
// cb.data_source = 'data/GIS_RS_REDUCED.json';
// cb.data_source = 'data/GIS_RS_link_count.json';
// cb.data_source = 'data/GIS_RS_link_and_individuals.json';
// cb.data_source = 'data/GISTBOK.json';
// cb.data_source = 'data/GISTBOK_LTB.json';
// cb.data_source = 'data/RESEARCH_SKILLS.json';
cb.data_source = 'COST.json';
/******************************************************************************************************
* Configuration variables
*****************************************************************************************************/
// Display settings
cb.mapWidth = 2000;
cb.mapWidthDragMargin = cb.mapWidth / 30;
cb.mapHeight = 1000;
cb.mapHeightDragMargin = cb.mapHeight / 20;
// Force settings
cb.collideStrength = 0.6;
cb.collideIterations = 1;
cb.linkStrength = 0.9;
cb.manyBodyStrength = -70;
cb.manyBodyDistanceMin = 20;
cb.manyBodyDistanceMax = 1500;
cb.boundForceStrenght = 80;
cb.linkNodeRadius = 20;
cb.nodeRadiusMargin = 10;
// General settings
cb.drawGrid = false;
cb.drawLinkNodes = false;
cb.zoomExtent = [0.1, 8]; // [min,max] zoom, min is also limited by screen size
/******************************************************************************************************
* Style configuration variables
*****************************************************************************************************/
// Fixed node layout
cb.baseNodeRadius = 8; // Node base radius
cb.extendNodeRatio = 3;
cb.nodeLineWidth = 2;
// Fixed link styles
cb.linkLineWidth = 1;
// Fixed node label layout
cb.minCharCount = 12;
cb.defaultNodeLabelFontSize = 10;
cb.activeNodeLabelLineWidth = 1.5;
cb.defaultNodeLabelFont = cb.defaultNodeLabelFontSize + 'px';
cb.activeNodeLabelFont = 'bold ' + cb.defaultNodeLabelFont;
// Node styles
cb.defaultNodeFillStyle = '';
cb.defaultNodeStrokeStyle = '';
cb.draggedNodeFillStyle = '';
cb.draggedNodeStrokeStyle = '';
cb.fadedNodeFillStyle = '';
cb.fadedNodeStrokeStyle = '';
cb.highlightedNodeFillStyle = '';
cb.highlightedNodeStrokeStyle = '';
// Link styles
cb.linkLineWidth = 1;
cb.defaultLinkStrokeStyle = '#696969';
cb.draggedLinkStrokeStyle = '#333';
cb.fadedLinksStrokeStyle = '#E0E0E0';
cb.highlightedLinkStrokeStyle = cb.draggedLinkStrokeStyle;
// Node label styles
cb.defaultNodeLabelColor = '#000';
cb.activeNodeLabelStrokeStyle = '#fff';
cb.applyStyle = function (style) {
switch (style) {
case 1: {
// Node styles
cb.defaultNodeFillStyle = '#de5356';
cb.defaultNodeStrokeStyle = '#fff';
cb.draggedNodeFillStyle = cb.defaultNodeFillStyle;
cb.draggedNodeStrokeStyle = '#ff2340';
cb.fadedNodeFillStyle = '#bc6d73';
cb.fadedNodeStrokeStyle = '#fff';
cb.highlightedNodeFillStyle = cb.draggedNodeFillStyle;
cb.highlightedNodeStrokeStyle = cb.draggedNodeStrokeStyle;
break;
}
case 2: {
// Node styles
cb.defaultNodeFillStyle = '#75de79';
cb.defaultNodeStrokeStyle = '#fff';
cb.draggedNodeFillStyle = cb.defaultNodeFillStyle;
cb.draggedNodeStrokeStyle = '#1ac321';
cb.fadedNodeFillStyle = '#9ebc9d';
cb.fadedNodeStrokeStyle = '#fff';
cb.highlightedNodeFillStyle = cb.draggedNodeFillStyle;
cb.highlightedNodeStrokeStyle = cb.draggedNodeStrokeStyle;
break;
}
case 3: {
// Node styles
cb.defaultNodeFillStyle = '#a4a5fe';
cb.defaultNodeStrokeStyle = '#fff';
cb.draggedNodeFillStyle = cb.defaultNodeFillStyle;
cb.draggedNodeStrokeStyle = '#1513ff';
cb.fadedNodeFillStyle = '#55557a';
cb.fadedNodeStrokeStyle = '#fff';
cb.highlightedNodeFillStyle = cb.draggedNodeFillStyle;
cb.highlightedNodeStrokeStyle = cb.draggedNodeStrokeStyle;
break;
}
case 4: {
// Node styles
cb.defaultNodeFillStyle = '#deaf6c';
cb.defaultNodeStrokeStyle = '#fff';
cb.draggedNodeFillStyle = cb.defaultNodeFillStyle;
cb.draggedNodeStrokeStyle = '#ff5d00';
cb.fadedNodeFillStyle = '#bcac9b';
cb.fadedNodeStrokeStyle = cb.fadedNodeFillStyle;
cb.highlightedNodeFillStyle = cb.draggedNodeFillStyle;
cb.highlightedNodeStrokeStyle = cb.draggedNodeStrokeStyle;
break;
}
case 0:
/* falls through */
default: {
// Node styles
cb.defaultNodeFillStyle = '#b1ded2';
cb.defaultNodeStrokeStyle = '#fff';
cb.draggedNodeFillStyle = cb.defaultNodeFillStyle;
cb.draggedNodeStrokeStyle = '#2359ff';
cb.fadedNodeFillStyle = '#E6ECE4';
cb.fadedNodeStrokeStyle = '#fff';
cb.highlightedNodeFillStyle = cb.draggedNodeFillStyle;
cb.highlightedNodeStrokeStyle = cb.draggedNodeStrokeStyle;
break;
}
}
if (d3.event && !d3.event.active) cbSimulation.restart();
};
/******************************************************************************************************
* Data types
* Required for JS-hinting
*****************************************************************************************************/
var Types = {};
/**
* Data type description
* @type {{nodes: [*], links: [*]}}
*/
Types.DataType = {
nodes: [
{
nodeName: '',
label: '',
link: '',
numberOfLinks: 0
}
],
links: [
{
source: 0,
target: 0,
relationName: ''
}
]
};
/**
* Node type description
* @type {{index: number, x: number, y: number, vx: number, vy: number, fx: number, fy: number, radius: number, nodeName: string, label: string, expandedLabel: Array, expandedLabelStart: number, link: string, numberOfLinks: number, dragged: boolean, highlighted: boolean, linkNode: boolean}}
*/
Types.NodeType = {
index: 0,
x: 0,
y: 0,
vx: 0,
vy: 0,
fx: 0,
fy: 0,
color: 0,
radius: 0,
nodeName: '',
label: '',
expandedLabel: [],
expandedLabelStart: 0,
link: '',
numberOfLinks: 0,
individuals: '',
dragged: false,
highlighted: false,
linkNode: false
};
/**
* Link type description
* @type {{source: number, target: number, relationName: string}}
*/
Types.LinkType = {
source: 0,
target: 0,
relationName: ''
};
/******************************************************************************************************
* Internal variables
*****************************************************************************************************/
var canvas, context, canvasWidth, canvasHeight, halfCanvasWidth, halfCanvasHeight;
var halfMapWidth = cb.mapWidth / 2, halfMapHeight = cb.mapHeight / 2;
var cbCanvas, cbSimulation, cbGraph, cbZoom, cbTransform = d3.zoomIdentity, cbDrag;
var dragPosY, dragPosX, isDragging = false;
var highlightedNode = null, mouseMoveDisabled = false;
var clickSend = false;
var contextMenuNode = null, lastTransformed;
// Initialize the graph object
cbGraph = {nodes: [], links: [], linkNodes: []};
/******************************************************************************************************
* Exposed functionality (for easy debugging purposes)
*****************************************************************************************************/
cb.getCurrentTransform = function () {
return cbTransform;
};
cb.getSimulation = function () {
return cbSimulation;
};
cb.getGraph = function () {
return cbGraph;
};
/******************************************************************************************************
* Exposed functionality
*****************************************************************************************************/
/**
* Search and focus on a node based on its name
* @param nodeName
*/
cb.searchNode = function (nodeName) {
// Find the node by label
var nodes = cbGraph.nodes.filter(function (node) {
return node.label === nodeName;
});
// If found, move to it
if (nodes.length > 0) {
moveToNode(nodes[0]);
setNodeAsHighlight(nodes[0]);
}
};
/**
* Recenter the viewport
* @param duration
*/
cb.centerView = function (duration) {
// Find current locations of all nodes, and select max
var minX = cb.mapWidth, maxX = 0, minY = cb.mapHeight, maxY = 0;
cbGraph.nodes.map(function (node) {
minX = Math.min(minX, node.x - node.radius);
maxX = Math.max(maxX, node.x + node.radius);
minY = Math.min(minY, node.y - node.radius);
maxY = Math.max(maxY, node.y + node.radius);
});
moveToPosition(minX, maxX, minY, maxY, duration);
};
/******************************************************************************************************
* Utility functions
*****************************************************************************************************/
/**
* Resize the canvas (draw area)
* This should be done on draggable window size changes, or browser window changes
*/
function resizeCanvas() {
// Get container size, and set sizes and zoom extent
var container = $('#graph_container_div');
canvas.width = canvasWidth = container.innerWidth();
canvas.height = canvasHeight = container.innerHeight();
halfCanvasWidth = canvasWidth / 2;
halfCanvasHeight = canvasHeight / 2;
cb.zoomExtent[0] = Math.max(canvasWidth / cb.mapWidth, canvasHeight / cb.mapHeight, 0.1);
// Get context if not available
if (context === undefined) {
context = canvas.getContext('2d');
}
// Check if the event loop is running, if not, restart
if (d3.event && !d3.event.active) cbSimulation.restart();
}
/**
* Retrieve the node radius
* @returns {number}
*/
function getNodeRadius(node) {
// Check whether link node
if (node.linkNode === true) {
node.radius = 1;
return cb.linkNodeRadius;
}
node.radius = cb.baseNodeRadius + cb.extendNodeRatio * (node.numberOfLinks ? parseInt(node.numberOfLinks) : 1);
return node.radius + cb.nodeRadiusMargin;
}
/**
* Limit the node position based on the map dimensions
* @param node
*/
function limitNode(node) {
node.x = Math.max(node.radius, Math.min(cb.mapWidth - node.radius, node.x));
node.y = Math.max(node.radius, Math.min(cb.mapHeight - node.radius, node.y));
}
/**
* Limits the transformation struct, by the map size with a small white margin
* @param transform
* @returns {*}
*/
function limitTransform(transform) {
transform.x =
Math.max(-(((cb.mapWidth + cb.mapWidthDragMargin) * transform.k) - canvasWidth),
Math.min(cb.mapWidthDragMargin * transform.k, transform.x));
transform.y =
Math.max(-(((cb.mapHeight + cb.mapHeightDragMargin) * transform.k) - canvasHeight),
Math.min(cb.mapHeightDragMargin * transform.k, transform.y));
return transform;
}
/**
* Mark the given node as being dragged
* @param node
*/
function setNodeAsDragged(node) {
isDragging = true;
node.dragged = true;
clearNodeHighlight();
setHighlightsByNode(node);
}
/**
* Unmark the given node as being dragged
* @param node
*/
function clearNodeAsDragged(node) {
isDragging = false;
node.dragged = false;
clearHighlightsByNode(node);
}
/**
* Mark the given node as being highlighted
* @param node
*/
function setNodeAsHighlight(node) {
// Check if node the same
if (highlightedNode && highlightedNode.index === node.index) return;
// Check for previous highlight
clearNodeHighlight();
// Set as highlighted
if (node === undefined) return;
highlightedNode = node;
node.highlighted = true;
setHighlightsByNode(node);
}
/**
* Unmark the given node as being highlighted
*/
function clearNodeHighlight() {
if (highlightedNode !== null) {
highlightedNode.highlighted = false;
clearHighlightsByNode(highlightedNode);
highlightedNode = null;
}
}
/**
* Mark relations of the given node as highlighted
* @param node
*/
function setHighlightsByNode(node) {
cbGraph.links.map(function (link) {
if (link.target.index === node.index) link.source.highlighted = true;
if (link.source.index === node.index) link.target.highlighted = true;
});
}
/**
* Unmark relations of the given node as highlighted
* @param node
*/
function clearHighlightsByNode(node) {
cbGraph.links.map(function (link) {
if (link.target.index === node.index) link.source.highlighted = false;
if (link.source.index === node.index) link.target.highlighted = false;
});
}
/**
* Retrieve the link minimum length
* @param {Types.LinkType.} link
* @returns {number}
*/
function getLinkDistance(link) {
return getNodeRadius(link.source) +
getNodeRadius(link.target) +
getLinkLabelLength(link);
}
/**
* Estimate the length of the link label
* @param link
* @returns {number}
*/
function getLinkLabelLength(link) {
return link.relationName.length * 5 + 10;
}
/**
* Transform a location with the current transformation
* @param loc
*/
function transformLocation(loc) {
return {
x: (loc.clientX - cbTransform.x) / cbTransform.k,
y: (loc.clientY - cbTransform.y) / cbTransform.k
};
}
/******************************************************************************************************
* Event handlers
*****************************************************************************************************/
/**
* Find a node based on the event location
* @returns {undefined}
*/
function findNode() {
var transformed;
if (typeof d3.event.clientX === 'undefined' || typeof d3.event.clientY === 'undefined') {
transformed = lastTransformed;
} else {
transformed = lastTransformed = transformLocation(d3.event);
}
var node = cbSimulation.find(transformed.x, transformed.y, 20);
return node && node.linkNode !== true ? node : undefined;
}
/**
* Event fired when the drag action starts
*/
function onDragStarted() {
if (!d3.event.active) cbSimulation.alphaTarget(0.1).restart();
d3.event.subject.fx = dragPosX = d3.event.subject.x;
d3.event.subject.fy = dragPosY = d3.event.subject.y;
mouseMoveDisabled = false;
setNodeAsDragged(d3.event.subject);
}
/**
* Event fired during dragging progress
*/
function onDragged() {
dragPosX += d3.event.dx / cbTransform.k;
dragPosY += d3.event.dy / cbTransform.k;
d3.event.subject.fx = Math.max(0, Math.min(cb.mapWidth, dragPosX));
d3.event.subject.fy = Math.max(0, Math.min(cb.mapHeight, dragPosY));
}
/**
* Event fired when the drag action stops
*/
function onDragEnded() {
if (!d3.event.active) cbSimulation.alphaTarget(0);
d3.event.subject.fx = null;
d3.event.subject.fy = null;
clearNodeAsDragged(d3.event.subject);
}
/**
* Event for mouse move, to select a node to highlight
*/
function onMouseMove() {
if (mouseMoveDisabled) return;
highlightNode(findNode());
}
/**
* Left mouse button click, in order to fix node highlight
* Communicates with the wiki in order to open the correct page
*/
function onClick() {
var node = findNode();
if (node && !mouseMoveDisabled) {
setNodeAsHighlight(node);
}
mouseMoveDisabled = !mouseMoveDisabled;
if (!clickSend && node !== undefined) {
if (typeof node.link !== 'undefined' && node.link !== '') {
clickSend = true;
//noinspection JSCheckFunctionSignatures
parent.postMessage({'type': 'wiki_update', 'data': node.link}, '*');
setTimeout(function () {
clickSend = false;
}, 250);
}
}
}
/**
* Right click event.
* On empty space, shows context menu for styling
*/
function onRightClick() {
d3.event.preventDefault();
var node = findNode();
contextMenuNode = typeof node !== 'undefined' ? node : null;
$('#graph_container_div').contextMenu({x: d3.event.clientX, y: d3.event.clientY});
}
/**
* Double click event, move to the clicked node
*/
function onDoubleClick() {
moveToNode(findNode());
}
/**
* Keyboard event handler
* space -> Stop simulation
*/
function onKeyDown() {
var node = findNode();
switch (d3.event.keyCode) {
case 32: // Space
// Force movement stop with space bar
cbSimulation.stop();
return;
case 73: // I
/* falls through */
case 48: // 0
if (node !== undefined) colorNode(node, 0);
break;
case 82: // R
/* falls through */
case 49: // 1
if (node !== undefined) colorNode(node, 1);
break;
case 71: // G
/* falls through */
case 50: // 2
if (node !== undefined) colorNode(node, 2);
break;
case 66: // B
/* falls through */
case 51: // 3
if (node !== undefined) colorNode(node, 3);
break;
case 79: // O
/* falls through */
case 52: // 4
if (node !== undefined) colorNode(node, 4);
break;
}
// Check if the event loop is running, if not, restart
if (!d3.event.active) cbSimulation.restart();
}
/**
* Highlight the current event location node
* @param node
*/
function highlightNode(node) {
if (node) {
setNodeAsHighlight(node);
} else {
clearNodeHighlight();
}
// Check if the event loop is running, if not, restart
if (!d3.event.active) cbSimulation.restart();
}
/**
* Move the view to the given node
* It keeps the relations inside the view
* @param node
*/
function moveToNode(node) {
// Check for node existence
if (node === undefined) return;
// Stop simulation for now to prevent node walking
mouseMoveDisabled = true;
cbSimulation.stop();
// Set clicked node as highlighted
setNodeAsHighlight(node);
// Find current locations of highlighted nodes
var minX = cb.mapWidth, maxX = 0, minY = cb.mapHeight, maxY = 0;
cbGraph.nodes.map(function (node) {
if (!node.highlighted) return;
minX = Math.min(minX, node.x - node.radius);
maxX = Math.max(maxX, node.x + node.radius);
minY = Math.min(minY, node.y - node.radius);
maxY = Math.max(maxY, node.y + node.radius);
});
// Do the actual move
moveToPosition(minX, maxX, minY, maxY);
}
/**
* Move the view to the given view bounds
* @param minX
* @param maxX
* @param minY
* @param maxY
* @param duration
*/
function moveToPosition(minX, maxX, minY, maxY, duration) {
// Check duration
duration = typeof duration !== 'undefined' ? duration : 3000;
// Calculate scale
var scale = 0.9 / Math.max((maxX - minX) / canvasWidth, (maxY - minY) / canvasHeight);
scale = Math.min(cb.zoomExtent[1], Math.max(1, scale));
// Calculate zoom identify
var transform = d3.zoomIdentity
.translate(halfCanvasWidth, halfCanvasHeight)
.scale(scale)
.translate(-(minX + maxX) / 2, -(minY + maxY) / 2);
// Move to it
cbCanvas
.transition()
.duration(duration)
.call(cbZoom.transform, transform);
}
/**
* Zoom event handler
* Limits the transformation and calls the draw function
*/
function zoomGraph() {
cbTransform = limitTransform(d3.event.transform);
drawGraph();
}
/******************************************************************************************************
* Canvas draw methods
*****************************************************************************************************/
/**
* Draws the complete concept browser, refreshes the view on every iteration
* @note Order in this function is important!
*/
function drawGraph() {
// Limit the nodes
cbGraph.nodes.map(limitNode);
// Save state
context.save();
// Clear canvas
context.clearRect(0, 0, canvasWidth, canvasHeight);
// Adjust scaling
context.translate(cbTransform.x, cbTransform.y);
context.scale(cbTransform.k, cbTransform.k);
// Draw grid lines
if (cb.drawGrid) {
context.beginPath();
for (var i = 0; i <= cb.mapWidth; i += 100) {
context.moveTo(i, 0);
context.lineTo(i, cb.mapHeight);
}
for (var j = 0; j <= cb.mapHeight; j += 100) {
context.moveTo(0, j);
context.lineTo(cb.mapWidth, j);
}
context.strokeStyle = 'black';
context.stroke();
// Draw canvas size rectangle
context.beginPath();
context.moveTo(0, 0);
context.lineTo(canvasWidth, 0);
context.lineTo(canvasWidth, canvasHeight);
context.lineTo(0, canvasHeight);
context.lineTo(0, 0);
context.strokeStyle = 'blue';
context.stroke();
}
//////////////////////
// NORMAL //
//////////////////////
// Draw normal links
context.beginPath();
context.lineWidth = cb.linkLineWidth;
context.strokeStyle = isDragging || highlightedNode !== null ? cb.fadedLinksStrokeStyle : cb.defaultLinkStrokeStyle;
cbGraph.links.map(drawNormalLink);
context.stroke();
// Draw normal nodes
for (var nn = 0; nn <= 4; nn++) {
cb.applyStyle(nn);
context.beginPath();
context.lineWidth = cb.nodeLineWidth;
context.fillStyle = isDragging || highlightedNode !== null ? cb.fadedNodeFillStyle : cb.defaultNodeFillStyle;
context.strokeStyle = isDragging || highlightedNode !== null ? cb.fadedNodeStrokeStyle : cb.defaultNodeStrokeStyle;
cbGraph.nodes.filter(filterNodeOnColor(nn)).map(drawNormalNode);
context.fill();
context.stroke();
}
// Draw link nodes
if (cb.drawLinkNodes) {
context.beginPath();
context.lineWidth = cb.nodeLineWidth;
context.fillStyle = cb.fadedNodeFillStyle;
context.strokeStyle = cb.fadedNodeStrokeStyle;
cbGraph.linkNodes.map(drawNormalNode);
context.fill();
context.stroke();
}
// Draw normal link arrows
context.fillStyle = isDragging || highlightedNode !== null ? cb.fadedLinksStrokeStyle : cb.defaultLinkStrokeStyle;
cbGraph.links.map(drawNormalLinkArrow);
//////////////////////
// DRAGGED //
//////////////////////
// Draw dragged links
if (isDragging) {
context.beginPath();
context.lineWidth = cb.linkLineWidth;
context.strokeStyle = cb.draggedLinkStrokeStyle;
cbGraph.links.map(drawDraggedLink);
context.stroke();
}
// Draw dragged nodes
if (isDragging) {
for (var dn = 0; dn <= 4; dn++) {
cb.applyStyle(dn);
context.beginPath();
context.lineWidth = cb.nodeLineWidth;
context.fillStyle = cb.draggedNodeFillStyle;
context.strokeStyle = cb.draggedNodeStrokeStyle;
cbGraph.nodes.filter(filterNodeOnColor(dn)).map(drawDraggedNode);
context.fill();
context.stroke();
}
}
// Draw dragged link arrows
if (isDragging) {
context.fillStyle = cb.draggedLinkStrokeStyle;
cbGraph.links.map(drawDraggedLinkArrow);
}
//////////////////////
// HIGHLIGHT //
//////////////////////
// Draw highlighted links
if (highlightedNode !== null) {
context.beginPath();
context.lineWidth = cb.linkLineWidth;
context.strokeStyle = cb.highlightedLinkStrokeStyle;
cbGraph.links.map(drawHighlightedLink);
context.stroke();
}
// Draw highlighted nodes
for (var hn = 0; hn <= 4; hn++) {
cb.applyStyle(hn);
context.beginPath();
context.lineWidth = cb.nodeLineWidth;
context.fillStyle = cb.highlightedNodeFillStyle;
context.strokeStyle = cb.highlightedNodeStrokeStyle;
cbGraph.nodes.filter(filterNodeOnColor(hn)).map(drawHighlightedNode);
context.fill();
context.stroke();
}
// Draw highlighted link arrows
if (highlightedNode !== null) {
context.fillStyle = cb.highlightedLinkStrokeStyle;
cbGraph.links.map(drawHighlightedLinkArrow);
}
//////////////////////
// LABELS //
//////////////////////
// Set this lower to prevent horns on M/W letters
// https://github.com/CreateJS/EaselJS/issues/781
context.miterLimit = 2.5;
// Draw link labels
if (isDragging || highlightedNode !== null) {
context.fillStyle = cb.defaultNodeLabelColor;
context.font = cb.defaultNodeLabelFont;
context.textBaseline = 'top';
context.lineWidth = cb.activeNodeLabelLineWidth;
context.strokeStyle = cb.activeNodeLabelStrokeStyle;
cbGraph.links.map(drawLinkText);
}
// Draw node labels
context.fillStyle = cb.defaultNodeLabelColor;
context.font = cb.defaultNodeLabelFont;
context.textBaseline = 'middle';
context.textAlign = 'center';
context.lineWidth = cb.activeNodeLabelLineWidth;
context.strokeStyle = cb.activeNodeLabelStrokeStyle;
cbGraph.nodes.map(drawNodeText);
// Restore state
context.restore();
}
/**
* Draw the link line
* @param link
*/
function drawLink(link) {
context.moveTo(link.target.x, link.target.y);
context.lineTo(link.source.x, link.source.y);
}
/**
* Draw a link when in normal state
* @param link
*/
function drawNormalLink(link) {
if ((link.target.dragged && link.source.dragged) || (link.target.highlighted && link.source.highlighted)) return;
drawLink(link);
}
/**
* Draw a link when in dragged state
* @param link
*/
function drawDraggedLink(link) {
if (link.source.dragged || link.target.dragged) drawLink(link);
}
/**
* Draw a link when in highlight state
* @param link
*/
function drawHighlightedLink(link) {
if (link.source.index === highlightedNode.index || link.target.index === highlightedNode.index) drawLink(link);
}
/**
* Draw the link text
* @param link
*/
function drawLinkText(link) {
// Only draw the text when the link is active
if (link.relationName &&
(!isDragging && (link.source.index === highlightedNode.index || link.target.index === highlightedNode.index)) ||
(link.source.dragged || link.target.dragged)) {
// Calculate angle of label
var startRadians = Math.atan((link.source.y - link.target.y) / (link.source.x - link.target.x));
startRadians += (link.source.x >= link.target.x) ? Math.PI : 0;
// Transform the context
context.save();
context.translate(link.source.x, link.source.y);
context.rotate(startRadians);
// Check rotation and add extra if required
if ((startRadians * 2) > Math.PI) {
context.rotate(Math.PI);
context.textAlign = 'right';
context.strokeText(link.relationName, -(getNodeRadius(link.source) + 5), 0, getLinkLabelLength(link));
context.fillText(link.relationName, -(getNodeRadius(link.source) + 5), 0, getLinkLabelLength(link));
} else {
context.textAlign = 'left';
context.strokeText(link.relationName, getNodeRadius(link.source) + 5, 0, getLinkLabelLength(link));
context.fillText(link.relationName, getNodeRadius(link.source) + 5, 0, getLinkLabelLength(link));
}
// Restore context
context.restore();
}
}
/**
* Draw the link arrow
* @param link
*/
function drawLinkArrow(link) {
// Calculate head rotation
var startRadians = Math.atan((link.source.y - link.target.y) / (link.source.x - link.target.x));
startRadians += ((link.source.x >= link.target.x) ? -1 : 1) * Math.PI / 2;
// Draw the triangle
context.save();
context.beginPath();
context.translate(link.target.x, link.target.y);
context.rotate(startRadians);
context.moveTo(0, link.target.radius - 1);
context.lineTo(3, 9 + link.target.radius);
context.lineTo(-3, 9 + link.target.radius);
context.closePath();
context.restore();
context.fill();
}
/**
* Draw a link arrow when in normal state
* @param link
*/
function drawNormalLinkArrow(link) {
if ((link.target.dragged && link.source.dragged) || (link.target.highlighted && link.source.highlighted)) return;
drawLinkArrow(link);
}
/**
* Draw a link arrow when in dragged state
* @param link
*/
function drawDraggedLinkArrow(link) {
if (link.target.dragged || link.source.dragged) drawLinkArrow(link);
}
/**
* Draw a link arrow when in highlighted state
* @param link
*/
function drawHighlightedLinkArrow(link) {
if (link.target.index === highlightedNode.index || link.source.index === highlightedNode.index) drawLinkArrow(link);
}
/**
* Draw the node
* @param node
*/
function drawNode(node) {
context.moveTo(node.x + node.radius, node.y);
context.arc(node.x, node.y, node.radius, 0, 2 * Math.PI);
}
/**