forked from smilli/d3-funnel-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3-funnel-charts.min.js
1 lines (1 loc) · 2.1 KB
/
d3-funnel-charts.min.js
1
function FunnelChart(e,t,n,r){var i=400,s=600,o=1/3;this.data=e;this.totalEngagement=0;for(var u=0;u<e.length;u++){this.totalEngagement+=e[u][1]}this.width=typeof t!=="undefined"?t:s;this.height=typeof n!=="undefined"?n:i;this.bottomPct=typeof r!=="undefined"?r:o;this.slope=2*this.height/(this.width-this.bottomPct*this.width);this.totalArea=(this.width+this.bottomPct*this.width)*this.height/2}FunnelChart.prototype.getLabel=function(e){return this.data[e][0]};FunnelChart.prototype.getEngagementCount=function(e){return this.data[e][1]};FunnelChart.prototype.createPaths=function(){function e(t,n,r,i,s){if(s>=t.data.length)return;area=t.data[s][1]*t.totalArea/t.totalEngagement;prevBaseLength=r-n;nextBaseLength=Math.sqrt((t.slope*prevBaseLength*prevBaseLength-4*area)/t.slope);nextLeftX=(prevBaseLength-nextBaseLength)/2+n;nextRightX=r-(prevBaseLength-nextBaseLength)/2;nextHeight=t.slope*(prevBaseLength-nextBaseLength)/2+i;points=[[nextRightX,nextHeight]];points.push([r,i]);points.push([n,i]);points.push([nextLeftX,nextHeight]);points.push([nextRightX,nextHeight]);trapezoids.push(points);e(t,nextLeftX,nextRightX,nextHeight,s+1)}trapezoids=[];e(this,0,this.width,0,0);return trapezoids};FunnelChart.prototype.draw=function(e,t){function u(e,n){var a=r.append("svg:path").attr("d",function(e){return i([o[n][0],o[n][1],o[n][2],o[n][2],o[n][1],o[n][2]])}).attr("fill","#fff");nextHeight=o[n][[o[n].length]-1];var f=a.node().getTotalLength();var l=a.transition().duration(f/t).ease("linear").attr("d",function(e){return i(o[n])}).attr("fill",function(e){return s(n)});r.append("text").text(e.getLabel(n)+": "+e.getEngagementCount(n)).attr("x",function(t){return e.width/2}).attr("y",function(e){return(o[n][0][1]+o[n][1][1])/2}).attr("text-anchor","middle").attr("dominant-baseline","middle").attr("fill","#fff");if(n<o.length-1){l.each("end",function(){u(e,n+1)})}}var n=2.5;t=typeof t!=="undefined"?t:n;var r=d3.select(e).append("svg").attr("width",this.width).attr("height",this.height).append("g");var i=d3.svg.line().x(function(e){return e[0]}).y(function(e){return e[1]});var s=d3.scale.category10();var o=this.createPaths();u(this,0)}