-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
222 lines (189 loc) · 6.12 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Circuits</title>
<style>
@import url(./style.css);
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
fill: none;
stroke: #000;
stroke-width: 4px;
opacity: 0.4;
marker-end: url(#end-arrow);
}
</style>
</head>
<body>
<h1>Circuits</h1>
<div>
<button onclick="toggleGuides()">toggle guides</button>
</div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="extern/cola.js"></script>
<script>
var width = 960,
height = 900;
var groups;
let showGuides = false;
var color = d3.scaleOrdinal(d3.schemeCategory20);
var d3cola = cola.d3adaptor(d3)
.avoidOverlaps(true)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("graphdata/circuit.json", function(error, graph) {
var nodeRadius = 30;
graph.nodes.forEach(function(v) {
v.height = v.width = 2 * nodeRadius;
});
d3cola
.nodes(graph.nodes)
.links(graph.links)
.groups(graph.groups)
// .flowLayout("y", -30)
.symmetricDiffLinkLengths(40)
.start(10, 20, 20);
// define arrow markers for graph links
// svg.append('svg:defs').append('svg:marker')
// .attr('id', 'end-arrow')
// .attr('viewBox', '0 -5 10 10')
// .attr('refX', 6)
// .attr('markerWidth', 15)
// .attr('markerHeight', 15)
// .attr('orient', 'auto')
// .append('svg:path')
// .attr('d', 'M0,-5L10,0L0,5')
// .attr('fill', '#000');
if (showGuides) {
renderGroups();
}
var path = svg.selectAll(".link")
.data(graph.links)
.enter().append('svg:path')
.attr('class', 'link');
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.call(d3cola.drag);
node
.filter(function (d) { return d.type === 'Var';})
.append('circle')
.attr("class", "node")
.attr("r", nodeRadius)
.style("fill", function(d) {
return color(d.group);
});
// Append images
var images = node
.filter(function (d) { return !!d.img;})
.append("svg:image")
.attr("xlink:href", function(d) { return d.img;})
.attr("x", function(d) { return -nodeRadius;})
.attr("y", function(d) { return -nodeRadius;})
.attr("height", 2*nodeRadius)
.attr("width", 2*nodeRadius);
node.append("title")
.text(function(d) {
return d.name;
});
function groupCenter (bounds) {
return {
x: (bounds.X - bounds.x) / 2 + bounds.x,
y: (bounds.Y - bounds.y) / 2 + bounds.y
};
}
function tick() {
path.each(function(d) {
if (isIE()) this.parentNode.insertBefore(this, this);
});
// draw directed edges with proper padding from node centers
path.attr('d', function(d) {
let varNode = d.source.type === 'Var' ? d.source : d.target;
let varGroup = varNode.parent;
let gCenter = groupCenter(varGroup.bounds);
// let groupCenterX = (varGroup.bounds.X - varGroup.bounds.x) / 2 + varGroup.bounds.x;
// let groupCenterY = (varGroup.bounds.Y - varGroup.bounds.y) / 2 + varGroup.bounds.y;
// console.log(varGroup);
let deltaX = d.target.x - d.source.x,
deltaY = d.target.y - d.source.y,
dist = Math.sqrt(deltaX * deltaX + deltaY * deltaY),
normX = deltaX / dist,
normY = deltaY / dist,
sourcePadding = 0; // nodeRadius,
targetPadding = 0; //nodeRadius + 2,
sourceX = d.source.x + (sourcePadding * normX),
sourceY = d.source.y + (sourcePadding * normY),
targetX = d.target.x - (targetPadding * normX),
targetY = d.target.y - (targetPadding * normY);
return 'M' + sourceX + ',' + sourceY + 'S' + gCenter.x + ',' + gCenter.y + ' ' + targetX + ',' + targetY;
});
node
.attr('transform', function (d) {
let transform = 'translate(' + d.x + ','+ d.y +')';
if(d.type == 'Component') {
let gc1 = groupCenter(d.parent.groups[0].bounds);
let gc2 = groupCenter(d.parent.groups[1].bounds);
let angle = Math.atan2(gc2.y - gc1.y, gc2.x - gc1.x) * 180 / Math.PI;
return transform + ' rotate('+angle+')'
}
return transform;
})
// .attr("cx", function(d) {
// return d.x;
// })
// .attr("cy", function(d) {
// return d.y;
// });
if (showGuides) {
group.attr("x", function(d) {
return d.bounds.x;
})
.attr("y", function(d) {
return d.bounds.y;
})
.attr("width", function(d) {
return d.bounds.width();
})
.attr("height", function(d) {
return d.bounds.height();
});
}
}
d3cola.on("tick", tick);
window.toggleGuides = function() {
showGuides = !showGuides;
if (showGuides) {
renderGroups();
} else {
removeGroups();
}
tick();
}
function renderGroups() {
// console.log('renderGroups');
group = svg.selectAll(".group")
.data(graph.groups)
.enter().insert("rect", ".link")
.attr("rx", 8).attr("ry", 8)
.attr("class", "group")
.style("fill", function(d, i) {
return color(i);
})
.call(d3cola.drag);
}
function removeGroups() {
svg.selectAll(".group").remove();
}
});
function isIE() {
return ((navigator.appName == 'Microsoft Internet Explorer') || ((navigator.appName == 'Netscape') && (new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})").exec(navigator.userAgent) != null)));
}
</script>
</body>
</html>