forked from geoffreybr/d3-parliament
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3-parliament.min.js
1 lines (1 loc) · 2.9 KB
/
d3-parliament.min.js
1
d3.parliament=function(){function t(t){t.each(function(t){var u=Math.min(e/2,a),c=u*n,s=d3.select(this);s.classed("d3-parliament",!0),s.attr("width",e),s.attr("height",a);var f=0;t.forEach(function(t){f+="number"==typeof t.seats?Math.floor(t.seats):t.seats.length});var m=0,h=0,p=.5;!function(){for(var t=n/(1-n);h<f;)m++,p+=t,h=r(function(t){return Math.floor(Math.PI*(p+t))},m-1)}();var d=(u-c)/m,v=[];!function(){for(var t=h-f,r=0;r<m;r++)for(var e=c+d*(r+.5),a=Math.floor(Math.PI*(p+r))-Math.floor(t/m)-(t%m>r?1:0),n=Math.PI/a,o=0;o<a;o++){var i={};i.polar={r:e,teta:-Math.PI+n*(o+.5)},i.cartesian={x:i.polar.r*Math.cos(i.polar.teta),y:i.polar.r*Math.sin(i.polar.teta)},v.push(i)}}(),v.sort(function(t,r){return t.polar.teta-r.polar.teta||r.polar.r-t.polar.r}),function(){var r=0,e=0;v.forEach(function(a){var n=t[r],o="number"==typeof n.seats?n.seats:n.seats.length;e>=o&&(r++,e=0,n=t[r]),a.party=n,a.data="number"==typeof n.seats?null:n.seats[e],e++})}();var g=function(t){var r="seat ";return r+=t.party&&t.party.id||"",r.trim()},C=function(t){return t.cartesian.x},y=function(t){return t.cartesian.y},M=function(t){var r=.4*d;return t.data&&"number"==typeof t.data.size&&(r*=t.data.size),r},T=s.select(".parliament");T.empty()&&(T=s.append("g"),T.classed("parliament",!0)),T.attr("transform","translate("+e/2+","+u+")");var b=T.selectAll(".seat").data(v);b.attr("class",g);var x=b.enter().append("circle");if(x.attr("class",g),x.attr("cx",o.fromCenter?0:C),x.attr("cy",o.fromCenter?0:y),x.attr("r",o.smallToBig?0:M),o.fromCenter||o.smallToBig){var B=x.transition().duration(function(){return 1e3+800*Math.random()});o.fromCenter&&(B.attr("cx",C),B.attr("cy",y)),o.smallToBig&&B.attr("r",M)}for(var S in l._)!function(t){x.on(t,function(r){l.call(t,this,r)})}(S);if(b.transition().duration(function(){return 1e3+800*Math.random()}).attr("cx",C).attr("cy",y).attr("r",M),i.toCenter||i.bigToSmall){var B=b.exit().transition().duration(function(){return 1e3+800*Math.random()});i.toCenter&&B.attr("cx",0).attr("cy",0),i.bigToSmall&&B.attr("r",0),B.remove()}else b.exit().remove()})}function r(t,r){for(var e=0,a=0;a<=r;a++)e+=t(a);return e}var e=500,a=500,n=.4,o={smallToBig:!0,fromCenter:!0},i={bigToSmall:!0,toCenter:!0},l=d3.dispatch("click","dblclick","mousedown","mouseenter","mouseleave","mousemove","mouseout","mouseover","mouseup","touchcancel","touchend","touchmove","touchstart");return t.width=function(r){return arguments.length?(e=r,t):e},t.height=function(r){return arguments.length?(a=r,t):a},t.innerRadiusCoef=function(r){return arguments.length?(n=r,t):n},t.enter={smallToBig:function(r){return arguments.length?(o.smallToBig=r,t.enter):o.smallToBig},fromCenter:function(r){return arguments.length?(o.fromCenter=r,t.enter):o.fromCenter}},t.exit={bigToSmall:function(r){return arguments.length?(i.bigToSmall=r,t.exit):i.bigToSmall},toCenter:function(r){return arguments.length?(i.toCenter=r,t.exit):i.toCenter}},t.on=function(t,r){l.on(t,r)},t};