-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathd3-org-chart.js
1909 lines (1678 loc) · 86.8 KB
/
d3-org-chart.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
import { selection, select } from "d3-selection";
import { max, min, sum, cumsum } from "d3-array";
import { tree, stratify } from "d3-hierarchy";
import { zoom, zoomIdentity } from "d3-zoom";
import { flextree } from 'd3-flextree';
import { linkHorizontal } from 'd3-shape';
const d3 = {
selection,
select,
max,
min,
sum,
cumsum,
tree,
stratify,
zoom,
zoomIdentity,
linkHorizontal,
flextree
}
export class OrgChart {
constructor() {
// Exposed variables test test
const attrs = {
/* NOT INTENDED FOR PUBLIC OVERRIDE */
id: `ID${Math.floor(Math.random() * 1000000)}`, // Id for event handlings
firstDraw: true, // Whether chart is drawn for the first time
ctx: document.createElement('canvas').getContext('2d'),
initialExpandLevel: 1,
nodeDefaultBackground: 'none',
lastTransform: { x: 0, y: 0, k: 1 }, // Panning and zooming values
allowedNodesCount: {},
zoomBehavior: null,
generateRoot: null,
/* INTENDED FOR PUBLIC OVERRIDE */
svgWidth: 800, // Configure svg width
svgHeight: window.innerHeight - 100, // Configure svg height
container: "body", // Set parent container, either CSS style selector or DOM element
data: null, // Set data, it must be an array of objects, where hierarchy is clearly defined via id and parent ID (property names are configurable)
connections: [], // Sets connection data, array of objects, SAMPLE: [{from:"145",to:"201",label:"Conflicts of interest"}]
defaultFont: "Helvetica", // Set default font
nodeId: d => d.nodeId || d.id, // Configure accessor for node id, default is either odeId or id
parentNodeId: d => d.parentNodeId || d.parentId, // Configure accessor for parent node id, default is either parentNodeId or parentId
rootMargin: 40, // Configure how much root node is offset from top
nodeWidth: d3Node => 250, // Configure each node width, use with caution, it is better to have the same value set for all nodes
nodeHeight: d => 150, // Configure each node height, use with caution, it is better to have the same value set for all nodes
neighbourMargin: (n1, n2) => 80, // Configure margin between two nodes, use with caution, it is better to have the same value set for all nodes
siblingsMargin: d3Node => 20, // Configure margin between two siblings, use with caution, it is better to have the same value set for all nodes
childrenMargin: d => 60, // Configure margin between parent and children, use with caution, it is better to have the same value set for all nodes
compactMarginPair: d => 100, // Configure margin between two nodes in compact mode, use with caution, it is better to have the same value set for all nodes
compactMarginBetween: (d3Node => 20), // Configure margin between two nodes in compact mode, use with caution, it is better to have the same value set for all nodes
nodeButtonWidth: d => 40, // Configure expand & collapse button width
nodeButtonHeight: d => 40, // Configure expand & collapse button height
nodeButtonX: d => -20, // Configure expand & collapse button x position
nodeButtonY: d => -20, // Configure expand & collapse button y position
linkYOffset: 30, // When correcting links which is not working for safari
pagingStep: d => 5, // Configure how many nodes to show when making new nodes appear
minPagingVisibleNodes: d => 2000, // Configure minimum number of visible nodes , after which paging button appears
scaleExtent: [0.001, 20], // Configure zoom scale extent , if you don't want any kind of zooming, set it to [1,1]
duration: 400, // Configure duration of transitions
imageName: 'Chart', // Configure exported PNG and SVG image name
setActiveNodeCentered: true, // Configure if active node should be centered when expanded and collapsed
layout: "top",// Configure layout direction , possible values are "top", "left", "right", "bottom"
compact: true, // Configure if compact mode is enabled , when enabled, nodes are shown in compact positions, instead of horizontal spread
createZoom: d => d3.zoom(),
onZoomStart: e => { }, // Callback for zoom & panning start
onZoom: e => { }, // Callback for zoom & panning
onZoomEnd: e => { }, // Callback for zoom & panning end
onNodeClick: (d) => d, // Callback for node click
onExpandOrCollapse: (d) => d, // Callback for node expand or collapse
/*
* Node HTML content generation , remember that you can access some helper methods:
* node=> node.data - to access node's original data
* node=> node.leaves() - to access node's leaves
* node=> node.descendants() - to access node's descendants
* node=> node.children - to access node's children
* node=> node.parent - to access node's parent
* node=> node.depth - to access node's depth
* node=> node.hierarchyHeight - to access node's hierarchy height ( Height, which d3 assigns to hierarchy nodes)
* node=> node.height - to access node's height
* node=> node.width - to access node's width
*
* You can also access additional properties to style your node:
*
* d=>d.data._centeredWithDescendants - when node is centered with descendants
* d=>d.data._directSubordinatesPaging - subordinates count in paging mode
* d=>d.data._directSubordinates - subordinates count
* d=>d.data._totalSubordinates - total subordinates count
* d=>d._highlighted - when node is highlighted
* d=>d._upToTheRootHighlighted - when node is highlighted up to the root
* d=>d._expanded - when node is expanded
* d=>d.data._centered - when node is centered
*/
nodeContent: d => `<div style="padding:5px;font-size:10px;">Sample Node(id=${d.id}), override using <br/>
<code>chart.nodeContent({data}=>{ <br/>
return '' // Custom HTML <br/>
})</code>
<br/>
Or check different <a href="https://github.com/bumbeishvili/org-chart#jump-to-examples" target="_blank">layout examples</a>
</div>`,
/* Node expand & collapse button content and styling. You can access same helper methods as above */
buttonContent: ({ node, state }) => {
const icons = {
"left": d => d ?
`<div style="display:flex;"><span style="align-items:center;display:flex;"><svg width="8" height="8" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M14.283 3.50094L6.51 11.4749C6.37348 11.615 6.29707 11.8029 6.29707 11.9984C6.29707 12.194 6.37348 12.3819 6.51 12.5219L14.283 20.4989C14.3466 20.5643 14.4226 20.6162 14.5066 20.6516C14.5906 20.6871 14.6808 20.7053 14.772 20.7053C14.8632 20.7053 14.9534 20.6871 15.0374 20.6516C15.1214 20.6162 15.1974 20.5643 15.261 20.4989C15.3918 20.365 15.4651 20.1852 15.4651 19.9979C15.4651 19.8107 15.3918 19.6309 15.261 19.4969L7.9515 11.9984L15.261 4.50144C15.3914 4.36756 15.4643 4.18807 15.4643 4.00119C15.4643 3.81431 15.3914 3.63482 15.261 3.50094C15.1974 3.43563 15.1214 3.38371 15.0374 3.34827C14.9534 3.31282 14.8632 3.29456 14.772 3.29456C14.6808 3.29456 14.5906 3.31282 14.5066 3.34827C14.4226 3.38371 14.3466 3.43563 14.283 3.50094V3.50094Z" fill="#716E7B" stroke="#716E7B"/>
</svg></span><span style="color:#716E7B">${node.data._directSubordinatesPaging} </span></div>` :
`<div style="display:flex;"><span style="align-items:center;display:flex;"><svg width="8" height="8" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7.989 3.49944C7.85817 3.63339 7.78492 3.8132 7.78492 4.00044C7.78492 4.18768 7.85817 4.36749 7.989 4.50144L15.2985 11.9999L7.989 19.4969C7.85817 19.6309 7.78492 19.8107 7.78492 19.9979C7.78492 20.1852 7.85817 20.365 7.989 20.4989C8.05259 20.5643 8.12863 20.6162 8.21261 20.6516C8.2966 20.6871 8.38684 20.7053 8.478 20.7053C8.56916 20.7053 8.6594 20.6871 8.74338 20.6516C8.82737 20.6162 8.90341 20.5643 8.967 20.4989L16.74 12.5234C16.8765 12.3834 16.9529 12.1955 16.9529 11.9999C16.9529 11.8044 16.8765 11.6165 16.74 11.4764L8.967 3.50094C8.90341 3.43563 8.82737 3.38371 8.74338 3.34827C8.6594 3.31282 8.56916 3.29456 8.478 3.29456C8.38684 3.29456 8.2966 3.31282 8.21261 3.34827C8.12863 3.38371 8.05259 3.43563 7.989 3.50094V3.49944Z" fill="#716E7B" stroke="#716E7B"/>
</svg></span><span style="color:#716E7B">${node.data._directSubordinatesPaging} </span></div>`
,
"bottom": d => d ? `<div style="display:flex;"><span style="align-items:center;display:flex;"><svg width="8" height="8" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M19.497 7.98903L12 15.297L4.503 7.98903C4.36905 7.85819 4.18924 7.78495 4.002 7.78495C3.81476 7.78495 3.63495 7.85819 3.501 7.98903C3.43614 8.05257 3.38462 8.12842 3.34944 8.21213C3.31427 8.29584 3.29615 8.38573 3.29615 8.47653C3.29615 8.56733 3.31427 8.65721 3.34944 8.74092C3.38462 8.82463 3.43614 8.90048 3.501 8.96403L11.4765 16.74C11.6166 16.8765 11.8044 16.953 12 16.953C12.1956 16.953 12.3834 16.8765 12.5235 16.74L20.499 8.96553C20.5643 8.90193 20.6162 8.8259 20.6517 8.74191C20.6871 8.65792 20.7054 8.56769 20.7054 8.47653C20.7054 8.38537 20.6871 8.29513 20.6517 8.21114C20.6162 8.12715 20.5643 8.05112 20.499 7.98753C20.3651 7.85669 20.1852 7.78345 19.998 7.78345C19.8108 7.78345 19.6309 7.85669 19.497 7.98753V7.98903Z" fill="#716E7B" stroke="#716E7B"/>
</svg></span><span style="margin-left:1px;color:#716E7B" >${node.data._directSubordinatesPaging} </span></div>
` : `<div style="display:flex;"><span style="align-items:center;display:flex;"><svg width="8" height="8" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M11.457 8.07005L3.49199 16.4296C3.35903 16.569 3.28485 16.7543 3.28485 16.9471C3.28485 17.1398 3.35903 17.3251 3.49199 17.4646L3.50099 17.4736C3.56545 17.5414 3.64304 17.5954 3.72904 17.6324C3.81504 17.6693 3.90765 17.6883 4.00124 17.6883C4.09483 17.6883 4.18745 17.6693 4.27344 17.6324C4.35944 17.5954 4.43703 17.5414 4.50149 17.4736L12.0015 9.60155L19.4985 17.4736C19.563 17.5414 19.6405 17.5954 19.7265 17.6324C19.8125 17.6693 19.9052 17.6883 19.9987 17.6883C20.0923 17.6883 20.1849 17.6693 20.2709 17.6324C20.3569 17.5954 20.4345 17.5414 20.499 17.4736L20.508 17.4646C20.641 17.3251 20.7151 17.1398 20.7151 16.9471C20.7151 16.7543 20.641 16.569 20.508 16.4296L12.543 8.07005C12.4729 7.99653 12.3887 7.93801 12.2954 7.89801C12.202 7.85802 12.1015 7.8374 12 7.8374C11.8984 7.8374 11.798 7.85802 11.7046 7.89801C11.6113 7.93801 11.527 7.99653 11.457 8.07005Z" fill="#716E7B" stroke="#716E7B"/>
</svg></span><span style="margin-left:1px;color:#716E7B" >${node.data._directSubordinatesPaging} </span></div>
`,
"right": d => d ? `<div style="display:flex;"><span style="align-items:center;display:flex;"><svg width="8" height="8" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7.989 3.49944C7.85817 3.63339 7.78492 3.8132 7.78492 4.00044C7.78492 4.18768 7.85817 4.36749 7.989 4.50144L15.2985 11.9999L7.989 19.4969C7.85817 19.6309 7.78492 19.8107 7.78492 19.9979C7.78492 20.1852 7.85817 20.365 7.989 20.4989C8.05259 20.5643 8.12863 20.6162 8.21261 20.6516C8.2966 20.6871 8.38684 20.7053 8.478 20.7053C8.56916 20.7053 8.6594 20.6871 8.74338 20.6516C8.82737 20.6162 8.90341 20.5643 8.967 20.4989L16.74 12.5234C16.8765 12.3834 16.9529 12.1955 16.9529 11.9999C16.9529 11.8044 16.8765 11.6165 16.74 11.4764L8.967 3.50094C8.90341 3.43563 8.82737 3.38371 8.74338 3.34827C8.6594 3.31282 8.56916 3.29456 8.478 3.29456C8.38684 3.29456 8.2966 3.31282 8.21261 3.34827C8.12863 3.38371 8.05259 3.43563 7.989 3.50094V3.49944Z" fill="#716E7B" stroke="#716E7B"/>
</svg></span><span style="color:#716E7B">${node.data._directSubordinatesPaging} </span></div>` :
`<div style="display:flex;"><span style="align-items:center;display:flex;"><svg width="8" height="8" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M14.283 3.50094L6.51 11.4749C6.37348 11.615 6.29707 11.8029 6.29707 11.9984C6.29707 12.194 6.37348 12.3819 6.51 12.5219L14.283 20.4989C14.3466 20.5643 14.4226 20.6162 14.5066 20.6516C14.5906 20.6871 14.6808 20.7053 14.772 20.7053C14.8632 20.7053 14.9534 20.6871 15.0374 20.6516C15.1214 20.6162 15.1974 20.5643 15.261 20.4989C15.3918 20.365 15.4651 20.1852 15.4651 19.9979C15.4651 19.8107 15.3918 19.6309 15.261 19.4969L7.9515 11.9984L15.261 4.50144C15.3914 4.36756 15.4643 4.18807 15.4643 4.00119C15.4643 3.81431 15.3914 3.63482 15.261 3.50094C15.1974 3.43563 15.1214 3.38371 15.0374 3.34827C14.9534 3.31282 14.8632 3.29456 14.772 3.29456C14.6808 3.29456 14.5906 3.31282 14.5066 3.34827C14.4226 3.38371 14.3466 3.43563 14.283 3.50094V3.50094Z" fill="#716E7B" stroke="#716E7B"/>
</svg></span><span style="color:#716E7B">${node.data._directSubordinatesPaging} </span></div>`,
"top": d => d ? `<div style="display:flex;"><span style="align-items:center;display:flex;"><svg width="8" height="8" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M11.457 8.07005L3.49199 16.4296C3.35903 16.569 3.28485 16.7543 3.28485 16.9471C3.28485 17.1398 3.35903 17.3251 3.49199 17.4646L3.50099 17.4736C3.56545 17.5414 3.64304 17.5954 3.72904 17.6324C3.81504 17.6693 3.90765 17.6883 4.00124 17.6883C4.09483 17.6883 4.18745 17.6693 4.27344 17.6324C4.35944 17.5954 4.43703 17.5414 4.50149 17.4736L12.0015 9.60155L19.4985 17.4736C19.563 17.5414 19.6405 17.5954 19.7265 17.6324C19.8125 17.6693 19.9052 17.6883 19.9987 17.6883C20.0923 17.6883 20.1849 17.6693 20.2709 17.6324C20.3569 17.5954 20.4345 17.5414 20.499 17.4736L20.508 17.4646C20.641 17.3251 20.7151 17.1398 20.7151 16.9471C20.7151 16.7543 20.641 16.569 20.508 16.4296L12.543 8.07005C12.4729 7.99653 12.3887 7.93801 12.2954 7.89801C12.202 7.85802 12.1015 7.8374 12 7.8374C11.8984 7.8374 11.798 7.85802 11.7046 7.89801C11.6113 7.93801 11.527 7.99653 11.457 8.07005Z" fill="#716E7B" stroke="#716E7B"/>
</svg></span><span style="margin-left:1px;color:#716E7B">${node.data._directSubordinatesPaging} </span></div>
` : `<div style="display:flex;"><span style="align-items:center;display:flex;"><svg width="8" height="8" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M19.497 7.98903L12 15.297L4.503 7.98903C4.36905 7.85819 4.18924 7.78495 4.002 7.78495C3.81476 7.78495 3.63495 7.85819 3.501 7.98903C3.43614 8.05257 3.38462 8.12842 3.34944 8.21213C3.31427 8.29584 3.29615 8.38573 3.29615 8.47653C3.29615 8.56733 3.31427 8.65721 3.34944 8.74092C3.38462 8.82463 3.43614 8.90048 3.501 8.96403L11.4765 16.74C11.6166 16.8765 11.8044 16.953 12 16.953C12.1956 16.953 12.3834 16.8765 12.5235 16.74L20.499 8.96553C20.5643 8.90193 20.6162 8.8259 20.6517 8.74191C20.6871 8.65792 20.7054 8.56769 20.7054 8.47653C20.7054 8.38537 20.6871 8.29513 20.6517 8.21114C20.6162 8.12715 20.5643 8.05112 20.499 7.98753C20.3651 7.85669 20.1852 7.78345 19.998 7.78345C19.8108 7.78345 19.6309 7.85669 19.497 7.98753V7.98903Z" fill="#716E7B" stroke="#716E7B"/>
</svg></span><span style="margin-left:1px;color:#716E7B">${node.data._directSubordinatesPaging} </span></div>
`,
}
return `<div style="border:1px solid #E4E2E9;border-radius:3px;padding:3px;font-size:9px;margin:auto auto;background-color:white"> ${icons[state.layout](node.children)} </div>`
},
/* Node paging button content and styling. You can access same helper methods as above. */
pagingButton: (d, i, arr, state) => {
const step = state.pagingStep(d.parent);
const currentIndex = d.parent.data._pagingStep;
const diff = d.parent.data._directSubordinatesPaging - currentIndex;
const min = Math.min(diff, step);
return `
<div style="margin-top:90px;">
<div style="display:flex;width:170px;border-radius:20px;padding:5px 15px; padding-bottom:4px;;background-color:#E5E9F2">
<div><svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M5.59 7.41L10.18 12L5.59 16.59L7 18L13 12L7 6L5.59 7.41ZM16 6H18V18H16V6Z" fill="#716E7B" stroke="#716E7B"/>
</svg>
</div><div style="line-height:2"> Show next ${min} nodes </div></div>
</div>
`
},
/* You can access and modify actual node DOM element in runtime using this method. */
nodeUpdate: function (d, i, arr) {
d3.select(this)
.select('.node-rect')
.attr("stroke", d => d.data._highlighted || d.data._upToTheRootHighlighted ? '#E27396' : 'none')
.attr("stroke-width", d.data._highlighted || d.data._upToTheRootHighlighted ? 10 : 1)
},
nodeEnter: (d) => d, // Custom handling of node update
nodeExit: (d) => d, // Custom handling of exit node
/* You can access and modify actual link DOM element in runtime using this method. */
linkUpdate: function (d, i, arr) {
d3.select(this)
.attr("stroke", d => d.data._upToTheRootHighlighted ? '#E27396' : '#E4E2E9')
.attr("stroke-width", d => d.data._upToTheRootHighlighted ? 5 : 1)
if (d.data._upToTheRootHighlighted) {
d3.select(this).raise()
}
},
/* Horizontal diagonal generation algorithm - https://observablehq.com/@bumbeishvili/curved-edges-compact-horizontal */
hdiagonal: function (s, t, m) {
// Define source and target x,y coordinates
const x = s.x;
const y = s.y;
const ex = t.x;
const ey = t.y;
let mx = m && m.x != null ? m.x : x; // This is a changed line
let my = m && m.y != null ? m.y : y; // This also is a changed line
// Values in case of top reversed and left reversed diagonals
let xrvs = ex - x < 0 ? -1 : 1;
let yrvs = ey - y < 0 ? -1 : 1;
// Define preferred curve radius
let rdef = 35;
// Reduce curve radius, if source-target x space is smaller
let r = Math.abs(ex - x) / 2 < rdef ? Math.abs(ex - x) / 2 : rdef;
// Further reduce curve radius, is y space is more small
r = Math.abs(ey - y) / 2 < r ? Math.abs(ey - y) / 2 : r;
// Defin width and height of link, excluding radius
let h = Math.abs(ey - y) / 2 - r;
let w = Math.abs(ex - x) / 2 - r;
// Build and return custom arc command
return `
M ${mx} ${my}
L ${mx} ${y}
L ${x} ${y}
L ${x + w * xrvs} ${y}
C ${x + w * xrvs + r * xrvs} ${y}
${x + w * xrvs + r * xrvs} ${y}
${x + w * xrvs + r * xrvs} ${y + r * yrvs}
L ${x + w * xrvs + r * xrvs} ${ey - r * yrvs}
C ${x + w * xrvs + r * xrvs} ${ey}
${x + w * xrvs + r * xrvs} ${ey}
${ex - w * xrvs} ${ey}
L ${ex} ${ey}
`;
},
/* Vertical diagonal generation algorithm - https://observablehq.com/@bumbeishvili/curved-edges-compacty-vertical */
diagonal: function (s, t, m, offsets = { sy: 0, }) {
const x = s.x;
let y = s.y;
const ex = t.x;
const ey = t.y;
let mx = m && m.x != null ? m.x : x; // This is a changed line
let my = m && m.y != null ? m.y : y; // This also is a changed line
let xrvs = ex - x < 0 ? -1 : 1;
let yrvs = ey - y < 0 ? -1 : 1;
y += offsets.sy;
let rdef = 35;
let r = Math.abs(ex - x) / 2 < rdef ? Math.abs(ex - x) / 2 : rdef;
r = Math.abs(ey - y) / 2 < r ? Math.abs(ey - y) / 2 : r;
let h = Math.abs(ey - y) / 2 - r;
let w = Math.abs(ex - x) - r * 2;
//w=0;
const path = `
M ${mx} ${my}
L ${x} ${my}
L ${x} ${y}
L ${x} ${y + h * yrvs}
C ${x} ${y + h * yrvs + r * yrvs} ${x} ${y + h * yrvs + r * yrvs
} ${x + r * xrvs} ${y + h * yrvs + r * yrvs}
L ${x + w * xrvs + r * xrvs} ${y + h * yrvs + r * yrvs}
C ${ex} ${y + h * yrvs + r * yrvs} ${ex} ${y + h * yrvs + r * yrvs
} ${ex} ${ey - h * yrvs}
L ${ex} ${ey}
`;
return path;
},
// Defining arrows with markers for connections
defs: function (state, visibleConnections) {
return `<defs>
${visibleConnections.map(conn => {
const labelWidth = this.getTextWidth(conn.label, { ctx: state.ctx, fontSize: 2, defaultFont: state.defaultFont });
return `
<marker id="${conn.from + "_" + conn.to}" refX="${conn._source.x < conn._target.x ? -7 : 7}" refY="5" markerWidth="500" markerHeight="500" orient="${conn._source.x < conn._target.x ? "auto" : "auto-start-reverse"}" >
<rect rx=0.5 width=${conn.label ? labelWidth + 3 : 0} height=3 y=1 fill="#E27396"></rect>
<text font-size="2px" x=1 fill="white" y=3>${conn.label || ''}</text>
</marker>
<marker id="arrow-${conn.from + "_" + conn.to}" markerWidth="500" markerHeight="500" refY="2" refX="1" orient="${conn._source.x < conn._target.x ? "auto" : "auto-start-reverse"}" >
<path transform="translate(0)" d='M0,0 V4 L2,2 Z' fill='#E27396' />
</marker>
`}).join("")}
</defs>
`},
/* You can update connections with custom styling using this function */
connectionsUpdate: function (d, i, arr) {
d3.select(this)
.attr("stroke", d => '#E27396')
.attr('stroke-linecap', 'round')
.attr("stroke-width", d => '5')
.attr('pointer-events', 'none')
.attr("marker-start", d => `url(#${d.from + "_" + d.to})`)
.attr("marker-end", d => `url(#arrow-${d.from + "_" + d.to})`)
},
// Link generator for connections
linkGroupArc: d3.linkHorizontal().x(d => d.x).y(d => d.y),
/*
* You can customize/offset positions for each node and link by overriding these functions
* For example, suppose you want to move link y position 30 px bellow in top layout. You can do it like this:
* ```javascript
* const layout = chart.layoutBindings();
* layout.top.linkY = node => node.y + 30;
* chart.layoutBindings(layout);
* ```
*/
layoutBindings: {
"left": {
"nodeLeftX": node => 0,
"nodeRightX": node => node.width,
"nodeTopY": node => - node.height / 2,
"nodeBottomY": node => node.height / 2,
"nodeJoinX": node => node.x + node.width,
"nodeJoinY": node => node.y - node.height / 2,
"linkJoinX": node => node.x + node.width,
"linkJoinY": node => node.y,
"linkX": node => node.x,
"linkY": node => node.y,
"linkCompactXStart": node => node.x + node.width / 2,//node.x + (node.compactEven ? node.width / 2 : -node.width / 2),
"linkCompactYStart": node => node.y + (node.compactEven ? node.height / 2 : -node.height / 2),
"compactLinkMidX": (node, state) => node.firstCompactNode.x,// node.firstCompactNode.x + node.firstCompactNode.flexCompactDim[0] / 4 + state.compactMarginPair(node) / 4,
"compactLinkMidY": (node, state) => node.firstCompactNode.y + node.firstCompactNode.flexCompactDim[0] / 4 + state.compactMarginPair(node) / 4,
"linkParentX": node => node.parent.x + node.parent.width,
"linkParentY": node => node.parent.y,
"buttonX": node => node.width,
"buttonY": node => node.height / 2,
"centerTransform": ({ root, rootMargin, centerY, scale, centerX }) => `translate(${rootMargin},${centerY}) scale(${scale})`,
"compactDimension": {
sizeColumn: node => node.height,
sizeRow: node => node.width,
reverse: arr => arr.slice().reverse()
},
"nodeFlexSize": ({ height, width, siblingsMargin, childrenMargin, state, node }) => {
if (state.compact && node.flexCompactDim) {
const result = [node.flexCompactDim[0], node.flexCompactDim[1]]
return result;
};
return [height + siblingsMargin, width + childrenMargin]
},
"zoomTransform": ({ centerY, scale }) => `translate(${0},${centerY}) scale(${scale})`,
"diagonal": this.hdiagonal.bind(this),
"swap": d => { const x = d.x; d.x = d.y; d.y = x; },
"nodeUpdateTransform": ({ x, y, width, height }) => `translate(${x},${y - height / 2})`,
},
"top": {
"nodeLeftX": node => -node.width / 2,
"nodeRightX": node => node.width / 2,
"nodeTopY": node => 0,
"nodeBottomY": node => node.height,
"nodeJoinX": node => node.x - node.width / 2,
"nodeJoinY": node => node.y + node.height,
"linkJoinX": node => node.x,
"linkJoinY": node => node.y + node.height,
"linkCompactXStart": node => node.x + (node.compactEven ? node.width / 2 : -node.width / 2),
"linkCompactYStart": node => node.y + node.height / 2,
"compactLinkMidX": (node, state) => node.firstCompactNode.x + node.firstCompactNode.flexCompactDim[0] / 4 + state.compactMarginPair(node) / 4,
"compactLinkMidY": node => node.firstCompactNode.y,
"compactDimension": {
sizeColumn: node => node.width,
sizeRow: node => node.height,
reverse: arr => arr,
},
"linkX": node => node.x,
"linkY": node => node.y,
"linkParentX": node => node.parent.x,
"linkParentY": node => node.parent.y + node.parent.height,
"buttonX": node => node.width / 2,
"buttonY": node => node.height,
"centerTransform": ({ root, rootMargin, centerY, scale, centerX }) => `translate(${centerX},${rootMargin}) scale(${scale})`,
"nodeFlexSize": ({ height, width, siblingsMargin, childrenMargin, state, node, compactViewIndex }) => {
if (state.compact && node.flexCompactDim) {
const result = [node.flexCompactDim[0], node.flexCompactDim[1]]
return result;
};
return [width + siblingsMargin, height + childrenMargin];
},
"zoomTransform": ({ centerX, scale }) => `translate(${centerX},0}) scale(${scale})`,
"diagonal": this.diagonal.bind(this),
"swap": d => { },
"nodeUpdateTransform": ({ x, y, width, height }) => `translate(${x - width / 2},${y})`,
},
"bottom": {
"nodeLeftX": node => -node.width / 2,
"nodeRightX": node => node.width / 2,
"nodeTopY": node => -node.height,
"nodeBottomY": node => 0,
"nodeJoinX": node => node.x - node.width / 2,
"nodeJoinY": node => node.y - node.height - node.height,
"linkJoinX": node => node.x,
"linkJoinY": node => node.y - node.height,
"linkCompactXStart": node => node.x + (node.compactEven ? node.width / 2 : -node.width / 2),
"linkCompactYStart": node => node.y - node.height / 2,
"compactLinkMidX": (node, state) => node.firstCompactNode.x + node.firstCompactNode.flexCompactDim[0] / 4 + state.compactMarginPair(node) / 4,
"compactLinkMidY": node => node.firstCompactNode.y,
"linkX": node => node.x,
"linkY": node => node.y,
"compactDimension": {
sizeColumn: node => node.width,
sizeRow: node => node.height,
reverse: arr => arr,
},
"linkParentX": node => node.parent.x,
"linkParentY": node => node.parent.y - node.parent.height,
"buttonX": node => node.width / 2,
"buttonY": node => 0,
"centerTransform": ({ root, rootMargin, centerY, scale, centerX, chartHeight }) => `translate(${centerX},${chartHeight - rootMargin}) scale(${scale})`,
"nodeFlexSize": ({ height, width, siblingsMargin, childrenMargin, state, node }) => {
if (state.compact && node.flexCompactDim) {
const result = [node.flexCompactDim[0], node.flexCompactDim[1]]
return result;
};
return [width + siblingsMargin, height + childrenMargin]
},
"zoomTransform": ({ centerX, scale }) => `translate(${centerX},0}) scale(${scale})`,
"diagonal": this.diagonal.bind(this),
"swap": d => { d.y = -d.y; },
"nodeUpdateTransform": ({ x, y, width, height }) => `translate(${x - width / 2},${y - height})`,
},
"right": {
"nodeLeftX": node => -node.width,
"nodeRightX": node => 0,
"nodeTopY": node => - node.height / 2,
"nodeBottomY": node => node.height / 2,
"nodeJoinX": node => node.x - node.width - node.width,
"nodeJoinY": node => node.y - node.height / 2,
"linkJoinX": node => node.x - node.width,
"linkJoinY": node => node.y,
"linkX": node => node.x,
"linkY": node => node.y,
"linkParentX": node => node.parent.x - node.parent.width,
"linkParentY": node => node.parent.y,
"buttonX": node => 0,
"buttonY": node => node.height / 2,
"linkCompactXStart": node => node.x - node.width / 2,//node.x + (node.compactEven ? node.width / 2 : -node.width / 2),
"linkCompactYStart": node => node.y + (node.compactEven ? node.height / 2 : -node.height / 2),
"compactLinkMidX": (node, state) => node.firstCompactNode.x,// node.firstCompactNode.x + node.firstCompactNode.flexCompactDim[0] / 4 + state.compactMarginPair(node) / 4,
"compactLinkMidY": (node, state) => node.firstCompactNode.y + node.firstCompactNode.flexCompactDim[0] / 4 + state.compactMarginPair(node) / 4,
"centerTransform": ({ root, rootMargin, centerY, scale, centerX, chartWidth }) => `translate(${chartWidth - rootMargin},${centerY}) scale(${scale})`,
"nodeFlexSize": ({ height, width, siblingsMargin, childrenMargin, state, node }) => {
if (state.compact && node.flexCompactDim) {
const result = [node.flexCompactDim[0], node.flexCompactDim[1]]
return result;
};
return [height + siblingsMargin, width + childrenMargin]
},
"compactDimension": {
sizeColumn: node => node.height,
sizeRow: node => node.width,
reverse: arr => arr.slice().reverse()
},
"zoomTransform": ({ centerY, scale }) => `translate(${0},${centerY}) scale(${scale})`,
"diagonal": this.hdiagonal.bind(this),
"swap": d => { const x = d.x; d.x = -d.y; d.y = x; },
"nodeUpdateTransform": ({ x, y, width, height }) => `translate(${x - width},${y - height / 2})`,
},
}
};
this.getChartState = () => attrs;
// Dynamically set getter and setter functions for Chart class
Object.keys(attrs).forEach((key) => {
//@ts-ignore
this[key] = function (_) {
if (!arguments.length) {
return attrs[key];
} else {
attrs[key] = _;
}
return this;
};
});
this.initializeEnterExitUpdatePattern();
}
initializeEnterExitUpdatePattern() {
d3.selection.prototype.patternify = function (params) {
var container = this;
var selector = params.selector;
var elementTag = params.tag;
var data = params.data || [selector];
// Pattern in action
var selection = container.selectAll("." + selector).data(data, (d, i) => {
if (typeof d === "object") {
if (d.id) { return d.id; }
}
return i;
});
selection.exit().remove();
selection = selection.enter().append(elementTag).merge(selection);
selection.attr("class", selector);
return selection;
};
}
// This method retrieves passed node's children IDs (including node)
getNodeChildren({ data, children, _children }, nodeStore) {
// Store current node ID
nodeStore.push(data);
// Loop over children and recursively store descendants id (expanded nodes)
if (children) {
children.forEach((d) => {
this.getNodeChildren(d, nodeStore);
});
}
// Loop over _children and recursively store descendants id (collapsed nodes)
if (_children) {
_children.forEach((d) => {
this.getNodeChildren(d, nodeStore);
});
}
// Return result
return nodeStore;
}
// This method can be invoked via chart.setZoomFactor API, it zooms to particulat scale
initialZoom(zoomLevel) {
const attrs = this.getChartState();
attrs.lastTransform.k = zoomLevel;
return this;
}
render() {
//InnerFunctions which will update visuals
const attrs = this.getChartState();
if (!attrs.data || attrs.data.length == 0) {
console.log('ORG CHART - Data is empty');
if (attrs.container) {
select(attrs.container).select('.nodes-wrapper').remove();
select(attrs.container).select('.links-wrapper').remove();
select(attrs.container).select('.connections-wrapper').remove();
}
return this;
}
//Drawing containers
const container = d3.select(attrs.container);
const containerRect = container.node().getBoundingClientRect();
if (containerRect.width > 0) attrs.svgWidth = containerRect.width;
//Calculated properties
const calc = {
id: `ID${Math.floor(Math.random() * 1000000)}`, // id for event handlings,
chartWidth: attrs.svgWidth,
chartHeight: attrs.svgHeight
};
attrs.calc = calc;
// Calculate max node depth (it's needed for layout heights calculation)
calc.centerX = calc.chartWidth / 2;
calc.centerY = calc.chartHeight / 2;
// ******************* BEHAVIORS **********************
if (attrs.firstDraw) {
const behaviors = {
zoom: null
};
// Get zooming function
behaviors.zoom = attrs.createZoom()
.clickDistance(10)
.on('start', (event, d) => attrs.onZoomStart(event))
.on('end', (event, d) => attrs.onZoomEnd(event))
.on("zoom", (event, d) => {
attrs.onZoom(event);
this.zoomed(event, d);
})
.scaleExtent(attrs.scaleExtent)
attrs.zoomBehavior = behaviors.zoom;
}
//****************** ROOT node work ************************
attrs.flexTreeLayout = flextree({
nodeSize: node => {
const width = attrs.nodeWidth(node);;
const height = attrs.nodeHeight(node);
const siblingsMargin = attrs.siblingsMargin(node)
const childrenMargin = attrs.childrenMargin(node);
return attrs.layoutBindings[attrs.layout].nodeFlexSize({
state: attrs,
node: node,
width,
height,
siblingsMargin,
childrenMargin
});
}
})
.spacing((nodeA, nodeB) => nodeA.parent == nodeB.parent ? 0 : attrs.neighbourMargin(nodeA, nodeB));
this.setLayouts({ expandNodesFirst: false });
// ************************* DRAWING **************************
//Add svg
const svg = container
.patternify({
tag: "svg",
selector: "svg-chart-container"
})
.attr("width", attrs.svgWidth)
.attr("height", attrs.svgHeight)
.attr("font-family", attrs.defaultFont)
if (attrs.firstDraw) {
svg.call(attrs.zoomBehavior)
.on("dblclick.zoom", null)
.attr("cursor", "move")
}
attrs.svg = svg;
//Add container g element
const chart = svg
.patternify({
tag: "g",
selector: "chart"
})
// Add one more container g element, for better positioning controls
attrs.centerG = chart
.patternify({
tag: "g",
selector: "center-group"
})
attrs.linksWrapper = attrs.centerG.patternify({
tag: "g",
selector: "links-wrapper"
})
attrs.nodesWrapper = attrs.centerG.patternify({
tag: "g",
selector: "nodes-wrapper"
})
attrs.connectionsWrapper = attrs.centerG.patternify({
tag: "g",
selector: "connections-wrapper"
})
attrs.defsWrapper = svg.patternify({
tag: "g",
selector: "defs-wrapper"
})
if (attrs.firstDraw) {
attrs.centerG.attr("transform", () => {
return attrs.layoutBindings[attrs.layout].centerTransform({
centerX: calc.centerX,
centerY: calc.centerY,
scale: attrs.lastTransform.k,
rootMargin: attrs.rootMargin,
root: attrs.root,
chartHeight: calc.chartHeight,
chartWidth: calc.chartWidth
})
});
}
attrs.chart = chart;
// Display tree contenrs
this.update(attrs.root);
//######################################### UTIL FUNCS ##################################
// This function restyles foreign object elements ()
d3.select(window).on(`resize.${attrs.id}`, () => {
const containerRect = d3.select(attrs.container).node().getBoundingClientRect();
attrs.svg.attr('width', containerRect.width)
});
if (attrs.firstDraw) {
attrs.firstDraw = false;
}
return this;
}
// This function can be invoked via chart.addNode API, and it adds node in tree at runtime
addNode(obj) {
const attrs = this.getChartState();
if (obj && (attrs.parentNodeId(obj) == null || attrs.parentNodeId(obj) == attrs.nodeId(obj)) && attrs.data.length == 0) {
attrs.data.push(obj);
this.render()
return this;
}
const root = attrs.generateRoot(attrs.data)
const descendants = root.descendants();
const nodeFound = descendants.filter(({ data }) => attrs.nodeId(data).toString() === attrs.nodeId(obj).toString())[0];
const parentFound = descendants.filter(({ data }) => attrs.nodeId(data).toString() === attrs.parentNodeId(obj).toString())[0];
if (nodeFound) {
console.log(`ORG CHART - ADD - Node with id "${attrs.nodeId(obj)}" already exists in tree`)
return this;
}
if (obj._centered && !obj._expanded) obj._expanded = true;
attrs.data.push(obj);
// Update state of nodes and redraw graph
this.updateNodesState();
return this;
}
// This function can be invoked via chart.removeNode API, and it removes node from tree at runtime
removeNode(nodeId) {
const attrs = this.getChartState();
const root = attrs.generateRoot(attrs.data)
const descendants = root.descendants();
const node = descendants.filter(({ data }) => attrs.nodeId(data) == nodeId)[0];
if (!node) {
console.log(`ORG CHART - REMOVE - Node with id "${nodeId}" not found in the tree`);
return this;
}
// Get all node descendants
const nodeDescendants = node.descendants()
// Mark all node children and node itself for removal
nodeDescendants
.forEach(d => d.data._filteredOut = true)
// Filter out retrieved nodes and reassign data
attrs.data = attrs.data.filter(d => !d._filteredOut);
if (attrs.data.length == 0) {
this.render();
} else {
const updateNodesState = this.updateNodesState.bind(this);
// Update state of nodes and redraw graph
updateNodesState();
}
return this;
}
groupBy(array, accessor, aggegator) {
const grouped = {}
array.forEach(item => {
const key = accessor(item)
if (!grouped[key]) {
grouped[key] = []
}
grouped[key].push(item)
})
Object.keys(grouped).forEach(key => {
grouped[key] = aggegator(grouped[key])
})
return Object.entries(grouped);
}
calculateCompactFlexDimensions(root) {
const attrs = this.getChartState();
root.eachBefore(node => {
node.firstCompact = null;
node.compactEven = null;
node.flexCompactDim = null;
node.firstCompactNode = null;
})
root.eachBefore(node => {
if (node.children && node.children.length > 1) {
const compactChildren = node.children
.filter(d => !d.children)
if (compactChildren.length < 2) return;
compactChildren.forEach((child, i) => {
if (!i) child.firstCompact = true;
if (i % 2) child.compactEven = false;
else child.compactEven = true;
child.row = Math.floor(i / 2);
})
const evenMaxColumnDimension = d3.max(compactChildren.filter(d => d.compactEven), attrs.layoutBindings[attrs.layout].compactDimension.sizeColumn);
const oddMaxColumnDimension = d3.max(compactChildren.filter(d => !d.compactEven), attrs.layoutBindings[attrs.layout].compactDimension.sizeColumn);
const columnSize = Math.max(evenMaxColumnDimension, oddMaxColumnDimension) * 2;
const rowsMapNew = this.groupBy(compactChildren, d => d.row, reducedGroup => d3.max(reducedGroup, d => attrs.layoutBindings[attrs.layout].compactDimension.sizeRow(d) + attrs.compactMarginBetween(d)));
const rowSize = d3.sum(rowsMapNew.map(v => v[1]))
compactChildren.forEach(node => {
node.firstCompactNode = compactChildren[0];
if (node.firstCompact) {
node.flexCompactDim = [
columnSize + attrs.compactMarginPair(node),
rowSize - attrs.compactMarginBetween(node)
];
} else {
node.flexCompactDim = [0, 0];
}
})
node.flexCompactDim = null;
}
})
}
calculateCompactFlexPositions(root) {
const attrs = this.getChartState();
root.eachBefore(node => {
if (node.children) {
const compactChildren = node.children.filter(d => d.flexCompactDim);
const fch = compactChildren[0];
if (!fch) return;
compactChildren.forEach((child, i, arr) => {
if (i == 0) fch.x -= fch.flexCompactDim[0] / 2;
if (i & i % 2 - 1) child.x = fch.x + fch.flexCompactDim[0] * 0.25 - attrs.compactMarginPair(child) / 4;
else if (i) child.x = fch.x + fch.flexCompactDim[0] * 0.75 + attrs.compactMarginPair(child) / 4;
})
const centerX = fch.x + fch.flexCompactDim[0] * 0.5;
fch.x = fch.x + fch.flexCompactDim[0] * 0.25 - attrs.compactMarginPair(fch) / 4;
const offsetX = node.x - centerX;
if (Math.abs(offsetX) < 10) {
compactChildren.forEach(d => d.x += offsetX);
}
const rowsMapNew = this.groupBy(compactChildren, d => d.row, reducedGroup => d3.max(reducedGroup, d => attrs.layoutBindings[attrs.layout].compactDimension.sizeRow(d)));
const cumSum = d3.cumsum(rowsMapNew.map(d => d[1] + attrs.compactMarginBetween(d)));
compactChildren
.forEach((node, i) => {
if (node.row) {
node.y = fch.y + cumSum[node.row - 1]
} else {
node.y = fch.y;
}
})
}
})
}
// This function basically redraws visible graph, based on nodes state
update({ x0, y0, x = 0, y = 0, width, height }) {
const attrs = this.getChartState();
const calc = attrs.calc;
// Paging
if (attrs.compact) {
this.calculateCompactFlexDimensions(attrs.root);
}
// Assigns the x and y position for the nodes
const treeData = attrs.flexTreeLayout(attrs.root);
// Reassigns the x and y position for the based on the compact layout
if (attrs.compact) {
this.calculateCompactFlexPositions(attrs.root);
}
const nodes = treeData.descendants();
// console.table(nodes.map(d => ({ x: d.x, y: d.y, width: d.width, height: d.height, flexCompactDim: d.flexCompactDim + "" })))
// Get all links
const links = treeData.descendants().slice(1);
nodes.forEach(attrs.layoutBindings[attrs.layout].swap)
// Connections
const connections = attrs.connections;
const allNodesMap = {};
attrs.allNodes.forEach(d => allNodesMap[attrs.nodeId(d.data)] = d);
const visibleNodesMap = {}
nodes.forEach(d => visibleNodesMap[attrs.nodeId(d.data)] = d);
connections.forEach(connection => {
const source = allNodesMap[connection.from];
const target = allNodesMap[connection.to];
connection._source = source;
connection._target = target;
})
const visibleConnections = connections.filter(d => visibleNodesMap[d.from] && visibleNodesMap[d.to]);
const defsString = attrs.defs.bind(this)(attrs, visibleConnections);
const existingString = attrs.defsWrapper.html();
if (defsString !== existingString) {
attrs.defsWrapper.html(defsString)
}
// -------------------------- LINKS ----------------------
// Get links selection
const linkSelection = attrs.linksWrapper
.selectAll("path.link")
.data(links, (d) => attrs.nodeId(d.data));
// Enter any new links at the parent's previous position.
const linkEnter = linkSelection
.enter()
.insert("path", "g")
.attr("class", "link")
.attr("d", (d) => {
const xo = attrs.layoutBindings[attrs.layout].linkJoinX({ x: x0, y: y0, width, height });
const yo = attrs.layoutBindings[attrs.layout].linkJoinY({ x: x0, y: y0, width, height });
const o = { x: xo, y: yo };
return attrs.layoutBindings[attrs.layout].diagonal(o, o, o);
});
// Get links update selection
const linkUpdate = linkEnter.merge(linkSelection);
// Styling links
linkUpdate
.attr("fill", "none")
if (this.isEdge()) {
linkUpdate
.style('display', d => {
const display = d.data._pagingButton ? 'none' : 'auto'
return display;
})
} else {
linkUpdate
.attr('display', d => {
const display = d.data._pagingButton ? 'none' : 'auto'
return display;
})
}
// Allow external modifications
linkUpdate.each(attrs.linkUpdate);
// Transition back to the parent element position
linkUpdate
.transition()
.duration(attrs.duration)
.attr("d", (d) => {
const n = attrs.compact && d.flexCompactDim ?
{
x: attrs.layoutBindings[attrs.layout].compactLinkMidX(d, attrs),
y: attrs.layoutBindings[attrs.layout].compactLinkMidY(d, attrs)
} :
{
x: attrs.layoutBindings[attrs.layout].linkX(d),
y: attrs.layoutBindings[attrs.layout].linkY(d)
};
const p = {
x: attrs.layoutBindings[attrs.layout].linkParentX(d),
y: attrs.layoutBindings[attrs.layout].linkParentY(d),
};
const m = attrs.compact && d.flexCompactDim ? {
x: attrs.layoutBindings[attrs.layout].linkCompactXStart(d),
y: attrs.layoutBindings[attrs.layout].linkCompactYStart(d),
} : n;
return attrs.layoutBindings[attrs.layout].diagonal(n, p, m, { sy: attrs.linkYOffset });
});
// Remove any links which is exiting after animation
const linkExit = linkSelection
.exit()
.transition()
.duration(attrs.duration)
.attr("d", (d) => {
const xo = attrs.layoutBindings[attrs.layout].linkJoinX({ x, y, width, height });
const yo = attrs.layoutBindings[attrs.layout].linkJoinY({ x, y, width, height });
const o = { x: xo, y: yo };
return attrs.layoutBindings[attrs.layout].diagonal(o, o, null, { sy: attrs.linkYOffset });
})
.remove();
// -------------------------- CONNECTIONS ----------------------
const connectionsSel = attrs.connectionsWrapper
.selectAll("path.connection")
.data(visibleConnections)
// Enter any new connections at the parent's previous position.
const connEnter = connectionsSel
.enter()
.insert("path", "g")
.attr("class", "connection")
.attr("d", (d) => {
const xo = attrs.layoutBindings[attrs.layout].linkJoinX({ x: x0, y: y0, width, height });
const yo = attrs.layoutBindings[attrs.layout].linkJoinY({ x: x0, y: y0, width, height });
const o = { x: xo, y: yo };
return attrs.layoutBindings[attrs.layout].diagonal(o, o, null, { sy: attrs.linkYOffset });
});
// Get connections update selection
const connUpdate = connEnter.merge(connectionsSel);
// Styling connections
connUpdate.attr("fill", "none")
// Transition back to the parent element position
connUpdate
.transition()
.duration(attrs.duration)
.attr('d', (d) => {
const xs = attrs.layoutBindings[attrs.layout].linkX({ x: d._source.x, y: d._source.y, width: d._source.width, height: d._source.height });
const ys = attrs.layoutBindings[attrs.layout].linkY({ x: d._source.x, y: d._source.y, width: d._source.width, height: d._source.height });
const xt = attrs.layoutBindings[attrs.layout].linkJoinX({ x: d._target.x, y: d._target.y, width: d._target.width, height: d._target.height });
const yt = attrs.layoutBindings[attrs.layout].linkJoinY({ x: d._target.x, y: d._target.y, width: d._target.width, height: d._target.height });
return attrs.linkGroupArc({ source: { x: xs, y: ys }, target: { x: xt, y: yt } })
})
// Allow external modifications