forked from koudelka/d3-message-sequence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3-msg-seq.min.js
7 lines (7 loc) · 3.36 KB
/
d3-msg-seq.min.js
1
2
3
4
5
6
7
d3.messageSequence=function(){function v(){var a=g.reduce(function(d,a){0>d.indexOf(a.from)&&d.push(a.from);0>d.indexOf(a.to)&&d.push(a.to);return d},[]);c=c.filter(function(d){return-1<a.indexOf(d)})}function w(a,d){0===a.size()&&d();var c=0;a.each(function(){++c}).each("end",function(){--c||d.apply(this,arguments)})}function e(a){a.each(function(){var d=d3.select(this);d.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");d.append("g").classed("actors",!0);h=function(){function a(q,f){return 100/(c.length+1)*(f+1)+"%"}function e(q){return a(null,c.indexOf(q))}function r(a,f){var b;b=e(a.from);b=d.property("scrollWidth")*Number.parseFloat(b)/100;var x=50*(f+1)-20,y=0==c.indexOf(a.from)?0:1;return"M "+b+" "+x+" a 40 20 0 1 "+y+" 0 40"}function t(a){a.select("line").transition().ease("cubic").duration(250).attr("x1",function(f){return e(f.from)}).attr("y1",
function(f,a){return 50*(a+1)}).attr("x2",function(a){return e(a.to)}).attr("y2",function(a,b){return 50*(b+1)});a.select("path").transition().ease("cubic").duration(250).attr("d",r);a.select("text").transition().ease("cubic").duration(250).attr("x",function(a){var b=Number.parseFloat(e(a.from));a=Number.parseFloat(e(a.to));return b>a?(a-b)/2+b+"%":(b-a)/2+a+"%"}).attr("y",function(a,b){return 50*(b+1)-5})}if(m)n=!0;else{m=!0;var p=d.selectAll("g.message").data(g);t(p);t(p.exit());var k=d.select("g.actors").selectAll("g.actor").data(c,
function(a){return a});k.exit().transition().ease("cubic").duration(250).style("opacity",0).remove();var u=k.enter().append("g");u.classed("actor",!0).append("text");u.append("line");k.select("text").attr("text-anchor","middle").attr("alignment-baseline","middle").style("font-size",function(a,f){return 1+10/(10+a.length)+"vw"}).text(function(a){return a}).attr("dy","20").transition().ease("cubic").duration(250).attr("x",a);k.select("line").attr("data-actor",function(a){return a}).attr("y1",function(a,
f){var b=this.parentNode.querySelector("text").getBBox();return b.y+b.height}).attr("y2","100%").transition().ease("cubic").duration(250).attr("x1",a).attr("x2",a).call(w,function(){var a=p.enter().append("g").classed("message",!0);a.each(function(a,b){var c=d3.select(this);a.from==a.to?c.append("path").attr("marker-end","url(#msg-arrow)").attr("fill-opacity",0).attr("d",function(){return r(a,b)}):c.append("line").attr("marker-end","url(#msg-arrow)").attr("x1",d.select("line[data-actor='"+a.from+
"']").attr("x1")).attr("y1",50*(b+1)).attr("x2",d.select("line[data-actor='"+a.to+"']").attr("x1")).attr("y2",50*(b+1));c.append("text").attr("text-anchor",a.from==a.to?"end":"middle").attr("alignment-baseline",a.from==a.to?"middle":"auto").text(a.msg).attr("x",function(){var b=Number.parseFloat(d.select("line[data-actor='"+a.from+"']").attr("x1")),c=Number.parseFloat(d.select("line[data-actor='"+a.to+"']").attr("x1"));return b>c?(c-b)/2+b+"%":(b-c)/2+c+"%"}).attr("y",50*(b+1)-5)});0<l&&a.transition().ease("cubic").duration(l).style("opacity",
0).remove().each("end",function(){g.shift();v();h()});m=!1;n&&(n=!1,h())})}}})}var l=2E3,g=[],c=[],h,m,n;e.fade=function(a){if(!arguments.length)return l;l=a;return e};e.addMessage=function(a){g.push(a);0>c.indexOf(a.from)&&c.push(a.from);0>c.indexOf(a.to)&&c.push(a.to);h();return e};return e};