-
Notifications
You must be signed in to change notification settings - Fork 593
/
create-edge-paths.js
130 lines (105 loc) · 3.35 KB
/
create-edge-paths.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
"use strict";
var _ = require("./lodash"),
intersectNode = require("./intersect/intersect-node"),
util = require("./util"),
d3 = require("./d3");
module.exports = createEdgePaths;
function createEdgePaths(selection, g, arrows) {
var svgPaths = selection.selectAll("g.edgePath")
.data(g.edges(), function(e) { return util.edgeToId(e); })
.classed("update", true);
enter(svgPaths, g);
exit(svgPaths, g);
util.applyTransition(svgPaths, g)
.style("opacity", 1);
// Save DOM element in the path group, and set ID and class
svgPaths.each(function(e) {
var domEdge = d3.select(this);
var edge = g.edge(e);
edge.elem = this;
if (edge.id) {
domEdge.attr("id", edge.id);
}
util.applyClass(domEdge, edge["class"],
(domEdge.classed("update") ? "update " : "") + "edgePath");
});
svgPaths.selectAll("path.path")
.each(function(e) {
var edge = g.edge(e);
edge.arrowheadId = _.uniqueId("arrowhead");
var domEdge = d3.select(this)
.attr("marker-end", function() {
return "url(#" + edge.arrowheadId + ")";
})
.style("fill", "none");
util.applyTransition(domEdge, g)
.attr("d", function(e) { return calcPoints(g, e); });
util.applyStyle(domEdge, edge.style);
});
svgPaths.selectAll("defs *").remove();
svgPaths.selectAll("defs")
.each(function(e) {
var edge = g.edge(e),
arrowhead = arrows[edge.arrowhead];
arrowhead(d3.select(this), edge.arrowheadId, edge, "arrowhead");
});
return svgPaths;
}
function calcPoints(g, e) {
var edge = g.edge(e),
tail = g.node(e.v),
head = g.node(e.w),
points = edge.points.slice(1, edge.points.length - 1);
points.unshift(intersectNode(tail, points[0]));
points.push(intersectNode(head, points[points.length - 1]));
return createLine(edge, points);
}
function createLine(edge, points) {
var line = d3.svg.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; });
if (_.has(edge, "lineInterpolate")) {
line.interpolate(edge.lineInterpolate);
}
if (_.has(edge, "lineTension")) {
line.tension(Number(edge.lineTension));
}
return line(points);
}
function getCoords(elem) {
var bbox = elem.getBBox(),
matrix = elem.getTransformToElement(elem.ownerSVGElement)
.translate(bbox.width / 2, bbox.height / 2);
return { x: matrix.e, y: matrix.f };
}
function enter(svgPaths, g) {
var svgPathsEnter = svgPaths.enter()
.append("g")
.attr("class", "edgePath")
.style("opacity", 0);
svgPathsEnter.append("path")
.attr("class", "path")
.attr("d", function(e) {
var edge = g.edge(e),
sourceElem = g.node(e.v).elem,
points = _.range(edge.points.length).map(function() { return getCoords(sourceElem); });
return createLine(edge, points);
});
svgPathsEnter.append("defs");
}
function exit(svgPaths, g) {
var svgPathExit = svgPaths.exit();
util.applyTransition(svgPathExit, g)
.style("opacity", 0)
.remove();
util.applyTransition(svgPathExit.select("path.path"), g)
.attr("d", function(e) {
var source = g.node(e.v);
if (source) {
var points = _.range(this.pathSegList.length).map(function() { return source; });
return createLine({}, points);
} else {
return d3.select(this).attr("d");
}
});
}