-
Notifications
You must be signed in to change notification settings - Fork 2
/
psd3.min.js
2 lines (2 loc) · 5.54 KB
/
psd3.min.js
1
2
var psd3=psd3||{};psd3.Graph=function(i){var t=this;this.config=i,this.defaults={width:400,height:400,value:"value",inner:"inner",label:function(i){return i.data.value},tooltip:function(i){return void 0!==t.config.value?i[t.config.value]:i.value},transition:"linear",transitionDuration:1e3,donutRadius:0,gradient:!1,colors:d3.scale.category20(),labelColor:"black",drilldownTransition:"linear",drilldownTransitionDuration:0,stroke:"white",strokeWidth:2,highlightColor:"orange"};for(var n in this.defaults)this.defaults.hasOwnProperty(n)&&(i.hasOwnProperty(n)||(i[n]=this.defaults[n]))};
var psd3=psd3||{};psd3.Pie=function(t){psd3.Graph.call(this,t),this.zoomStack=[];var i="top";void 0!==this.config.heading&&void 0!==this.config.heading.pos&&(i=this.config.heading.pos),"top"==i&&this.setHeading(),this.drawPie(t.data),"bottom"==i&&this.setHeading()},psd3.Pie.prototype=Object.create(psd3.Graph.prototype),psd3.Pie.prototype.constructor=psd3.Pie,psd3.Pie.prototype.findMaxDepth=function(t){if(null===t||void 0===t||t.length<1)return 0;for(var i,e=0,n=0;n<t.length;n++){var o=this.findMaxDepth(t[n][this.config.inner]);o>e&&(e=o)}return i=1+e},psd3.Pie.prototype.setHeading=function(){void 0!==this.config.heading&&d3.select("#"+this.config.containerId).append("div").style("text-align","center").style("width",""+this.config.width+"px").style("padding-top","20px").style("padding-bottom","20px").append("strong").text(this.config.heading.text)},psd3.Pie.prototype.mouseover=function(t){d3.select("#"+_this.tooltipId).style("left",d3.event.clientX+window.scrollX+"px").style("top",d3.event.clientY+window.scrollY+"px").select("#value").html(_this.config.tooltip(t.data,_this.config.label)),d3.select("#"+_this.tooltipId).classed("psd3Hidden",!1),d3.select(t.path).style("fill",_this.config.highlightColor)},psd3.Pie.prototype.mouseout=function(t){d3.select("#"+_this.tooltipId).classed("psd3Hidden",!0),d3.select(t.path).style("fill",t.fill)},psd3.Pie.prototype.drawPie=function(t){if(!(null===t||void 0===t||t.length<1)){_this=this,_this.arcIndex=0;var i=d3.select("#"+_this.config.containerId).append("svg").attr("id",_this.config.containerId+"_svg").attr("width",_this.config.width).attr("height",_this.config.height);_this.tooltipId=_this.config.containerId+"_tooltip";var e=d3.select("#"+_this.config.containerId).append("div").attr("id",_this.tooltipId).attr("class","psd3Hidden psd3Tooltip");e.append("p").append("span").attr("id","value").text("100%");var n;n=_this.config.width>_this.config.height?_this.config.width/2:_this.config.height/2;var o=_this.config.donutRadius,s=_this.findMaxDepth(t),a=o+(n-o)/s,r=a-o;_this.draw(i,n,t,t,t.length,o,a,r,0,7920/7/180,[0,0])}},psd3.Pie.prototype.customArcTween=function(t){var i={startAngle:t.startAngle,endAngle:t.startAngle},e=d3.interpolate(i,t);return function(i){return t.arc(e(i))}},psd3.Pie.prototype.textTransform=function(t){return"translate("+t.arc.centroid(t)+")"},psd3.Pie.prototype.textTitle=function(t){return t.data[_this.config.value]},psd3.Pie.prototype.draw=function(t,i,e,n,o,s,a,r,d,h,c){if(_this=this,!(null===e||void 0===e||e.length<1)){psd3.Pie.prototype.textText=function(t){return _this.config.label(t)};var l=d3.layout.pie();l.sort(null),l.value(function(t){return t[_this.config.value]}),l.startAngle(d).endAngle(h);for(var p=[],g=0;g<e.length;g++)p.push(e[g][_this.config.value]);var f=function(t){_this.reDrawPie(t,n)},u=d3.svg.arc().innerRadius(s).outerRadius(a);_this.arcIndex=_this.arcIndex+1;var _,v,x="arc"+_this.arcIndex,y=function(t){t.path=this,t.fill=this.fill,t.arc=u,t.length=e.length},w=t.selectAll("g."+x).data(l(e)).enter().append("g").attr("class","arc "+x).attr("transform","translate("+i+","+i+")").on("dblclick",f),I=t.append("svg:defs").append("svg:linearGradient").attr("id","gradient_"+_this.arcIndex).attr("x1","0%").attr("y1","0%").attr("x2","100%").attr("y2","100%").attr("spreadMethod","pad");if(_this.config.gradient){var m=2*_this.arcIndex,P=m+1;_=_this.config.colors(m),v=_this.config.colors(P)}else _=v=_this.config.colors(this.arcIndex);I.append("svg:stop").attr("offset","0%").attr("stop-color",_).attr("stop-opacity",1),I.append("svg:stop").attr("offset","100%").attr("stop-color",v).attr("stop-opacity",1);var A=w.append("path").attr("fill","url(#gradient_"+_this.arcIndex+")").style("stroke",_this.config.stroke).style("stroke-width",_this.config.strokeWidth);A.on("mouseover",_this.mouseover),A.on("mouseout",_this.mouseout),A.each(y),A.transition().duration(_this.config.transitionDuration).delay(_this.config.transitionDuration*(_this.arcIndex-1)).ease(_this.config.transition).attrTween("d",_this.customArcTween);for(var T=(w.append("text").attr("x",function(){return c[0]}).attr("y",function(){return c[1]}).transition().ease(_this.config.transition).duration(_this.config.transitionDuration).delay(_this.config.transitionDuration*(_this.arcIndex-1)).attr("transform",function(t){var i=[];return i[0]=u.centroid(t)[0]-c[0],i[1]=u.centroid(t)[1]-c[1],"translate("+i+")"}).attr("text-anchor","middle").text(_this.textText).style("fill",_this.config.labelColor).attr("title",_this.textTitle),0);T<e.length;T++)void 0!==e[T][_this.config.inner]&&_this.draw(t,i,e[T][_this.config.inner],n,o,s+r,a+r,r,A.data()[T].startAngle,A.data()[T].endAngle,u.centroid(A.data()[T]))}},psd3.Pie.prototype.reDrawPie=function(t,i){var e=[];d3.select("#"+_this.tooltipId).remove(),d3.select("#"+_this.config.containerId+"_svg").transition().ease(_this.config.drilldownTransition).duration(_this.config.drilldownTransitionDuration).style("height",0).remove().each("end",function(){1==t.length?e=_this.zoomStack.pop():(e.push(t.data),_this.zoomStack.push(i)),_this.drawPie(e)})};