forked from dbarzin/mercator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3-graphviz.js
2213 lines (1855 loc) · 68 KB
/
d3-graphviz.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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-selection'), require('d3-dispatch'), require('d3-transition'), require('d3-timer'), require('d3-interpolate'), require('d3-zoom'), require('@hpcc-js/wasm'), require('d3-format'), require('d3-path')) :
typeof define === 'function' && define.amd ? define(['exports', 'd3-selection', 'd3-dispatch', 'd3-transition', 'd3-timer', 'd3-interpolate', 'd3-zoom', '@hpcc-js/wasm', 'd3-format', 'd3-path'], factory) :
(global = global || self, factory(global['d3-graphviz'] = {}, global.d3, global.d3, global.d3, global.d3, global.d3, global.d3, global['@hpcc-js/wasm'], global.d3, global.d3));
}(this, (function (exports, d3, d3Dispatch, d3Transition, d3Timer, d3Interpolate, d3Zoom, wasm, d3Format, d3Path) { 'use strict';
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
}
}
function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance");
}
function extractElementData(element) {
var datum = {};
var tag = element.node().nodeName;
datum.tag = tag;
if (tag == '#text') {
datum.text = element.text();
} else if (tag == '#comment') {
datum.comment = element.text();
}
datum.attributes = {};
var attributes = element.node().attributes;
if (attributes) {
for (var i = 0; i < attributes.length; i++) {
var attribute = attributes[i];
var name = attribute.name;
var value = attribute.value;
datum.attributes[name] = value;
}
}
var transform = element.node().transform;
if (transform && transform.baseVal.numberOfItems != 0) {
var matrix = transform.baseVal.consolidate().matrix;
datum.translation = {
x: matrix.e,
y: matrix.f
};
datum.scale = matrix.a;
}
if (tag == 'ellipse') {
datum.center = {
x: datum.attributes.cx,
y: datum.attributes.cy
};
}
if (tag == 'polygon') {
var points = element.attr('points').split(' ');
var x = points.map(function (p) {
return p.split(',')[0];
});
var y = points.map(function (p) {
return p.split(',')[1];
});
var xmin = Math.min.apply(null, x);
var xmax = Math.max.apply(null, x);
var ymin = Math.min.apply(null, y);
var ymax = Math.max.apply(null, y);
var bbox = {
x: xmin,
y: ymin,
width: xmax - xmin,
height: ymax - ymin
};
datum.bbox = bbox;
datum.center = {
x: (xmin + xmax) / 2,
y: (ymin + ymax) / 2
};
}
if (tag == 'path') {
var d = element.attr('d');
var points = d.split(/[A-Z ]/);
points.shift();
var x = points.map(function (p) {
return +p.split(',')[0];
});
var y = points.map(function (p) {
return +p.split(',')[1];
});
var xmin = Math.min.apply(null, x);
var xmax = Math.max.apply(null, x);
var ymin = Math.min.apply(null, y);
var ymax = Math.max.apply(null, y);
var bbox = {
x: xmin,
y: ymin,
width: xmax - xmin,
height: ymax - ymin
};
datum.bbox = bbox;
datum.center = {
x: (xmin + xmax) / 2,
y: (ymin + ymax) / 2
};
datum.totalLength = element.node().getTotalLength();
}
if (tag == 'text') {
datum.center = {
x: element.attr('x'),
y: element.attr('y')
};
}
if (tag == '#text') {
datum.text = element.text();
} else if (tag == '#comment') {
datum.comment = element.text();
}
return datum;
}
function extractAllElementsData(element) {
var datum = extractElementData(element);
datum.children = [];
var children = d3.selectAll(element.node().childNodes);
children.each(function () {
var childData = extractAllElementsData(d3.select(this));
childData.parent = datum;
datum.children.push(childData);
});
return datum;
}
function createElement(data) {
if (data.tag == '#text') {
return document.createTextNode("");
} else if (data.tag == '#comment') {
return document.createComment(data.comment);
} else {
return document.createElementNS('http://www.w3.org/2000/svg', data.tag);
}
}
function createElementWithAttributes(data) {
var elementNode = createElement(data);
var element = d3.select(elementNode);
var attributes = data.attributes;
for (var _i = 0, _Object$keys = Object.keys(attributes); _i < _Object$keys.length; _i++) {
var attributeName = _Object$keys[_i];
var attributeValue = attributes[attributeName];
element.attr(attributeName, attributeValue);
}
return elementNode;
}
function replaceElement(element, data) {
var parent = d3.select(element.node().parentNode);
var newElementNode = createElementWithAttributes(data);
var newElement = parent.insert(function () {
return newElementNode;
}, function () {
return element.node();
});
element.remove();
return newElement;
}
function insertElementData(element, datum) {
element.datum(datum);
element.data([datum], function (d) {
return d.key;
});
}
function insertAllElementsData(element, datum) {
insertElementData(element, datum);
var children = d3.selectAll(element.node().childNodes);
children.each(function (d, i) {
insertAllElementsData(d3.select(this), datum.children[i]);
});
}
function insertChildren(element, index) {
var children = element.selectAll(function () {
return element.node().childNodes;
});
children = children.data(function (d) {
return d.children;
}, function (d) {
return d.tag + '-' + index;
});
var childrenEnter = children.enter().append(function (d) {
return createElement(d);
});
var childrenExit = children.exit();
childrenExit = childrenExit.remove();
children = childrenEnter.merge(children);
var childTagIndexes = {};
children.each(function (childData) {
var childTag = childData.tag;
if (childTagIndexes[childTag] == null) {
childTagIndexes[childTag] = 0;
}
var childIndex = childTagIndexes[childTag]++;
attributeElement.call(this, childData, childIndex);
});
}
function attributeElement(data) {
var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var element = d3.select(this);
var tag = data.tag;
var attributes = data.attributes;
var currentAttributes = element.node().attributes;
if (currentAttributes) {
for (var i = 0; i < currentAttributes.length; i++) {
var currentAttribute = currentAttributes[i];
var name = currentAttribute.name;
if (name.split(':')[0] != 'xmlns' && currentAttribute.namespaceURI) {
var namespaceURIParts = currentAttribute.namespaceURI.split('/');
var namespace = namespaceURIParts[namespaceURIParts.length - 1];
name = namespace + ':' + name;
}
if (!(name in attributes)) {
attributes[name] = null;
}
}
}
for (var _i2 = 0, _Object$keys2 = Object.keys(attributes); _i2 < _Object$keys2.length; _i2++) {
var attributeName = _Object$keys2[_i2];
element.attr(attributeName, attributes[attributeName]);
}
if (data.text) {
element.text(data.text);
}
insertChildren(element, index);
}
function shallowCopyObject(obj) {
return Object.assign({}, obj);
}
function roundTo2Decimals(x) {
return Math.round(x * 100.0) / 100.0;
}
function zoom (enable) {
this._options.zoom = enable;
if (this._options.zoom && !this._zoomBehavior) {
createZoomBehavior.call(this);
}
return this;
}
function createZoomBehavior() {
function zoomed() {
var g = d3.select(svg.node().querySelector("g"));
g.attr('transform', d3.event.transform);
}
var root = this._selection;
var svg = d3.select(root.node().querySelector("svg"));
if (svg.size() == 0) {
return this;
}
this._zoomSelection = svg;
var zoomBehavior = d3Zoom.zoom().scaleExtent(this._options.zoomScaleExtent).translateExtent(this._options.zoomTranslateExtent).interpolate(d3Interpolate.interpolate).on("zoom", zoomed);
this._zoomBehavior = zoomBehavior;
var g = d3.select(svg.node().querySelector("g"));
svg.call(zoomBehavior);
if (!this._active) {
translateZoomBehaviorTransform.call(this, g);
}
this._originalTransform = d3Zoom.zoomTransform(svg.node());
return this;
}
function getTranslatedZoomTransform(selection) {
// Get the current zoom transform for the top level svg and
// translate it uniformly with the given selection, using the
// difference between the translation specified in the selection's
// data and it's saved previous translation. The selection is
// normally the top level g element of the graph.
var oldTranslation = this._translation;
var oldScale = this._scale;
var newTranslation = selection.datum().translation;
var newScale = selection.datum().scale;
var t = d3Zoom.zoomTransform(this._zoomSelection.node());
if (oldTranslation) {
t = t.scale(1 / oldScale);
t = t.translate(-oldTranslation.x, -oldTranslation.y);
}
t = t.translate(newTranslation.x, newTranslation.y);
t = t.scale(newScale);
return t;
}
function translateZoomBehaviorTransform(selection) {
// Translate the current zoom transform for the top level svg
// uniformly with the given selection, using the difference
// between the translation specified in the selection's data and
// it's saved previous translation. The selection is normally the
// top level g element of the graph.
this._zoomBehavior.transform(this._zoomSelection, getTranslatedZoomTransform.call(this, selection)); // Save the selections's new translation and scale.
this._translation = selection.datum().translation;
this._scale = selection.datum().scale; // Set the original zoom transform to the translation and scale specified in
// the selection's data.
this._originalTransform = d3Zoom.zoomIdentity.translate(selection.datum().translation.x, selection.datum().translation.y).scale(selection.datum().scale);
}
function resetZoom(transition) {
// Reset the zoom transform to the original zoom transform.
var selection = this._zoomSelection;
if (transition) {
selection = selection.transition(transition);
}
selection.call(this._zoomBehavior.transform, this._originalTransform);
return this;
}
function zoomScaleExtent(extent) {
this._options.zoomScaleExtent = extent;
return this;
}
function zoomTranslateExtent(extent) {
this._options.zoomTranslateExtent = extent;
return this;
}
function zoomBehavior() {
return this._zoomBehavior || null;
}
function zoomSelection() {
return this._zoomSelection || null;
}
function pathTween(points, d1) {
return function () {
var pointInterpolators = points.map(function (p) {
return d3Interpolate.interpolate([p[0][0], p[0][1]], [p[1][0], p[1][1]]);
});
return function (t) {
return t < 1 ? "M" + pointInterpolators.map(function (p) {
return p(t);
}).join("L") : d1;
};
};
}
function pathTweenPoints(node, d1, precision, precisionIsRelative) {
var path0 = node;
var path1 = path0.cloneNode();
var n0 = path0.getTotalLength();
var n1 = (path1.setAttribute("d", d1), path1).getTotalLength(); // Uniform sampling of distance based on specified precision.
var distances = [0];
var i = 0;
var dt = precisionIsRelative ? precision : precision / Math.max(n0, n1);
while ((i += dt) < 1) {
distances.push(i);
}
distances.push(1); // Compute point-interpolators at each distance.
var points = distances.map(function (t) {
var p0 = path0.getPointAtLength(t * n0);
var p1 = path1.getPointAtLength(t * n1);
return [[p0.x, p0.y], [p1.x, p1.y]];
});
return points;
}
function data () {
return this._data || null;
}
function isEdgeElementParent(datum) {
return datum.attributes["class"] == 'edge' || datum.tag == 'a' && datum.parent.tag == 'g' && datum.parent.parent.attributes["class"] == 'edge';
}
function isEdgeElement(datum) {
return datum.parent && isEdgeElementParent(datum.parent);
}
function getEdgeGroup(datum) {
if (datum.parent.attributes["class"] == 'edge') {
return datum.parent;
} else {
// datum.parent.tag == 'g' && datum.parent.parent.tag == 'g' && datum.parent.parent.parent.attributes.class == 'edge'
return datum.parent.parent.parent;
}
}
function getEdgeTitle(datum) {
return getEdgeGroup(datum).children.find(function (e) {
return e.tag == 'title';
});
}
function render (callback) {
if (this._busy) {
this._queue.push(this.render.bind(this, callback));
return this;
}
this._dispatch.call('renderStart', this);
if (this._transitionFactory) {
d3Timer.timeout(function () {
// Decouple from time spent. See https://github.com/d3/d3-timer/issues/27
this._transition = d3Transition.transition(this._transitionFactory());
_render.call(this, callback);
}.bind(this), 0);
} else {
_render.call(this, callback);
}
return this;
}
function _render(callback) {
var transitionInstance = this._transition;
var fade = this._options.fade && transitionInstance != null;
var tweenPaths = this._options.tweenPaths;
var tweenShapes = this._options.tweenShapes;
var convertEqualSidedPolygons = this._options.convertEqualSidedPolygons;
var growEnteringEdges = this._options.growEnteringEdges && transitionInstance != null;
var attributer = this._attributer;
var graphvizInstance = this;
function insertChildren(element) {
var children = element.selectAll(function () {
return element.node().childNodes;
});
children = children.data(function (d) {
return d.children;
}, function (d) {
return d.key;
});
var childrenEnter = children.enter().append(function (d) {
var element = createElement(d);
if (d.tag == '#text' && fade) {
element.nodeValue = d.text;
}
return element;
});
if (fade || growEnteringEdges && isEdgeElementParent(element.datum())) {
var childElementsEnter = childrenEnter.filter(function (d) {
return d.tag[0] == '#' ? null : this;
}).each(function (d) {
var childEnter = d3.select(this);
for (var _i = 0, _Object$keys = Object.keys(d.attributes); _i < _Object$keys.length; _i++) {
var attributeName = _Object$keys[_i];
var attributeValue = d.attributes[attributeName];
childEnter.attr(attributeName, attributeValue);
}
});
childElementsEnter.filter(function (d) {
return d.tag == 'svg' || d.tag == 'g' ? null : this;
}).style("opacity", 0.0);
}
var childrenExit = children.exit();
if (attributer) {
childrenExit.each(attributer);
}
if (transitionInstance) {
childrenExit = childrenExit.transition(transitionInstance);
if (fade) {
childrenExit.filter(function (d) {
return d.tag[0] == '#' ? null : this;
}).style("opacity", 0.0);
}
}
childrenExit = childrenExit.remove();
children = childrenEnter.merge(children);
children.each(attributeElement);
}
function attributeElement(data) {
var element = d3.select(this);
if (data.tag == "svg") {
var options = graphvizInstance._options;
if (options.width != null || options.height != null) {
var width = options.width;
var height = options.height;
if (width == null) {
width = data.attributes.width.replace('pt', '') * 4 / 3;
} else {
element.attr("width", width);
data.attributes.width = width;
}
if (height == null) {
height = data.attributes.height.replace('pt', '') * 4 / 3;
} else {
element.attr("height", height);
data.attributes.height = height;
}
if (!options.fit) {
element.attr("viewBox", "0 0 ".concat(width * 3 / 4 / options.scale, " ").concat(height * 3 / 4 / options.scale));
data.attributes.viewBox = "0 0 ".concat(width * 3 / 4 / options.scale, " ").concat(height * 3 / 4 / options.scale);
}
}
if (options.scale != 1 && (options.fit || options.width == null && options.height == null)) {
width = data.attributes.viewBox.split(' ')[2];
height = data.attributes.viewBox.split(' ')[3];
element.attr("viewBox", "0 0 ".concat(width / options.scale, " ").concat(height / options.scale));
data.attributes.viewBox = "0 0 ".concat(width / options.scale, " ").concat(height / options.scale);
}
}
if (attributer) {
element.each(attributer);
}
var tag = data.tag;
var attributes = data.attributes;
var currentAttributes = element.node().attributes;
if (currentAttributes) {
for (var i = 0; i < currentAttributes.length; i++) {
var currentAttribute = currentAttributes[i];
var name = currentAttribute.name;
if (name.split(':')[0] != 'xmlns' && currentAttribute.namespaceURI) {
var namespaceURIParts = currentAttribute.namespaceURI.split('/');
var namespace = namespaceURIParts[namespaceURIParts.length - 1];
name = namespace + ':' + name;
}
if (!(name in attributes)) {
attributes[name] = null;
}
}
}
var convertShape = false;
var convertPrevShape = false;
if (tweenShapes && transitionInstance) {
if ((this.nodeName == 'polygon' || this.nodeName == 'ellipse') && data.alternativeOld) {
convertPrevShape = true;
}
if ((tag == 'polygon' || tag == 'ellipse') && data.alternativeNew) {
convertShape = true;
}
if (this.nodeName == 'polygon' && tag == 'polygon' && data.alternativeOld) {
var prevData = extractElementData(element);
var prevPoints = prevData.attributes.points;
if (!convertEqualSidedPolygons) {
var nPrevPoints = prevPoints.split(' ').length;
var points = data.attributes.points;
var nPoints = points.split(' ').length;
if (nPoints == nPrevPoints) {
convertShape = false;
convertPrevShape = false;
}
}
}
if (convertPrevShape) {
var prevPathData = data.alternativeOld;
var pathElement = replaceElement(element, prevPathData);
pathElement.data([data], function () {
return data.key;
});
element = pathElement;
}
if (convertShape) {
var newPathData = data.alternativeNew;
tag = 'path';
attributes = newPathData.attributes;
}
}
var elementTransition = element;
if (transitionInstance) {
elementTransition = elementTransition.transition(transitionInstance);
if (fade) {
elementTransition.filter(function (d) {
return d.tag[0] == '#' ? null : this;
}).style("opacity", 1.0);
}
elementTransition.filter(function (d) {
return d.tag[0] == '#' ? null : this;
}).on("end", function () {
d3.select(this).attr('style', null);
});
}
var growThisPath = growEnteringEdges && tag == 'path' && data.offset;
if (growThisPath) {
var totalLength = data.totalLength;
element.attr("stroke-dasharray", totalLength + " " + totalLength).attr("stroke-dashoffset", totalLength).attr('transform', 'translate(' + data.offset.x + ',' + data.offset.y + ')');
attributes["stroke-dashoffset"] = 0;
attributes['transform'] = 'translate(0,0)';
elementTransition.attr("stroke-dashoffset", attributes["stroke-dashoffset"]).attr('transform', attributes['transform']).on("start", function () {
d3.select(this).style('opacity', null);
}).on("end", function () {
d3.select(this).attr('stroke-dashoffset', null).attr('stroke-dasharray', null).attr('transform', null);
});
}
var moveThisPolygon = growEnteringEdges && tag == 'polygon' && isEdgeElement(data) && data.offset && data.parent.children[3].tag == 'path';
if (moveThisPolygon) {
var edgePath = d3.select(element.node().parentNode.querySelector("path"));
var p0 = edgePath.node().getPointAtLength(0);
var p1 = edgePath.node().getPointAtLength(data.totalLength);
var p2 = edgePath.node().getPointAtLength(data.totalLength - 1);
var angle1 = Math.atan2(p1.y - p2.y, p1.x - p2.x) * 180 / Math.PI;
var x = p0.x - p1.x + data.offset.x;
var y = p0.y - p1.y + data.offset.y;
element.attr('transform', 'translate(' + x + ',' + y + ')');
elementTransition.attrTween("transform", function () {
return function (t) {
var p = edgePath.node().getPointAtLength(data.totalLength * t);
var p2 = edgePath.node().getPointAtLength(data.totalLength * t + 1);
var angle = Math.atan2(p2.y - p.y, p2.x - p.x) * 180 / Math.PI - angle1;
x = p.x - p1.x + data.offset.x * (1 - t);
y = p.y - p1.y + data.offset.y * (1 - t);
return 'translate(' + x + ',' + y + ') rotate(' + angle + ' ' + p1.x + ' ' + p1.y + ')';
};
}).on("start", function () {
d3.select(this).style('opacity', null);
}).on("end", function () {
d3.select(this).attr('transform', null);
});
}
var tweenThisPath = tweenPaths && transitionInstance && tag == 'path' && element.attr('d') != null;
for (var _i2 = 0, _Object$keys2 = Object.keys(attributes); _i2 < _Object$keys2.length; _i2++) {
var attributeName = _Object$keys2[_i2];
var attributeValue = attributes[attributeName];
if (tweenThisPath && attributeName == 'd') {
var points = (data.alternativeOld || data).points;
if (points) {
elementTransition.attrTween("d", pathTween(points, attributeValue));
}
} else {
if (attributeName == 'transform' && data.translation) {
if (transitionInstance) {
var onEnd = elementTransition.on("end");
elementTransition.on("start", function () {
if (graphvizInstance._zoomBehavior) {
// Update the transform to transition to, just before the transition starts
// in order to catch changes between the transition scheduling to its start.
elementTransition.tween("attr.transform", function () {
var node = this;
return function (t) {
node.setAttribute("transform", d3Interpolate.interpolateTransformSvg(d3Zoom.zoomTransform(graphvizInstance._zoomSelection.node()).toString(), getTranslatedZoomTransform.call(graphvizInstance, element).toString())(t));
};
});
}
}).on("end", function () {
onEnd.call(this); // Update the zoom transform to the new translated transform
if (graphvizInstance._zoomBehavior) {
translateZoomBehaviorTransform.call(graphvizInstance, element);
}
});
} else {
if (graphvizInstance._zoomBehavior) {
// Update the transform attribute to set with the current pan translation
attributeValue = getTranslatedZoomTransform.call(graphvizInstance, element).toString();
}
}
}
elementTransition.attr(attributeName, attributeValue);
}
}
if (convertShape) {
elementTransition.on("end", function (d, i, nodes) {
pathElement = d3.select(this);
var newElement = replaceElement(pathElement, d);
newElement.data([d], function () {
return d.key;
});
});
}
if (data.text) {
elementTransition.text(data.text);
}
insertChildren(element);
}
var root = this._selection;
if (transitionInstance != null) {
// Ensure original SVG shape elements are restored after transition before rendering new graph
var jobs = this._jobs;
if (graphvizInstance._active) {
jobs.push(null);
return this;
} else {
root.transition(transitionInstance).transition().duration(0).on("end", function () {
graphvizInstance._active = false;
if (jobs.length != 0) {
jobs.shift();
graphvizInstance.render();
}
});
this._active = true;
}
}
if (transitionInstance != null) {
root.transition(transitionInstance).on("start", function () {
graphvizInstance._dispatch.call('transitionStart', graphvizInstance);
}).on("end", function () {
graphvizInstance._dispatch.call('transitionEnd', graphvizInstance);
}).transition().duration(0).on("start", function () {
graphvizInstance._dispatch.call('restoreEnd', graphvizInstance);
graphvizInstance._dispatch.call('end', graphvizInstance);
if (callback) {
callback.call(graphvizInstance);
}
});
}
var data = this._data;
var svg = root.selectAll("svg").data([data], function (d) {
return d.key;
});
svg = svg.enter().append("svg").merge(svg);
attributeElement.call(svg.node(), data);
if (this._options.zoom && !this._zoomBehavior) {
createZoomBehavior.call(this);
}
graphvizInstance._dispatch.call('renderEnd', graphvizInstance);
if (transitionInstance == null) {
this._dispatch.call('end', this);
if (callback) {
callback.call(this);
}
}
return this;
}
function convertToPathData(originalData, guideData) {
if (originalData.tag == 'polygon') {
var newData = shallowCopyObject(originalData);
newData.tag = 'path';
var originalAttributes = originalData.attributes;
var newAttributes = shallowCopyObject(originalAttributes);
var newPointsString = originalAttributes.points;
if (guideData.tag == 'polygon') {
var bbox = originalData.bbox;
bbox.cx = bbox.x + bbox.width / 2;
bbox.cy = bbox.y + bbox.height / 2;
var pointsString = originalAttributes.points;
var pointStrings = pointsString.split(' ');
var normPoints = pointStrings.map(function (p) {
var xy = p.split(',');
return [xy[0] - bbox.cx, xy[1] - bbox.cy];
});
var x0 = normPoints[normPoints.length - 1][0];
var y0 = normPoints[normPoints.length - 1][1];
for (var i = 0; i < normPoints.length; i++, x0 = x1, y0 = y1) {
var x1 = normPoints[i][0];
var y1 = normPoints[i][1];
var dx = x1 - x0;
var dy = y1 - y0;
if (dy == 0) {
continue;
} else {
var x2 = x0 - y0 * dx / dy;
}
if (0 <= x2 && x2 < Infinity && (x0 <= x2 && x2 <= x1 || x1 <= x2 && x2 <= x0)) {
break;
}
}
var newPointStrings = [[bbox.cx + x2, bbox.cy + 0].join(',')];
newPointStrings = newPointStrings.concat(pointStrings.slice(i));
newPointStrings = newPointStrings.concat(pointStrings.slice(0, i));
newPointsString = newPointStrings.join(' ');
}
newAttributes['d'] = 'M' + newPointsString + 'z';
delete newAttributes.points;
newData.attributes = newAttributes;
} else
/* if (originalData.tag == 'ellipse') */
{
var newData = shallowCopyObject(originalData);
newData.tag = 'path';
var originalAttributes = originalData.attributes;
var newAttributes = shallowCopyObject(originalAttributes);
var cx = originalAttributes.cx;
var cy = originalAttributes.cy;
var rx = originalAttributes.rx;
var ry = originalAttributes.ry;
if (guideData.tag == 'polygon') {
var bbox = guideData.bbox;
bbox.cx = bbox.x + bbox.width / 2;
bbox.cy = bbox.y + bbox.height / 2;
var p = guideData.attributes.points.split(' ')[0].split(',');
var sx = p[0];
var sy = p[1];
var dx = sx - bbox.cx;
var dy = sy - bbox.cy;
var l = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
var cosA = dx / l;
var sinA = -dy / l;
} else {
// if (guideData.tag == 'path') {
// FIXME: add support for getting start position from path
var cosA = 1;
var sinA = 0;
}
var x1 = rx * cosA;
var y1 = -ry * sinA;
var x2 = rx * -cosA;
var y2 = -ry * -sinA;
var dx = x2 - x1;
var dy = y2 - y1;
newAttributes['d'] = 'M ' + cx + ' ' + cy + ' m ' + x1 + ',' + y1 + ' a ' + rx + ',' + ry + ' 0 1,0 ' + dx + ',' + dy + ' a ' + rx + ',' + ry + ' 0 1,0 ' + -dx + ',' + -dy + 'z';
delete newAttributes.cx;
delete newAttributes.cy;
delete newAttributes.rx;
delete newAttributes.ry;
newData.attributes = newAttributes;
}
return newData;
}
function translatePointsAttribute(pointsString, x, y) {
var pointStrings = pointsString.split(' ');
var points = pointStrings.map(function (p) {
return p.split(',');
});
var points = pointStrings.map(function (p) {
return [roundTo2Decimals(+x + +p.split(',')[0]), roundTo2Decimals(+y + +p.split(',')[1])];
});
var pointStrings = points.map(function (p) {
return p.join(',');
});
var pointsString = pointStrings.join(' ');
return pointsString;
}
function translateDAttribute(d, x, y) {
var pointStrings = d.split(/[A-Z ]/);
pointStrings.shift();
var commands = d.split(/[^[A-Z ]+/);
var points = pointStrings.map(function (p) {
return p.split(',');
});
var points = pointStrings.map(function (p) {
return [roundTo2Decimals(+x + +p.split(',')[0]), roundTo2Decimals(+y + +p.split(',')[1])];
});
var pointStrings = points.map(function (p) {
return p.join(',');
});
d = commands.reduce(function (arr, v, i) {
return arr.concat(v, pointStrings[i]);
}, []).join('');
return d;
}
function initViz() {
var _this = this;
// force JIT compilation of Viz.js
try {
wasm.graphviz.layout("", "svg", "dot").then(function () {
wasm.graphvizSync().then(function (graphviz1) {
_this.layoutSync = graphviz1.layout.bind(graphviz1);
if (_this._worker == null) {
_this._dispatch.call("initEnd", _this);
}
if (_this._afterInit) {
_this._afterInit();
}
});
});
} catch (error) {}
if (this._worker != null) {
var vizURL = this._vizURL;
var graphvizInstance = this;
this._worker.onmessage = function (event) {
var callback = graphvizInstance._workerCallbacks.shift();
switch (event.data.type) {
case "init":
graphvizInstance._dispatch.call("initEnd", this);
break;
case "done":
return layoutDone.call(graphvizInstance, event.data.svg, callback);
case "error":
if (graphvizInstance._onerror) {
graphvizInstance._onerror(event.data.error);
} else {
throw event.data.error;
}
break;
}
};
if (!vizURL.match(/^https?:\/\/|^\/\//i)) {
// Local URL. Prepend with local domain to be usable in web worker
vizURL = new window.URL(vizURL, document.location.href).href;
}
postMessage.call(this, {
dot: "",
engine: 'dot',
vizURL: vizURL
});