-
Notifications
You must be signed in to change notification settings - Fork 12
/
d3-msg-seq.js
247 lines (198 loc) · 7.43 KB
/
d3-msg-seq.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
d3.messageSequence = function() {
var actor_box_offset = 2,
fade_time = 2000,
animation_time = 250;
var data = [];
var actor_names = [];
var update;
var animation_in_progress,
update_pending;
// lots of O(n) here, move to a map at some point
function remove_free_actors() {
var busy = data.reduce(function(actors, d) {
if (actors.indexOf(d.from) < 0)
actors.push(d.from);
if (actors.indexOf(d.to) < 0)
actors.push(d.to);
return actors;
}, []);
actor_names = actor_names.filter(function(a) {
return busy.indexOf(a) > -1;
});
}
function chart(selection) {
selection.each(function() {
var svg = d3.select(this);
svg.append("marker")
.attr("id", "msg-arrow")
.attr("viewBox", "0 0 10 10")
.attr("refX", "10")
.attr("refY", "5")
.attr("markerWidth", "5")
.attr("markerHeight", "4")
.attr("orient", "auto")
.append("path")
.attr("d", "M 0 0 L 10 5 L 0 10 z");
svg.append("g").classed("actors", true);
update = function() {
if (animation_in_progress) {
update_pending = true;
return;
}
animation_in_progress = true;
function index_horizontal_center_percent(_d, index) {
return (index+1) * (100/(actor_names.length+1)) + "%";
}
// this could be memoized, if speed became a concern
function actor_horizontal_center_percent(actor_name) {
return index_horizontal_center_percent(null, actor_names.indexOf(actor_name));
}
function horizontal_center(x1, x2) {
if (x1 > x2)
return (x2 - x1)/2 + x1 + "%";
else
return (x1 - x2)/2 + x2 + "%";
}
function horizontal_percent_to_coord(percent) {
return svg.property("scrollWidth") * Number.parseFloat(percent)/100;
}
function arc_to_self(d, i) {
var ry = 20;
var x_percent = actor_horizontal_center_percent(d.from),
x = horizontal_percent_to_coord(x_percent),
y = (i+1)*50 - ry,
sweep_flag = actor_names.indexOf(d.from) == 0 ? 0 : 1;
return "M "+ x + " " + y + " " +
"a 40 " + ry + " 0 1 " + sweep_flag + " 0 " + ry*2;
}
function fade_message() {
svg.select("g.message")
.transition()
.duration(fade_time)
.style("opacity", 0)
.remove()
.on("end", function() {
data.shift();
remove_free_actors();
update();
if (data.length > 0)
fade_message();
});
}
function update_message_elements(messages) {
messages.select("line")
.transition()
.duration(animation_time)
.attr("x1", function(d) { return actor_horizontal_center_percent(d.from) })
.attr("y1", function(d, i) { return (i+1)*50 })
.attr("x2", function(d) { return actor_horizontal_center_percent(d.to) })
.attr("y2", function(d, i) { return (i+1)*50 });
messages.select("path")
.transition()
.duration(animation_time)
.attr("d", arc_to_self);
messages.select("text")
.transition()
.duration(animation_time)
.attr("x", function(d) {
var x1 = Number.parseFloat(actor_horizontal_center_percent(d.from)),
x2 = Number.parseFloat(actor_horizontal_center_percent(d.to));
return horizontal_center(x1, x2);
})
.attr("y", function(d, i) { return (i+1)*50 - 5 });
}
var messages = svg.selectAll("g.message").data(data);
update_message_elements(messages);
update_message_elements(messages.exit());
var actors = svg.select("g.actors").selectAll("g.actor").data(actor_names, function(d) { return d });
actors.exit()
.transition()
.duration(animation_time)
.style("opacity", 0)
.remove();
var new_actors = actors.enter().append("g");
actors = actors.merge(new_actors);
new_actors.classed("actor", true)
.append("text");
new_actors.append("line");
actors.select("text")
.attr("text-anchor", "middle")
.attr("alignment-baseline", "middle")
.style("font-size", function(d, i) { return 1 + 10/(10 + d.length) + "vw" })
.text(function(d) { return d })
.attr("dy", "20")
.transition()
.duration(animation_time)
.attr("x", index_horizontal_center_percent);
actors.select("line")
.attr("data-actor", function(d) { return d })
.attr("y1", function(_d, i) {
var text_bbox = this.parentNode.querySelector("text").getBBox();
return text_bbox.y + text_bbox.height;
})
.attr("y2", "100%")
.transition()
.duration(animation_time)
.attr("x1", index_horizontal_center_percent)
.attr("x2", index_horizontal_center_percent)
.on("end", function() {
var new_messages = messages
.enter()
.append("g")
.classed("message", true);
messages = messages.merge(new_messages);
new_messages.each(function(d, i) {
var new_message = d3.select(this);
if (d.from == d.to) {
new_message.append("path")
.attr("marker-end", "url(#msg-arrow)")
.attr("fill-opacity", 0)
.attr("d", function() { return arc_to_self(d, i) });
}
else {
new_message.append("line")
.attr("marker-end", "url(#msg-arrow)")
.attr("x1", svg.select("line[data-actor='" + d.from + "']").attr("x1"))
.attr("y1", (i+1)*50)
.attr("x2", svg.select("line[data-actor='" + d.to + "']").attr("x1"))
.attr("y2", (i+1)*50);
}
new_message.append("text")
.attr("text-anchor", d.from == d.to ? "end" : "middle")
.attr("alignment-baseline", d.from == d.to ? "middle" : "auto")
.text(d.msg)
.attr("x", function() {
var x1 = Number.parseFloat(svg.select("line[data-actor='" + d.from + "']").attr("x1")),
x2 = Number.parseFloat(svg.select("line[data-actor='" + d.to + "']").attr("x1"));
return horizontal_center(x1, x2);
})
.attr("y", (i+1)*50 - 5);
});
if (fade_time > 0 && data.length == 1)
fade_message();
animation_in_progress = false;
if (update_pending) {
update_pending = false;
update();
}
});
};
});
};
chart.fade = function(time) {
if (!arguments.length)
return fade_time;
fade_time = time;
return chart;
};
chart.addMessage = function(msg) {
data.push(msg);
if (actor_names.indexOf(msg.from) < 0)
actor_names.push(msg.from);
if (actor_names.indexOf(msg.to) < 0)
actor_names.push(msg.to);
update();
return chart;
};
return chart;
}