-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathdrawEdge.js
226 lines (185 loc) · 6.35 KB
/
drawEdge.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
import * as d3 from "d3-selection";
import {path as d3_path} from "d3-path";
import {rotate} from "./geometry.js";
import {extractAllElementsData} from "./element.js";
import {attributeElement} from "./element.js";
import {insertAllElementsData} from "./element.js";
export function drawEdge(x1, y1, x2, y2, attributes, options={}) {
attributes = Object.assign({}, attributes);
if (attributes.style && attributes.style.includes('invis')) {
var newEdge = d3.select(null);
} else {
var root = this._selection;
var svg = root.selectWithoutDataPropagation("svg");
var graph0 = svg.selectWithoutDataPropagation("g");
var newEdge0 = createEdge.call(this, attributes);
var edgeData = extractAllElementsData(newEdge0);
var newEdge = graph0.append('g')
.data([edgeData]);
attributeElement.call(newEdge.node(), edgeData);
_updateEdge.call(this, newEdge, x1, y1, x2, y2, attributes, options);
}
this._drawnEdge = {
g: newEdge,
x1: x1,
y1: y1,
x2: x2,
y2: y2,
attributes: attributes,
};
return this;
}
export function updateDrawnEdge(x1, y1, x2, y2, attributes={}, options={}) {
if (!this._drawnEdge) {
throw Error('No edge has been drawn');
}
var edge = this._drawnEdge.g
attributes = Object.assign(this._drawnEdge.attributes, attributes);
this._drawnEdge.x1 = x1;
this._drawnEdge.y1 = y1;
this._drawnEdge.x2 = x2;
this._drawnEdge.y2 = y2;
if (edge.empty() && !(attributes.style && attributes.style.includes('invis'))) {
var root = this._selection;
var svg = root.selectWithoutDataPropagation("svg");
var graph0 = svg.selectWithoutDataPropagation("g");
var edge = graph0.append('g');
this._drawnEdge.g = edge;
}
if (!edge.empty()) {
_updateEdge.call(this, edge, x1, y1, x2, y2, attributes, options);
}
return this;
}
function _updateEdge(edge, x1, y1, x2, y2, attributes, options) {
var newEdge = createEdge.call(this, attributes);
var edgeData = extractAllElementsData(newEdge);
edge.data([edgeData]);
attributeElement.call(edge.node(), edgeData);
_moveEdge(edge, x1, y1, x2, y2, attributes, options);
}
function _moveEdge(edge, x1, y1, x2, y2, attributes, options) {
var shortening = options.shortening || 0;
var arrowHeadLength = 10;
var arrowHeadWidth = 7;
var margin = 0.1;
var arrowHeadPoints = [
[0, -arrowHeadWidth / 2],
[arrowHeadLength, 0],
[0, arrowHeadWidth / 2],
[0, -arrowHeadWidth / 2],
];
var dx = x2 - x1;
var dy = y2 - y1;
var length = Math.sqrt(dx * dx + dy * dy);
if (length == 0) {
var cosA = 1;
var sinA = 0;
} else {
var cosA = dx / length;
var sinA = dy / length;
}
x2 = x1 + (length - shortening - arrowHeadLength - margin) * cosA;
y2 = y1 + (length - shortening - arrowHeadLength - margin) * sinA;
if (attributes.URL || attributes.tooltip) {
var a = edge.selectWithoutDataPropagation("g").selectWithoutDataPropagation("a");
var line = a.selectWithoutDataPropagation("path");
var arrowHead = a.selectWithoutDataPropagation("polygon");
} else {
var line = edge.selectWithoutDataPropagation("path");
var arrowHead = edge.selectWithoutDataPropagation("polygon");
}
var path1 = d3_path();
path1.moveTo(x1, y1);
path1.lineTo(x2, y2);
line
.attr("d", path1);
x2 = x1 + (length - shortening - arrowHeadLength) * cosA;
y2 = y1 + (length - shortening - arrowHeadLength) * sinA;
for (var i = 0; i < arrowHeadPoints.length; i++) {
var point = arrowHeadPoints[i];
arrowHeadPoints[i] = rotate(point[0], point[1], cosA, sinA);
}
for (var i = 0; i < arrowHeadPoints.length; i++) {
var point = arrowHeadPoints[i];
arrowHeadPoints[i] = [x2 + point[0], y2 + point[1]];
}
var allPoints = [];
for (var i = 0; i < arrowHeadPoints.length; i++) {
var point = arrowHeadPoints[i];
allPoints.push(point.join(','));
}
var pointsAttr = allPoints.join(' ');
arrowHead
.attr("points", pointsAttr);
return this;
}
export function moveDrawnEdgeEndPoint(x2, y2, options={}) {
if (!this._drawnEdge) {
throw Error('No edge has been drawn');
}
var edge = this._drawnEdge.g;
var x1 = this._drawnEdge.x1;
var y1 = this._drawnEdge.y1;
var attributes = this._drawnEdge.attributes;
this._drawnEdge.x2 = x2;
this._drawnEdge.y2 = y2;
_moveEdge(edge, x1, y1, x2, y2, attributes, options);
return this
}
export function removeDrawnEdge() {
if (!this._drawnEdge) {
return this;
}
var edge = this._drawnEdge.g;
edge.remove();
this._drawnEdge = null;
return this
}
export function insertDrawnEdge(name) {
if (!this._drawnEdge) {
throw Error('No edge has been drawn');
}
var edge = this._drawnEdge.g;
if (edge.empty()) {
return this;
}
var attributes = this._drawnEdge.attributes;
var title = edge.selectWithoutDataPropagation("title");
title
.text(name);
var root = this._selection;
var svg = root.selectWithoutDataPropagation("svg");
var graph0 = svg.selectWithoutDataPropagation("g");
var graph0Datum = graph0.datum();
var edgeData = this._extractData(edge, graph0Datum.children.length, graph0.datum());
graph0Datum.children.push(edgeData);
insertAllElementsData(edge, edgeData);
this._drawnEdge = null;
return this
}
export function drawnEdgeSelection() {
if (this._drawnEdge) {
return this._drawnEdge.g;
} else {
return d3.select(null);
}
}
function createEdge(attributes) {
var attributesString = ''
for (var name of Object.keys(attributes)) {
if (attributes[name] != null) {
attributesString += ' "' + name + '"="' + attributes[name] + '"';
}
}
var dotSrc = 'digraph {a -> b [' + attributesString + ']}';
var svgDoc = this.layoutSync(dotSrc, 'svg', 'dot');
var parser = new window.DOMParser();
var doc = parser.parseFromString(svgDoc, "image/svg+xml");
var newDoc = d3.select(document.createDocumentFragment())
.append(function() {
return doc.documentElement;
});
var edge = newDoc.select('.edge');
return edge;
}