1 line
7.7 KiB
JavaScript
1 line
7.7 KiB
JavaScript
!function(e){var i={series:{pie:{show:!1,radius:"auto",innerRadius:0,startAngle:1.5,tilt:1,shadow:{left:5,top:15,alpha:.02},offset:{top:0,left:"auto"},stroke:{color:"#fff",width:1},label:{show:"auto",formatter:function(e,i){return"<div style='font-size:x-small;text-align:center;padding:2px;color:"+i.color+";'>"+e+"<br/>"+Math.round(i.percent)+"%</div>"},radius:1,background:{color:null,opacity:0},threshold:0},combine:{threshold:-1,color:null,label:"Other"},highlight:{opacity:.5}}}};e.plot.plugins.push({init:function(s){var t=null,r=null,a=null,l=null,n=null,o=!1,p=null,h=[];function c(s,a,l){o||(o=!0,t=s.getCanvas(),r=e(t).parent(),i=s.getOptions(),s.setData(function(s){for(var t=0,r=0,a=0,l=i.series.pie.combine.color,n=[],o=0;o<s.length;++o){var p=s[o].data;e.isArray(p)?e.isNumeric(p[1])?p[1]=+p[1]:p[1]=0:p=e.isNumeric(p)?[1,+p]:[1,0],s[o].data=[p]}for(o=0;o<s.length;++o)t+=s[o].data[0][1];for(o=0;o<s.length;++o)(p=s[o].data[0][1])/t<=i.series.pie.combine.threshold&&(r+=p,a++,l||(l=s[o].color));for(o=0;o<s.length;++o)p=s[o].data[0][1],(a<2||p/t>i.series.pie.combine.threshold)&&n.push({data:[[1,p]],color:s[o].color,label:s[o].label,angle:p*Math.PI*2/t,percent:p/(t/100)});return a>1&&n.push({data:[[1,r]],color:l,label:i.series.pie.combine.label,angle:r*Math.PI*2/t,percent:r/(t/100)}),n}(s.getData())))}function g(s,t){if(r){var h=s.getPlaceholder().width(),c=s.getPlaceholder().height(),g=r.children().filter(".legend").children().width()||0;p=t,o=!1,a=Math.min(h,c/i.series.pie.tilt)/2,n=c/2+i.series.pie.offset.top,l=h/2,"auto"==i.series.pie.offset.left?i.legend.position.match("w")?l+=g/2:l-=g/2:l+=i.series.pie.offset.left,l<a?l=a:l>h-a&&(l=h-a);var d=s.getData(),f=0;do{f>0&&(a*=.95),f+=1,v(),i.series.pie.tilt<=.8&&b()}while(!w()&&f<10);f>=10&&(v(),r.prepend("<div class='error'>Could not draw pie with labels contained inside canvas</div>")),s.setSeries&&s.insertLegend&&(s.setSeries(d),s.insertLegend())}function v(){p.clearRect(0,0,h,c),r.children().filter(".pieLabel, .pieLabelBackground").remove()}function b(){var e=i.series.pie.shadow.left,s=i.series.pie.shadow.top,t=i.series.pie.shadow.alpha,r=i.series.pie.radius>1?i.series.pie.radius:a*i.series.pie.radius;if(!(r>=h/2-e||r*i.series.pie.tilt>=c/2-s||r<=10)){p.save(),p.translate(e,s),p.globalAlpha=t,p.fillStyle="#000",p.translate(l,n),p.scale(1,i.series.pie.tilt);for(var o=1;o<=10;o++)p.beginPath(),p.arc(0,0,r,0,2*Math.PI,!1),p.fill(),r-=o;p.restore()}}function w(){var s=Math.PI*i.series.pie.startAngle,t=i.series.pie.radius>1?i.series.pie.radius:a*i.series.pie.radius;p.save(),p.translate(l,n),p.scale(1,i.series.pie.tilt),p.save();for(var o=s,g=0;g<d.length;++g)d[g].startAngle=o,f(d[g].angle,d[g].color,!0);if(p.restore(),i.series.pie.stroke.width>0){for(p.save(),p.lineWidth=i.series.pie.stroke.width,o=s,g=0;g<d.length;++g)f(d[g].angle,i.series.pie.stroke.color,!1);p.restore()}return u(p),p.restore(),!i.series.pie.label.show||function(){for(var t=s,o=i.series.pie.label.radius>1?i.series.pie.label.radius:a*i.series.pie.label.radius,p=0;p<d.length;++p){if(d[p].percent>=100*i.series.pie.label.threshold&&!g(d[p],t,p))return!1;t+=d[p].angle}return!0;function g(s,t,a){if(0==s.data[0][1])return!0;var p,g=i.legend.labelFormatter,u=i.series.pie.label.formatter;p=g?g(s.label,s):s.label,u&&(p=u(p,s));var d=(t+s.angle+t)/2,f=l+Math.round(Math.cos(d)*o),v=n+Math.round(Math.sin(d)*o)*i.series.pie.tilt,b="<span class='pieLabel' id='pieLabel"+a+"' style='position:absolute;top:"+v+"px;left:"+f+"px;'>"+p+"</span>";r.append(b);var w=r.children("#pieLabel"+a),k=v-w.height()/2,M=f-w.width()/2;if(w.css("top",k),w.css("left",M),0-k>0||0-M>0||c-(k+w.height())<0||h-(M+w.width())<0)return!1;if(0!=i.series.pie.label.background.opacity){var P=i.series.pie.label.background.color;null==P&&(P=s.color);var m="top:"+k+"px;left:"+M+"px;";e("<div class='pieLabelBackground' style='position:absolute;width:"+w.width()+"px;height:"+w.height()+"px;"+m+"background-color:"+P+";'></div>").css("opacity",i.series.pie.label.background.opacity).insertBefore(w)}return!0}}();function f(e,i,s){e<=0||isNaN(e)||(s?p.fillStyle=i:(p.strokeStyle=i,p.lineJoin="round"),p.beginPath(),Math.abs(e-2*Math.PI)>1e-9&&p.moveTo(0,0),p.arc(0,0,t,o,o+e/2,!1),p.arc(0,0,t,o+e/2,o+e,!1),p.closePath(),o+=e,s?p.fill():p.stroke())}}}function u(e){if(i.series.pie.innerRadius>0){e.save();var s=i.series.pie.innerRadius>1?i.series.pie.innerRadius:a*i.series.pie.innerRadius;e.globalCompositeOperation="destination-out",e.beginPath(),e.fillStyle=i.series.pie.stroke.color,e.arc(0,0,s,0,2*Math.PI,!1),e.fill(),e.closePath(),e.restore(),e.save(),e.beginPath(),e.strokeStyle=i.series.pie.stroke.color,e.arc(0,0,s,0,2*Math.PI,!1),e.stroke(),e.closePath(),e.restore()}}function d(e,i){for(var s=!1,t=-1,r=e.length,a=r-1;++t<r;a=t)(e[t][1]<=i[1]&&i[1]<e[a][1]||e[a][1]<=i[1]&&i[1]<e[t][1])&&i[0]<(e[a][0]-e[t][0])*(i[1]-e[t][1])/(e[a][1]-e[t][1])+e[t][0]&&(s=!s);return s}function f(e){b("plothover",e)}function v(e){b("plotclick",e)}function b(e,t){var o=s.offset(),c=function(e,i){for(var t,r,o=s.getData(),h=s.getOptions(),c=h.series.pie.radius>1?h.series.pie.radius:a*h.series.pie.radius,g=0;g<o.length;++g){var u=o[g];if(u.pie.show){if(p.save(),p.beginPath(),p.moveTo(0,0),p.arc(0,0,c,u.startAngle,u.startAngle+u.angle/2,!1),p.arc(0,0,c,u.startAngle+u.angle/2,u.startAngle+u.angle,!1),p.closePath(),t=e-l,r=i-n,p.isPointInPath){if(p.isPointInPath(e-l,i-n))return p.restore(),{datapoint:[u.percent,u.data],dataIndex:0,series:u,seriesIndex:g}}else if(d([[0,0],[c*Math.cos(u.startAngle),c*Math.sin(u.startAngle)],[c*Math.cos(u.startAngle+u.angle/4),c*Math.sin(u.startAngle+u.angle/4)],[c*Math.cos(u.startAngle+u.angle/2),c*Math.sin(u.startAngle+u.angle/2)],[c*Math.cos(u.startAngle+u.angle/1.5),c*Math.sin(u.startAngle+u.angle/1.5)],[c*Math.cos(u.startAngle+u.angle),c*Math.sin(u.startAngle+u.angle)]],[t,r]))return p.restore(),{datapoint:[u.percent,u.data],dataIndex:0,series:u,seriesIndex:g};p.restore()}}return null}(parseInt(t.pageX-o.left),parseInt(t.pageY-o.top));if(i.grid.autoHighlight)for(var g=0;g<h.length;++g){var u=h[g];u.auto!=e||c&&u.series==c.series||w(u.series)}c&&function(e,i){var t=k(e);-1==t?(h.push({series:e,auto:i}),s.triggerRedrawOverlay()):i||(h[t].auto=!1)}(c.series,e);var f={pageX:t.pageX,pageY:t.pageY};r.trigger(e,[f,c])}function w(e){null==e&&(h=[],s.triggerRedrawOverlay());var i=k(e);-1!=i&&(h.splice(i,1),s.triggerRedrawOverlay())}function k(e){for(var i=0;i<h.length;++i)if(h[i].series==e)return i;return-1}s.hooks.processOptions.push((function(e,i){i.series.pie.show&&(i.grid.show=!1,"auto"==i.series.pie.label.show&&(i.legend.show?i.series.pie.label.show=!1:i.series.pie.label.show=!0),"auto"==i.series.pie.radius&&(i.series.pie.label.show?i.series.pie.radius=.75:i.series.pie.radius=1),i.series.pie.tilt>1?i.series.pie.tilt=1:i.series.pie.tilt<0&&(i.series.pie.tilt=0))})),s.hooks.bindEvents.push((function(e,i){var s=e.getOptions();s.series.pie.show&&(s.grid.hoverable&&i.unbind("mousemove").mousemove(f),s.grid.clickable&&i.unbind("click").click(v))})),s.hooks.processDatapoints.push((function(e,i,s,t){e.getOptions().series.pie.show&&c(e)})),s.hooks.drawOverlay.push((function(e,i){e.getOptions().series.pie.show&&function(e,i){var s=e.getOptions(),t=s.series.pie.radius>1?s.series.pie.radius:a*s.series.pie.radius;i.save(),i.translate(l,n),i.scale(1,s.series.pie.tilt);for(var r=0;r<h.length;++r)o(h[r].series);function o(e){e.angle<=0||isNaN(e.angle)||(i.fillStyle="rgba(255, 255, 255, "+s.series.pie.highlight.opacity+")",i.beginPath(),Math.abs(e.angle-2*Math.PI)>1e-9&&i.moveTo(0,0),i.arc(0,0,t,e.startAngle,e.startAngle+e.angle/2,!1),i.arc(0,0,t,e.startAngle+e.angle/2,e.startAngle+e.angle,!1),i.closePath(),i.fill())}u(i),i.restore()}(e,i)})),s.hooks.draw.push((function(e,i){e.getOptions().series.pie.show&&g(e,i)}))},options:i,name:"pie",version:"1.1"})}(jQuery); |