From 080a2cc8365a9182bd04ddf149101e5418a92df3 Mon Sep 17 00:00:00 2001 From: Ben Keen Date: Sat, 7 Jun 2014 16:06:57 -0700 Subject: [PATCH] closes #7 --- d3pie-source/_labels.js | 1 - d3pie-source/_text.js | 17 +++++++++++++++-- d3pie/d3pie.js | 18 +++++++++++++++--- d3pie/d3pie.min.js | 2 +- website/examples/single.html | 2 +- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/d3pie-source/_labels.js b/d3pie-source/_labels.js index 2960acb..e92994c 100644 --- a/d3pie-source/_labels.js +++ b/d3pie-source/_labels.js @@ -25,7 +25,6 @@ var labels = { .attr("class", pie.cssPrefix + "labelGroup-" + section) .style("opacity", 0); - // 1. Add the main label if (include.mainLabel) { labelGroup.append("text") diff --git a/d3pie-source/_text.js b/d3pie-source/_text.js index 44c87d3..b250959 100644 --- a/d3pie-source/_text.js +++ b/d3pie-source/_text.js @@ -33,7 +33,12 @@ var text = { var canvasWidth = pie.options.size.canvasWidth; var titleSubtitlePadding = pie.options.header.titleSubtitlePadding; - var x = (headerLocation === "top-left") ? canvasPadding.left : canvasWidth / 2; + var x; + if (headerLocation === "top-left") { + x = canvasPadding.left; + } else { + x = ((canvasWidth - canvasPadding.right) / 2) + canvasPadding.left; + } var y = canvasPadding.top + textComponents.title.h; if (headerLocation === "pie-center") { @@ -80,7 +85,15 @@ var text = { }, positionSubtitle: function(pie) { - var x = (pie.options.header.location === "top-left") ? pie.options.misc.canvasPadding.left : pie.options.size.canvasWidth / 2; + var canvasPadding = pie.options.misc.canvasPadding; + var canvasWidth = pie.options.size.canvasWidth; + + var x; + if (pie.options.header.location === "top-left") { + x = canvasPadding.left; + } else { + x = ((canvasWidth - canvasPadding.right) / 2) + canvasPadding.left; + } var y = text.getHeaderHeight(pie); pie.svg.select("#" + pie.cssPrefix + "subtitle") .attr("x", x) diff --git a/d3pie/d3pie.js b/d3pie/d3pie.js index 1543bae..4a706ac 100644 --- a/d3pie/d3pie.js +++ b/d3pie/d3pie.js @@ -613,7 +613,6 @@ var labels = { .attr("class", pie.cssPrefix + "labelGroup-" + section) .style("opacity", 0); - // 1. Add the main label if (include.mainLabel) { labelGroup.append("text") @@ -1348,7 +1347,12 @@ var text = { var canvasWidth = pie.options.size.canvasWidth; var titleSubtitlePadding = pie.options.header.titleSubtitlePadding; - var x = (headerLocation === "top-left") ? canvasPadding.left : canvasWidth / 2; + var x; + if (headerLocation === "top-left") { + x = canvasPadding.left; + } else { + x = ((canvasWidth - canvasPadding.right) / 2) + canvasPadding.left; + } var y = canvasPadding.top + textComponents.title.h; if (headerLocation === "pie-center") { @@ -1395,7 +1399,15 @@ var text = { }, positionSubtitle: function(pie) { - var x = (pie.options.header.location === "top-left") ? pie.options.misc.canvasPadding.left : pie.options.size.canvasWidth / 2; + var canvasPadding = pie.options.misc.canvasPadding; + var canvasWidth = pie.options.size.canvasWidth; + + var x; + if (pie.options.header.location === "top-left") { + x = canvasPadding.left; + } else { + x = ((canvasWidth - canvasPadding.right) / 2) + canvasPadding.left; + } var y = text.getHeaderHeight(pie); pie.svg.select("#" + pie.cssPrefix + "subtitle") .attr("x", x) diff --git a/d3pie/d3pie.min.js b/d3pie/d3pie.min.js index 68f6604..439a55c 100644 --- a/d3pie/d3pie.min.js +++ b/d3pie/d3pie.min.js @@ -1 +1 @@ -!function(a){"use strict";var b="d3pie",c="0.1.2",d=0,e={header:{title:{text:"",color:"#333333",fontSize:18,font:"arial"},subtitle:{text:"",color:"#666666",fontSize:14,font:"arial"},location:"top-center",titleSubtitlePadding:8},footer:{text:"",color:"#666666",fontSize:14,font:"arial",location:"left"},size:{canvasHeight:500,canvasWidth:500,pieInnerRadius:"0%",pieOuterRadius:null},data:{sortOrder:"none",content:[]},labels:{outer:{format:"label",hideWhenLessThanPercentage:null,pieDistance:30},inner:{format:"percentage",hideWhenLessThanPercentage:null},mainLabel:{color:"#333333",font:"arial",fontSize:10},percentage:{color:"#dddddd",font:"arial",fontSize:10,decimalPlaces:0},value:{color:"#cccc44",font:"arial",fontSize:10},lines:{enabled:!0,style:"curved",color:"segment"},truncation:{enabled:!1,length:30}},effects:{load:{effect:"default",speed:1e3},pullOutSegmentOnClick:{effect:"bounce",speed:300,size:10},highlightSegmentOnMouseover:!0,highlightLuminosity:-.2},misc:{colors:{background:null,segments:["#2484c1","#65a620","#7b6888","#a05d56","#961a1a","#d8d23a","#e98125","#d0743c","#635222","#6ada6a","#0c6197","#7d9058","#207f33","#44b9b0","#bca44a","#e4a14b","#a3acb2","#8cc3e9","#69a6f9","#5b388f","#546e91","#8bde95","#d2ab58","#273c71","#98bf6e","#4daa4b","#98abc5","#cc1010","#31383b","#006391","#c2643f","#b0a474","#a5a39c","#a9c2bc","#22af8c","#7fcecf","#987ac6","#3d3b87","#b77b1c","#c9c2b6","#807ece","#8db27c","#be66a2","#9ed3c6","#00644b","#005064","#77979f","#77e079","#9c73ab","#1f79a7"],segmentStroke:"#ffffff"},gradient:{enabled:!1,percentage:95,color:"#000000"},canvasPadding:{top:5,right:5,bottom:5,left:5},pieCenterOffset:{x:0,y:0},cssPrefix:null},callbacks:{onload:null,onMouseoverSegment:null,onMouseoutSegment:null,onClickSegment:null}},f={initialCheck:function(b){var c=b.cssPrefix,d=b.element,e=b.options;if(!window.d3||!window.d3.hasOwnProperty("version"))return console.error("d3pie error: d3 is not available"),!1;if(!(d instanceof HTMLElement))return console.error("d3pie error: the first d3pie() param must be a valid DOM element (not jQuery) or a ID string."),!1;if(!/[a-zA-Z][a-zA-Z0-9_-]*$/.test(c))return console.error("d3pie error: invalid options.misc.cssPrefix"),!1;if(!a.isArray(e.data.content))return console.error("d3pie error: invalid config structure: missing data.content property."),!1;if(0===e.data.content.length)return console.error("d3pie error: no data supplied."),!1;for(var f=[],g=0;gd&&clearInterval(e),c++},1)},whenElementsExist:function(a,b){var c=1,d=1e3,e=setInterval(function(){for(var f=!0,g=0;gd&&clearInterval(e),c++},1)},shuffleArray:function(a){for(var b,c,d=a.length;0!==d;)c=Math.floor(Math.random()*d),d-=1,b=a[d],a[d]=a[c],a[c]=b;return a},processObj:function(a,b,c){return"string"==typeof b?g.processObj(a,b.split("."),c):1==b.length&&void 0!==c?a[b[0]]=c:0==b.length?a:g.processObj(a[b[0]],b.slice(1),c)},getDimensions:function(a){var b=document.getElementById(a),c=0,d=0;if(b){var e=b.getBBox();c=e.width,d=e.height}else console.log("error: getDimensions() "+a+" not found.");return{w:c,h:d}},rectIntersect:function(a,b){var c=b.x>a.x+a.w||b.x+b.wa.y+a.h;return!c},getColorShade:function(a,b){a=String(a).replace(/[^0-9a-f]/gi,""),a.length<6&&(a=a[0]+a[0]+a[1]+a[1]+a[2]+a[2]),b=b||0;for(var c="#",d=0;3>d;d++){var e=parseInt(a.substr(2*d,2),16);e=Math.round(Math.min(Math.max(0,e+e*b),255)).toString(16),c+=("00"+e).substr(e.length)}return c},initSegmentColors:function(a){for(var b=a.options.data,c=a.options.misc.colors.segments,d=[],e=0;ee?0:e;var f,g,h=(e>d?d:e)/3;if(null!==b.pieOuterRadius)if(/%/.test(b.pieOuterRadius)){g=parseInt(b.pieOuterRadius.replace(/[\D]/,""),10),g=g>99?99:g,g=0>g?0:g;var i=e>d?d:e;if("none"!==a.options.labels.outer.format){var j=2*parseInt(a.options.labels.outer.pieDistance,10);i-j>0&&(i-=j)}h=Math.floor(i/100*g)/2}else h=parseInt(b.pieOuterRadius,10);/%/.test(b.pieInnerRadius)?(g=parseInt(b.pieInnerRadius.replace(/[\D]/,""),10),g=g>99?99:g,g=0>g?0:g,f=Math.floor(h/100*g)):f=parseInt(b.pieInnerRadius,10),a.innerRadius=f,a.outerRadius=h},getTotalPieSize:function(a){for(var b=0,c=0;cb.label.toLowerCase()?1:-1});break;case"label-desc":b.sort(function(a,b){return a.label.toLowerCase()i?i+2*Math.PI:i,h>=d*d&&e*e>=h&&i>=f&&g>=i}},i={add:function(a,b,c){var d=i.getIncludes(c),e=a.options.labels,f=a.svg.insert("g","."+a.cssPrefix+"labels-"+b).attr("class",a.cssPrefix+"labels-"+b),g=f.selectAll("."+a.cssPrefix+"labelGroup-"+b).data(a.options.data).enter().append("g").attr("id",function(c,d){return a.cssPrefix+"labelGroup"+d+"-"+b}).attr("data-index",function(a,b){return b}).attr("class",a.cssPrefix+"labelGroup-"+b).style("opacity",0);d.mainLabel&&g.append("text").attr("id",function(c,d){return a.cssPrefix+"segmentMainLabel"+d+"-"+b}).attr("class",a.cssPrefix+"segmentMainLabel-"+b).text(function(a){var b=a.label;return e.truncation.enabled&&a.label.length>e.truncation.length&&(b=a.label.substring(0,e.truncation.length)+"..."),b}).style("font-size",e.mainLabel.fontSize+"px").style("font-family",e.mainLabel.font).style("fill",e.mainLabel.color),d.percentage&&g.append("text").attr("id",function(c,d){return a.cssPrefix+"segmentPercentage"+d+"-"+b}).attr("class",a.cssPrefix+"segmentPercentage-"+b).text(function(b){var c=b.value/a.totalSize*100;return c.toFixed(a.options.labels.percentage.decimalPlaces)+"%"}).style("font-size",e.percentage.fontSize+"px").style("font-family",e.percentage.font).style("fill",e.percentage.color),d.value&&g.append("text").attr("id",function(c,d){return a.cssPrefix+"segmentValue"+d+"-"+b}).attr("class",a.cssPrefix+"segmentValue-"+b).text(function(a){return a.value}).style("font-size",e.value.fontSize+"px").style("font-family",e.value.font).style("fill",e.value.color)},positionLabelElements:function(b,c,d){i["dimensions-"+c]=[];for(var e=a("."+b.cssPrefix+"labelGroup-"+c),f=0;f0?g[0].getBBox():null,percentage:h.length>0?h[0].getBBox():null,value:j.length>0?j[0].getBBox():null})}var k=5,l=i["dimensions-"+c];switch(d){case"label-value1":d3.selectAll("."+b.cssPrefix+"segmentValue-"+c).attr("dx",function(a,b){return l[b].mainLabel.width+k});break;case"label-value2":d3.selectAll("."+b.cssPrefix+"segmentValue-"+c).attr("dy",function(a,b){return l[b].mainLabel.height});break;case"label-percentage1":d3.selectAll("."+b.cssPrefix+"segmentPercentage-"+c).attr("dx",function(a,b){return l[b].mainLabel.width+k});break;case"label-percentage2":d3.selectAll("."+b.cssPrefix+"segmentPercentage-"+c).attr("dx",function(a,b){return l[b].mainLabel.width/2-l[b].percentage.width/2}).attr("dy",function(a,b){return l[b].mainLabel.height})}},computeLabelLinePositions:function(a){a.lineCoordGroups=[],d3.selectAll("."+a.cssPrefix+"labelGroup-outer").each(function(b,c){return i.computeLinePosition(a,c)})},computeLinePosition:function(a,b){var c,d,e,f,g=j.getSegmentAngle(b,a.options.data,a.totalSize,{midpoint:!0}),i=h.rotate(a.pieCenter.x,a.pieCenter.y-a.outerRadius,a.pieCenter.x,a.pieCenter.y,g),k=a.outerLabelGroupData[b].h/5,l=6,m=Math.floor(g/90),n=4;switch(2===m&&180===g&&(m=1),m){case 0:c=a.outerLabelGroupData[b].x-l-(a.outerLabelGroupData[b].x-l-i.x)/2,d=a.outerLabelGroupData[b].y+(i.y-a.outerLabelGroupData[b].y)/n,e=a.outerLabelGroupData[b].x-l,f=a.outerLabelGroupData[b].y-k;break;case 1:c=i.x+(a.outerLabelGroupData[b].x-i.x)/n,d=i.y+(a.outerLabelGroupData[b].y-i.y)/n,e=a.outerLabelGroupData[b].x-l,f=a.outerLabelGroupData[b].y-k;break;case 2:var o=a.outerLabelGroupData[b].x+a.outerLabelGroupData[b].w+l;c=i.x-(i.x-o)/n,d=i.y+(a.outerLabelGroupData[b].y-i.y)/n,e=a.outerLabelGroupData[b].x+a.outerLabelGroupData[b].w+l,f=a.outerLabelGroupData[b].y-k;break;case 3:var p=a.outerLabelGroupData[b].x+a.outerLabelGroupData[b].w+l;c=p+(i.x-p)/n,d=a.outerLabelGroupData[b].y+(i.y-a.outerLabelGroupData[b].y)/n,e=a.outerLabelGroupData[b].x+a.outerLabelGroupData[b].w+l,f=a.outerLabelGroupData[b].y-k}a.lineCoordGroups[b]="straight"===a.options.labels.lines.style?[{x:i.x,y:i.y},{x:e,y:f}]:[{x:i.x,y:i.y},{x:c,y:d},{x:e,y:f}]},addLabelLines:function(a){var b=a.svg.insert("g","."+a.cssPrefix+"pieChart").attr("class",a.cssPrefix+"lineGroups").style("opacity",0),c=b.selectAll("."+a.cssPrefix+"lineGroup").data(a.lineCoordGroups).enter().append("g").attr("class",a.cssPrefix+"lineGroup"),d=d3.svg.line().interpolate("basis").x(function(a){return a.x}).y(function(a){return a.y});c.append("path").attr("d",d).attr("stroke",function(b,c){return"segment"===a.options.labels.lines.color?a.options.colors[c]:a.options.labels.lines.color}).attr("stroke-width",1).attr("fill","none").style("opacity",function(b,c){var d=a.options.labels.outer.hideWhenLessThanPercentage,e=j.getPercentage(a,c),f=null!==d&&d>e||""===a.options.data[c].label;return f?0:1})},positionLabelGroups:function(b,c){d3.selectAll("."+b.cssPrefix+"labelGroup-"+c).style("opacity",0).attr("transform",function(d,e){var f,i;if("outer"===c)f=b.outerLabelGroupData[e].x,i=b.outerLabelGroupData[e].y;else{var k=a.extend(!0,{},b.pieCenter);if(b.innerRadius>0){var l=j.getSegmentAngle(e,b.options.data,b.totalSize,{midpoint:!0}),m=h.translate(b.pieCenter.x,b.pieCenter.y,b.innerRadius,l);k.x=m.x,k.y=m.y}var n=g.getDimensions(b.cssPrefix+"labelGroup"+e+"-inner"),o=n.w/2,p=n.h/4;f=k.x+(b.lineCoordGroups[e][0].x-k.x)/1.8,i=k.y+(b.lineCoordGroups[e][0].y-k.y)/1.8,f-=o,i+=p}return"translate("+f+","+i+")"})},fadeInLabelsAndLines:function(b){var c="default"===b.options.effects.load.effect?b.options.effects.load.speed:1;setTimeout(function(){var c="default"===b.options.effects.load.effect?400:1;d3.selectAll("."+b.cssPrefix+"labelGroup-outer").transition().duration(c).style("opacity",function(a,c){var d=b.options.labels.outer.hideWhenLessThanPercentage,e=j.getPercentage(b,c);return null!==d&&d>e?0:1}),d3.selectAll("."+b.cssPrefix+"labelGroup-inner").transition().duration(c).style("opacity",function(a,c){var d=b.options.labels.inner.hideWhenLessThanPercentage,e=j.getPercentage(b,c);return null!==d&&d>e?0:1}),d3.selectAll("g."+b.cssPrefix+"lineGroups").transition().duration(c).style("opacity",1),a.isFunction(b.options.callbacks.onload)&&setTimeout(function(){try{b.options.callbacks.onload()}catch(a){}},c)},c)},getIncludes:function(a){var b=!1,c=!1,d=!1;switch(a){case"label":b=!0;break;case"value":c=!0;break;case"percentage":d=!0;break;case"label-value1":case"label-value2":b=!0,c=!0;break;case"label-percentage1":case"label-percentage2":b=!0,d=!0}return{mainLabel:b,value:c,percentage:d}},computeOuterLabelCoords:function(a){a.svg.selectAll("."+a.cssPrefix+"labelGroup-outer").each(function(b,c){return i.getIdealOuterLabelPositions(a,c)}),i.resolveOuterLabelCollisions(a)},resolveOuterLabelCollisions:function(a){var b=a.options.data.length;i.checkConflict(a,0,"clockwise",b),i.checkConflict(a,b-1,"anticlockwise",b)},checkConflict:function(a,b,c,d){if(!(1>=d)){var e=a.outerLabelGroupData[b].hs;if(!("clockwise"===c&&"right"!=e||"anticlockwise"===c&&"left"!=e)){var f="clockwise"===c?b+1:b-1,h=a.outerLabelGroupData[b],j=a.outerLabelGroupData[f],k={labelHeights:a.outerLabelGroupData[0].h,center:a.pieCenter,lineLength:a.outerRadius+a.options.labels.outer.pieDistance,heightChange:a.outerLabelGroupData[0].h+1};if("clockwise"===c)for(var l=0;b>=l;l++){var m=a.outerLabelGroupData[l];if(g.rectIntersect(m,j)){i.adjustLabelPos(a,f,h,k);break}}else for(var l=d-1;l>=b;l--){var m=a.outerLabelGroupData[l];if(g.rectIntersect(m,j)){i.adjustLabelPos(a,f,h,k);break}}i.checkConflict(a,f,c,d)}}},adjustLabelPos:function(a,b,c,d){var e,f,g,h;h=c.y+d.heightChange,f=d.center.y-h,e=Math.sqrt(Math.abs(d.lineLength)>Math.abs(f)?d.lineLength*d.lineLength-f*f:f*f-d.lineLength*d.lineLength),g="right"===c.hs?d.center.x+e:d.center.x-e-a.outerLabelGroupData[b].w,g||console.log(c.hs,e),a.outerLabelGroupData[b].x=g,a.outerLabelGroupData[b].y=h},getIdealOuterLabelPositions:function(b,c){var d=a("#"+b.cssPrefix+"labelGroup"+c+"-outer")[0].getBBox(),e=j.getSegmentAngle(c,b.options.data,b.totalSize,{midpoint:!0}),f=b.pieCenter.x,g=b.pieCenter.y-(b.outerRadius+b.options.labels.outer.pieDistance),i=h.rotate(f,g,b.pieCenter.x,b.pieCenter.y,e),k="right";e>180?(i.x-=d.width+8,k="left"):i.x+=8,b.outerLabelGroupData[c]={x:i.x,y:i.y,w:d.width,h:d.height,hs:k}}},j={create:function(a){var b=a.pieCenter,c=a.options.colors,d=a.options.effects.load,e=a.options.misc.colors.segmentStroke,f=a.svg.insert("g","#"+a.cssPrefix+"title").attr("transform",function(){return h.getPieTranslateCenter(b)}).attr("class",a.cssPrefix+"pieChart"),g=d3.svg.arc().innerRadius(a.innerRadius).outerRadius(a.outerRadius).startAngle(0).endAngle(function(b){return b.value/a.totalSize*2*Math.PI}),i=f.selectAll("."+a.cssPrefix+"arc").data(a.options.data).enter().append("g").attr("class",a.cssPrefix+"arc"),k=d.speed;"none"===d.effect&&(k=0),i.append("path").attr("id",function(b,c){return a.cssPrefix+"segment"+c}).attr("fill",function(b,d){var e=c[d];return a.options.misc.gradient.enabled&&(e="url(#"+a.cssPrefix+"grad"+d+")"),e}).style("stroke",e).style("stroke-width",1).transition().ease("cubic-in-out").duration(k).attr("data-index",function(a,b){return b}).attrTween("d",function(b){var c=d3.interpolate({value:0},b);return function(b){return a.arc(c(b))}}),a.svg.selectAll("g."+a.cssPrefix+"arc").attr("transform",function(b,c){var d=0;return c>0&&(d=j.getSegmentAngle(c-1,a.options.data,a.totalSize)),"rotate("+d+")"}),a.arc=g},addGradients:function(a){var b=a.svg.append("defs").selectAll("radialGradient").data(a.options.data).enter().append("radialGradient").attr("gradientUnits","userSpaceOnUse").attr("cx",0).attr("cy",0).attr("r","120%").attr("id",function(b,c){return a.cssPrefix+"grad"+c});b.append("stop").attr("offset","0%").style("stop-color",function(b,c){return a.options.colors[c]}),b.append("stop").attr("offset",a.options.misc.gradient.percentage+"%").style("stop-color",a.options.misc.gradient.color)},addSegmentEventHandlers:function(b){var c=a("."+b.cssPrefix+"arc,."+b.cssPrefix+"labelGroup-inner,."+b.cssPrefix+"labelGroup-outer");c.on("click",function(c){var d;if(d3.select(c.currentTarget).attr("class")===b.cssPrefix+"arc")d=a(c.currentTarget).find("path");else{var e=a(c.currentTarget).data("index");d=a("#"+b.cssPrefix+"segment"+e)}var f=d.attr("class")===b.cssPrefix+"expanded";j.onSegmentEvent(b,b.options.callbacks.onClickSegment,d,f),"none"!==b.options.effects.pullOutSegmentOnClick.effect&&(f?j.closeSegment(b,d[0]):j.openSegment(b,d[0]))}),c.on("mouseover",function(c){var d;if(d3.select(c.currentTarget).attr("class")===b.cssPrefix+"arc")d=a(c.currentTarget).find("path");else{var e=a(c.currentTarget).data("index");d=a("#"+b.cssPrefix+"segment"+e)}if(b.options.effects.highlightSegmentOnMouseover){var e=d.data("index"),f=b.options.colors[e];d3.select(d[0]).style("fill",g.getColorShade(f,b.options.effects.highlightLuminosity))}var h=d.attr("class")===b.cssPrefix+"expanded";j.onSegmentEvent(b,b.options.callbacks.onMouseoverSegment,d,h)}),c.on("mouseout",function(c){var d;if(d3.select(c.currentTarget).attr("class")===b.cssPrefix+"arc")d=a(c.currentTarget).find("path");else{var e=a(c.currentTarget).data("index");d=a("#"+b.cssPrefix+"segment"+e)}if(b.options.effects.highlightSegmentOnMouseover){var e=d.data("index"),f=b.options.colors[e];b.options.misc.gradient.enabled&&(f="url(#"+b.cssPrefix+"grad"+e+")"),d3.select(d[0]).style("fill",f)}var g=d.attr("class")===b.cssPrefix+"expanded";j.onSegmentEvent(b,b.options.callbacks.onMouseoutSegment,d,g)})},onSegmentEvent:function(b,c,d,e){if(a.isFunction(c))try{var f=parseInt(d.data("index"),10);c({segment:d[0],index:f,expanded:e,data:b.options.data[f]})}catch(g){}},openSegment:function(b,c){b.isOpeningSegment||(b.isOpeningSegment=!0,a("."+b.cssPrefix+"expanded").length>0&&j.closeSegment(b,a("."+b.cssPrefix+"expanded")[0]),d3.select(c).transition().ease(b.options.effects.pullOutSegmentOnClick.effect).duration(b.options.effects.pullOutSegmentOnClick.speed).attr("transform",function(a){var c=b.arc.centroid(a),d=c[0],e=c[1],f=Math.sqrt(d*d+e*e),g=parseInt(b.options.effects.pullOutSegmentOnClick.size,10);return"translate("+d/f*g+","+e/f*g+")"}).each("end",function(){b.currentlyOpenSegment=c,b.isOpeningSegment=!1,a(this).attr("class",b.cssPrefix+"expanded")}))},closeSegment:function(b,c){d3.select(c).transition().duration(400).attr("transform","translate(0,0)").each("end",function(){a(this).attr("class",""),b.currentlyOpenSegment=null})},getCentroid:function(a){var b=a.getBBox();return{x:b.x+b.width/2,y:b.y+b.height/2}},getSegmentAngle:function(b,c,d,e){var f,g=a.extend({compounded:!0,midpoint:!1},e),h=c[b].value;if(g.compounded){f=0;for(var i=0;b>=i;i++)f+=c[i].value}"undefined"==typeof f&&(f=h);var j=f/d*360;if(g.midpoint){var k=h/d*360;j-=k/2}return j},getPercentage:function(a,b){return Math.floor(a.options.data[b].value/a.totalSize*100)}},k={offscreenCoord:-1e4,addTitle:function(a){a.svg.selectAll("."+a.cssPrefix+"title").data([a.options.header.title]).enter().append("text").text(function(a){return a.text}).attr("id",a.cssPrefix+"title").attr("class",a.cssPrefix+"title").attr("x",k.offscreenCoord).attr("y",k.offscreenCoord).attr("text-anchor",function(){var b;return b="top-center"===a.options.header.location||"pie-center"===a.options.header.location?"middle":"left"}).attr("fill",function(a){return a.color}).style("font-size",function(a){return a.fontSize+"px"}).style("font-family",function(a){return a.font})},positionTitle:function(a){var b=a.textComponents,c=a.options.header.location,d=a.options.misc.canvasPadding,e=a.options.size.canvasWidth,f=a.options.header.titleSubtitlePadding,g="top-left"===c?d.left:e/2,h=d.top+b.title.h;if("pie-center"===c)if(h=a.pieCenter.y,b.subtitle.exists){var i=b.title.h+f+b.subtitle.h;h=h-i/2+b.title.h}else h+=b.title.h/4;a.svg.select("#"+a.cssPrefix+"title").attr("x",g).attr("y",h)},addSubtitle:function(a){var b=a.options.header.location;a.svg.selectAll("."+a.cssPrefix+"subtitle").data([a.options.header.subtitle]).enter().append("text").text(function(a){return a.text}).attr("x",k.offscreenCoord).attr("y",k.offscreenCoord).attr("id",a.cssPrefix+"subtitle").attr("class",a.cssPrefix+"subtitle").attr("text-anchor",function(){var a;return a="top-center"===b||"pie-center"===b?"middle":"left"}).attr("fill",function(a){return a.color}).style("font-size",function(a){return a.fontSize+"px"}).style("font-family",function(a){return a.font})},positionSubtitle:function(a){var b="top-left"===a.options.header.location?a.options.misc.canvasPadding.left:a.options.size.canvasWidth/2,c=k.getHeaderHeight(a);a.svg.select("#"+a.cssPrefix+"subtitle").attr("x",b).attr("y",c)},addFooter:function(a){a.svg.selectAll("."+a.cssPrefix+"footer").data([a.options.footer]).enter().append("text").text(function(a){return a.text}).attr("x",k.offscreenCoord).attr("y",k.offscreenCoord).attr("id",a.cssPrefix+"footer").attr("class",a.cssPrefix+"footer").attr("text-anchor",function(){var b="left";return"bottom-center"===a.options.footer.location?b="middle":"bottom-right"===a.options.footer.location&&(b="left"),b}).attr("fill",function(a){return a.color}).style("font-size",function(a){return a.fontSize+"px"}).style("font-family",function(a){return a.font})},positionFooter:function(a){var b,c=a.options.footer.location,d=a.textComponents.footer.w,e=a.options.size.canvasWidth,f=a.options.size.canvasHeight,g=a.options.misc.canvasPadding;b="bottom-left"===c?g.left:"bottom-right"===c?e-d-g.right:e/2,a.svg.select("#"+a.cssPrefix+"footer").attr("x",b).attr("y",f-g.bottom)},getHeaderHeight:function(a){var b;if(a.textComponents.title.exists){var c=a.textComponents.title.h+a.options.header.titleSubtitlePadding+a.textComponents.subtitle.h;b="pie-center"===a.options.header.location?a.pieCenter.y-c/2+c:c+a.options.misc.canvasPadding.top}else if("pie-center"===a.options.header.location){var d=a.options.misc.canvasPadding.bottom+a.textComponents.footer.h;b=(a.options.size.canvasHeight-d)/2+a.options.misc.canvasPadding.top+a.textComponents.subtitle.h/2}else b=a.options.misc.canvasPadding.top+a.textComponents.subtitle.h;return b}},l=function(i,j){this.element=i,"string"==typeof i&&(this.element=/#/.test(i)?a(i)[0]:a("#"+i)[0]),this.options=a.extend(!0,{},e,j),null!==this.options.misc.cssPrefix?this.cssPrefix=this.options.misc.cssPrefix:(this.cssPrefix="p"+d+"_",d++),f.initialCheck(this)&&(a(this.element).data(b,c),this.options.data=h.sortPieData(this),this.options.colors=g.initSegmentColors(this),this.totalSize=h.getTotalPieSize(this.options.data),m.call(this))};l.prototype.redraw=function(){this.element.innerHTML="",m.call(this)},l.prototype.destroy=function(){this.element.innerHTML="",a(this.element).removeData(b)},l.prototype.getOpenSegment=function(){var b=this.currentlyOpenSegment;if(null!==b&&"undefined"!=typeof b){var c=parseInt(a(b).data("index"),10);return{element:b,index:c,data:this.options.data[c]}}return null},l.prototype.openSegment=function(b){var b=parseInt(b,10);0>b||b>this.options.data.length-1||j.openSegment(this,a("#"+this.cssPrefix+"segment"+b)[0])},l.prototype.closeSegment=function(){var a=this.currentlyOpenSegment;a&&j.closeSegment(this,a)},l.prototype.updateProp=function(b,c){switch(b){case"header.title.text":var d=g.processObj(this.options,b);g.processObj(this.options,b,c),a("#"+this.cssPrefix+"title").html(c),(""===d&&""!==c||""!==d&&""===c)&&this.redraw();break;case"header.subtitle.text":var e=g.processObj(this.options,b);g.processObj(this.options,b,c),a("#"+this.cssPrefix+"subtitle").html(c),(""===e&&""!==c||""!==e&&""===c)&&this.redraw();break;case"callbacks.onload":case"callbacks.onMouseoverSegment":case"callbacks.onMouseoutSegment":case"callbacks.onClickSegment":case"effects.pullOutSegmentOnClick.effect":case"effects.pullOutSegmentOnClick.speed":case"effects.pullOutSegmentOnClick.size":case"effects.highlightSegmentOnMouseover":case"effects.highlightLuminosity":g.processObj(this.options,b,c)}};var m=function(){this.svg=g.addSVGSpace(this),this.textComponents={headerHeight:0,title:{exists:""!==this.options.header.title.text,h:0,w:0},subtitle:{exists:""!==this.options.header.subtitle.text,h:0,w:0},footer:{exists:""!==this.options.footer.text,h:0,w:0}},this.outerLabelGroupData=[],this.textComponents.title.exists&&k.addTitle(this),this.textComponents.subtitle.exists&&k.addSubtitle(this),k.addFooter(this);var a=this;g.whenIdExists(this.cssPrefix+"footer",function(){k.positionFooter(a);var b=g.getDimensions(a.cssPrefix+"footer");a.textComponents.footer.h=b.h,a.textComponents.footer.w=b.w});var b=[];this.textComponents.title.exists&&b.push(this.cssPrefix+"title"),this.textComponents.subtitle.exists&&b.push(this.cssPrefix+"subtitle"),this.textComponents.footer.exists&&b.push(this.cssPrefix+"footer"),g.whenElementsExist(b,function(){if(a.textComponents.title.exists){var b=g.getDimensions(a.cssPrefix+"title");a.textComponents.title.h=b.h,a.textComponents.title.w=b.w}if(a.textComponents.subtitle.exists){var c=g.getDimensions(a.cssPrefix+"subtitle");a.textComponents.subtitle.h=c.h,a.textComponents.subtitle.w=c.w}if(a.textComponents.title.exists||a.textComponents.subtitle.exists){var d=0;a.textComponents.title.exists&&(d+=a.textComponents.title.h,a.textComponents.subtitle.exists&&(d+=a.options.header.titleSubtitlePadding)),a.textComponents.subtitle.exists&&(d+=a.textComponents.subtitle.h),a.textComponents.headerHeight=d}h.computePieRadius(a),h.calculatePieCenter(a),k.positionTitle(a),k.positionSubtitle(a),a.options.misc.gradient.enabled&&j.addGradients(a),j.create(a),i.add(a,"inner",a.options.labels.inner.format),i.add(a,"outer",a.options.labels.outer.format),i.positionLabelElements(a,"inner",a.options.labels.inner.format),i.positionLabelElements(a,"outer",a.options.labels.outer.format),i.computeOuterLabelCoords(a),i.positionLabelGroups(a,"outer"),i.computeLabelLinePositions(a),a.options.labels.lines.enabled&&"none"!==a.options.labels.outer.format&&i.addLabelLines(a),i.positionLabelGroups(a,"inner"),i.fadeInLabelsAndLines(a),j.addSegmentEventHandlers(a)})};window.d3pie=l}(jQuery); \ No newline at end of file +!function(a){"use strict";var b="d3pie",c="0.1.2",d=0,e={header:{title:{text:"",color:"#333333",fontSize:18,font:"arial"},subtitle:{text:"",color:"#666666",fontSize:14,font:"arial"},location:"top-center",titleSubtitlePadding:8},footer:{text:"",color:"#666666",fontSize:14,font:"arial",location:"left"},size:{canvasHeight:500,canvasWidth:500,pieInnerRadius:"0%",pieOuterRadius:null},data:{sortOrder:"none",content:[]},labels:{outer:{format:"label",hideWhenLessThanPercentage:null,pieDistance:30},inner:{format:"percentage",hideWhenLessThanPercentage:null},mainLabel:{color:"#333333",font:"arial",fontSize:10},percentage:{color:"#dddddd",font:"arial",fontSize:10,decimalPlaces:0},value:{color:"#cccc44",font:"arial",fontSize:10},lines:{enabled:!0,style:"curved",color:"segment"},truncation:{enabled:!1,length:30}},effects:{load:{effect:"default",speed:1e3},pullOutSegmentOnClick:{effect:"bounce",speed:300,size:10},highlightSegmentOnMouseover:!0,highlightLuminosity:-.2},misc:{colors:{background:null,segments:["#2484c1","#65a620","#7b6888","#a05d56","#961a1a","#d8d23a","#e98125","#d0743c","#635222","#6ada6a","#0c6197","#7d9058","#207f33","#44b9b0","#bca44a","#e4a14b","#a3acb2","#8cc3e9","#69a6f9","#5b388f","#546e91","#8bde95","#d2ab58","#273c71","#98bf6e","#4daa4b","#98abc5","#cc1010","#31383b","#006391","#c2643f","#b0a474","#a5a39c","#a9c2bc","#22af8c","#7fcecf","#987ac6","#3d3b87","#b77b1c","#c9c2b6","#807ece","#8db27c","#be66a2","#9ed3c6","#00644b","#005064","#77979f","#77e079","#9c73ab","#1f79a7"],segmentStroke:"#ffffff"},gradient:{enabled:!1,percentage:95,color:"#000000"},canvasPadding:{top:5,right:5,bottom:5,left:5},pieCenterOffset:{x:0,y:0},cssPrefix:null},callbacks:{onload:null,onMouseoverSegment:null,onMouseoutSegment:null,onClickSegment:null}},f={initialCheck:function(b){var c=b.cssPrefix,d=b.element,e=b.options;if(!window.d3||!window.d3.hasOwnProperty("version"))return console.error("d3pie error: d3 is not available"),!1;if(!(d instanceof HTMLElement))return console.error("d3pie error: the first d3pie() param must be a valid DOM element (not jQuery) or a ID string."),!1;if(!/[a-zA-Z][a-zA-Z0-9_-]*$/.test(c))return console.error("d3pie error: invalid options.misc.cssPrefix"),!1;if(!a.isArray(e.data.content))return console.error("d3pie error: invalid config structure: missing data.content property."),!1;if(0===e.data.content.length)return console.error("d3pie error: no data supplied."),!1;for(var f=[],g=0;gd&&clearInterval(e),c++},1)},whenElementsExist:function(a,b){var c=1,d=1e3,e=setInterval(function(){for(var f=!0,g=0;gd&&clearInterval(e),c++},1)},shuffleArray:function(a){for(var b,c,d=a.length;0!==d;)c=Math.floor(Math.random()*d),d-=1,b=a[d],a[d]=a[c],a[c]=b;return a},processObj:function(a,b,c){return"string"==typeof b?g.processObj(a,b.split("."),c):1==b.length&&void 0!==c?a[b[0]]=c:0==b.length?a:g.processObj(a[b[0]],b.slice(1),c)},getDimensions:function(a){var b=document.getElementById(a),c=0,d=0;if(b){var e=b.getBBox();c=e.width,d=e.height}else console.log("error: getDimensions() "+a+" not found.");return{w:c,h:d}},rectIntersect:function(a,b){var c=b.x>a.x+a.w||b.x+b.wa.y+a.h;return!c},getColorShade:function(a,b){a=String(a).replace(/[^0-9a-f]/gi,""),a.length<6&&(a=a[0]+a[0]+a[1]+a[1]+a[2]+a[2]),b=b||0;for(var c="#",d=0;3>d;d++){var e=parseInt(a.substr(2*d,2),16);e=Math.round(Math.min(Math.max(0,e+e*b),255)).toString(16),c+=("00"+e).substr(e.length)}return c},initSegmentColors:function(a){for(var b=a.options.data,c=a.options.misc.colors.segments,d=[],e=0;ee?0:e;var f,g,h=(e>d?d:e)/3;if(null!==b.pieOuterRadius)if(/%/.test(b.pieOuterRadius)){g=parseInt(b.pieOuterRadius.replace(/[\D]/,""),10),g=g>99?99:g,g=0>g?0:g;var i=e>d?d:e;if("none"!==a.options.labels.outer.format){var j=2*parseInt(a.options.labels.outer.pieDistance,10);i-j>0&&(i-=j)}h=Math.floor(i/100*g)/2}else h=parseInt(b.pieOuterRadius,10);/%/.test(b.pieInnerRadius)?(g=parseInt(b.pieInnerRadius.replace(/[\D]/,""),10),g=g>99?99:g,g=0>g?0:g,f=Math.floor(h/100*g)):f=parseInt(b.pieInnerRadius,10),a.innerRadius=f,a.outerRadius=h},getTotalPieSize:function(a){for(var b=0,c=0;cb.label.toLowerCase()?1:-1});break;case"label-desc":b.sort(function(a,b){return a.label.toLowerCase()i?i+2*Math.PI:i,h>=d*d&&e*e>=h&&i>=f&&g>=i}},i={add:function(a,b,c){var d=i.getIncludes(c),e=a.options.labels,f=a.svg.insert("g","."+a.cssPrefix+"labels-"+b).attr("class",a.cssPrefix+"labels-"+b),g=f.selectAll("."+a.cssPrefix+"labelGroup-"+b).data(a.options.data).enter().append("g").attr("id",function(c,d){return a.cssPrefix+"labelGroup"+d+"-"+b}).attr("data-index",function(a,b){return b}).attr("class",a.cssPrefix+"labelGroup-"+b).style("opacity",0);d.mainLabel&&g.append("text").attr("id",function(c,d){return a.cssPrefix+"segmentMainLabel"+d+"-"+b}).attr("class",a.cssPrefix+"segmentMainLabel-"+b).text(function(a){var b=a.label;return e.truncation.enabled&&a.label.length>e.truncation.length&&(b=a.label.substring(0,e.truncation.length)+"..."),b}).style("font-size",e.mainLabel.fontSize+"px").style("font-family",e.mainLabel.font).style("fill",e.mainLabel.color),d.percentage&&g.append("text").attr("id",function(c,d){return a.cssPrefix+"segmentPercentage"+d+"-"+b}).attr("class",a.cssPrefix+"segmentPercentage-"+b).text(function(b){var c=b.value/a.totalSize*100;return c.toFixed(a.options.labels.percentage.decimalPlaces)+"%"}).style("font-size",e.percentage.fontSize+"px").style("font-family",e.percentage.font).style("fill",e.percentage.color),d.value&&g.append("text").attr("id",function(c,d){return a.cssPrefix+"segmentValue"+d+"-"+b}).attr("class",a.cssPrefix+"segmentValue-"+b).text(function(a){return a.value}).style("font-size",e.value.fontSize+"px").style("font-family",e.value.font).style("fill",e.value.color)},positionLabelElements:function(b,c,d){i["dimensions-"+c]=[];for(var e=a("."+b.cssPrefix+"labelGroup-"+c),f=0;f0?g[0].getBBox():null,percentage:h.length>0?h[0].getBBox():null,value:j.length>0?j[0].getBBox():null})}var k=5,l=i["dimensions-"+c];switch(d){case"label-value1":d3.selectAll("."+b.cssPrefix+"segmentValue-"+c).attr("dx",function(a,b){return l[b].mainLabel.width+k});break;case"label-value2":d3.selectAll("."+b.cssPrefix+"segmentValue-"+c).attr("dy",function(a,b){return l[b].mainLabel.height});break;case"label-percentage1":d3.selectAll("."+b.cssPrefix+"segmentPercentage-"+c).attr("dx",function(a,b){return l[b].mainLabel.width+k});break;case"label-percentage2":d3.selectAll("."+b.cssPrefix+"segmentPercentage-"+c).attr("dx",function(a,b){return l[b].mainLabel.width/2-l[b].percentage.width/2}).attr("dy",function(a,b){return l[b].mainLabel.height})}},computeLabelLinePositions:function(a){a.lineCoordGroups=[],d3.selectAll("."+a.cssPrefix+"labelGroup-outer").each(function(b,c){return i.computeLinePosition(a,c)})},computeLinePosition:function(a,b){var c,d,e,f,g=j.getSegmentAngle(b,a.options.data,a.totalSize,{midpoint:!0}),i=h.rotate(a.pieCenter.x,a.pieCenter.y-a.outerRadius,a.pieCenter.x,a.pieCenter.y,g),k=a.outerLabelGroupData[b].h/5,l=6,m=Math.floor(g/90),n=4;switch(2===m&&180===g&&(m=1),m){case 0:c=a.outerLabelGroupData[b].x-l-(a.outerLabelGroupData[b].x-l-i.x)/2,d=a.outerLabelGroupData[b].y+(i.y-a.outerLabelGroupData[b].y)/n,e=a.outerLabelGroupData[b].x-l,f=a.outerLabelGroupData[b].y-k;break;case 1:c=i.x+(a.outerLabelGroupData[b].x-i.x)/n,d=i.y+(a.outerLabelGroupData[b].y-i.y)/n,e=a.outerLabelGroupData[b].x-l,f=a.outerLabelGroupData[b].y-k;break;case 2:var o=a.outerLabelGroupData[b].x+a.outerLabelGroupData[b].w+l;c=i.x-(i.x-o)/n,d=i.y+(a.outerLabelGroupData[b].y-i.y)/n,e=a.outerLabelGroupData[b].x+a.outerLabelGroupData[b].w+l,f=a.outerLabelGroupData[b].y-k;break;case 3:var p=a.outerLabelGroupData[b].x+a.outerLabelGroupData[b].w+l;c=p+(i.x-p)/n,d=a.outerLabelGroupData[b].y+(i.y-a.outerLabelGroupData[b].y)/n,e=a.outerLabelGroupData[b].x+a.outerLabelGroupData[b].w+l,f=a.outerLabelGroupData[b].y-k}a.lineCoordGroups[b]="straight"===a.options.labels.lines.style?[{x:i.x,y:i.y},{x:e,y:f}]:[{x:i.x,y:i.y},{x:c,y:d},{x:e,y:f}]},addLabelLines:function(a){var b=a.svg.insert("g","."+a.cssPrefix+"pieChart").attr("class",a.cssPrefix+"lineGroups").style("opacity",0),c=b.selectAll("."+a.cssPrefix+"lineGroup").data(a.lineCoordGroups).enter().append("g").attr("class",a.cssPrefix+"lineGroup"),d=d3.svg.line().interpolate("basis").x(function(a){return a.x}).y(function(a){return a.y});c.append("path").attr("d",d).attr("stroke",function(b,c){return"segment"===a.options.labels.lines.color?a.options.colors[c]:a.options.labels.lines.color}).attr("stroke-width",1).attr("fill","none").style("opacity",function(b,c){var d=a.options.labels.outer.hideWhenLessThanPercentage,e=j.getPercentage(a,c),f=null!==d&&d>e||""===a.options.data[c].label;return f?0:1})},positionLabelGroups:function(b,c){d3.selectAll("."+b.cssPrefix+"labelGroup-"+c).style("opacity",0).attr("transform",function(d,e){var f,i;if("outer"===c)f=b.outerLabelGroupData[e].x,i=b.outerLabelGroupData[e].y;else{var k=a.extend(!0,{},b.pieCenter);if(b.innerRadius>0){var l=j.getSegmentAngle(e,b.options.data,b.totalSize,{midpoint:!0}),m=h.translate(b.pieCenter.x,b.pieCenter.y,b.innerRadius,l);k.x=m.x,k.y=m.y}var n=g.getDimensions(b.cssPrefix+"labelGroup"+e+"-inner"),o=n.w/2,p=n.h/4;f=k.x+(b.lineCoordGroups[e][0].x-k.x)/1.8,i=k.y+(b.lineCoordGroups[e][0].y-k.y)/1.8,f-=o,i+=p}return"translate("+f+","+i+")"})},fadeInLabelsAndLines:function(b){var c="default"===b.options.effects.load.effect?b.options.effects.load.speed:1;setTimeout(function(){var c="default"===b.options.effects.load.effect?400:1;d3.selectAll("."+b.cssPrefix+"labelGroup-outer").transition().duration(c).style("opacity",function(a,c){var d=b.options.labels.outer.hideWhenLessThanPercentage,e=j.getPercentage(b,c);return null!==d&&d>e?0:1}),d3.selectAll("."+b.cssPrefix+"labelGroup-inner").transition().duration(c).style("opacity",function(a,c){var d=b.options.labels.inner.hideWhenLessThanPercentage,e=j.getPercentage(b,c);return null!==d&&d>e?0:1}),d3.selectAll("g."+b.cssPrefix+"lineGroups").transition().duration(c).style("opacity",1),a.isFunction(b.options.callbacks.onload)&&setTimeout(function(){try{b.options.callbacks.onload()}catch(a){}},c)},c)},getIncludes:function(a){var b=!1,c=!1,d=!1;switch(a){case"label":b=!0;break;case"value":c=!0;break;case"percentage":d=!0;break;case"label-value1":case"label-value2":b=!0,c=!0;break;case"label-percentage1":case"label-percentage2":b=!0,d=!0}return{mainLabel:b,value:c,percentage:d}},computeOuterLabelCoords:function(a){a.svg.selectAll("."+a.cssPrefix+"labelGroup-outer").each(function(b,c){return i.getIdealOuterLabelPositions(a,c)}),i.resolveOuterLabelCollisions(a)},resolveOuterLabelCollisions:function(a){var b=a.options.data.length;i.checkConflict(a,0,"clockwise",b),i.checkConflict(a,b-1,"anticlockwise",b)},checkConflict:function(a,b,c,d){if(!(1>=d)){var e=a.outerLabelGroupData[b].hs;if(!("clockwise"===c&&"right"!=e||"anticlockwise"===c&&"left"!=e)){var f="clockwise"===c?b+1:b-1,h=a.outerLabelGroupData[b],j=a.outerLabelGroupData[f],k={labelHeights:a.outerLabelGroupData[0].h,center:a.pieCenter,lineLength:a.outerRadius+a.options.labels.outer.pieDistance,heightChange:a.outerLabelGroupData[0].h+1};if("clockwise"===c)for(var l=0;b>=l;l++){var m=a.outerLabelGroupData[l];if(g.rectIntersect(m,j)){i.adjustLabelPos(a,f,h,k);break}}else for(var l=d-1;l>=b;l--){var m=a.outerLabelGroupData[l];if(g.rectIntersect(m,j)){i.adjustLabelPos(a,f,h,k);break}}i.checkConflict(a,f,c,d)}}},adjustLabelPos:function(a,b,c,d){var e,f,g,h;h=c.y+d.heightChange,f=d.center.y-h,e=Math.sqrt(Math.abs(d.lineLength)>Math.abs(f)?d.lineLength*d.lineLength-f*f:f*f-d.lineLength*d.lineLength),g="right"===c.hs?d.center.x+e:d.center.x-e-a.outerLabelGroupData[b].w,g||console.log(c.hs,e),a.outerLabelGroupData[b].x=g,a.outerLabelGroupData[b].y=h},getIdealOuterLabelPositions:function(b,c){var d=a("#"+b.cssPrefix+"labelGroup"+c+"-outer")[0].getBBox(),e=j.getSegmentAngle(c,b.options.data,b.totalSize,{midpoint:!0}),f=b.pieCenter.x,g=b.pieCenter.y-(b.outerRadius+b.options.labels.outer.pieDistance),i=h.rotate(f,g,b.pieCenter.x,b.pieCenter.y,e),k="right";e>180?(i.x-=d.width+8,k="left"):i.x+=8,b.outerLabelGroupData[c]={x:i.x,y:i.y,w:d.width,h:d.height,hs:k}}},j={create:function(a){var b=a.pieCenter,c=a.options.colors,d=a.options.effects.load,e=a.options.misc.colors.segmentStroke,f=a.svg.insert("g","#"+a.cssPrefix+"title").attr("transform",function(){return h.getPieTranslateCenter(b)}).attr("class",a.cssPrefix+"pieChart"),g=d3.svg.arc().innerRadius(a.innerRadius).outerRadius(a.outerRadius).startAngle(0).endAngle(function(b){return b.value/a.totalSize*2*Math.PI}),i=f.selectAll("."+a.cssPrefix+"arc").data(a.options.data).enter().append("g").attr("class",a.cssPrefix+"arc"),k=d.speed;"none"===d.effect&&(k=0),i.append("path").attr("id",function(b,c){return a.cssPrefix+"segment"+c}).attr("fill",function(b,d){var e=c[d];return a.options.misc.gradient.enabled&&(e="url(#"+a.cssPrefix+"grad"+d+")"),e}).style("stroke",e).style("stroke-width",1).transition().ease("cubic-in-out").duration(k).attr("data-index",function(a,b){return b}).attrTween("d",function(b){var c=d3.interpolate({value:0},b);return function(b){return a.arc(c(b))}}),a.svg.selectAll("g."+a.cssPrefix+"arc").attr("transform",function(b,c){var d=0;return c>0&&(d=j.getSegmentAngle(c-1,a.options.data,a.totalSize)),"rotate("+d+")"}),a.arc=g},addGradients:function(a){var b=a.svg.append("defs").selectAll("radialGradient").data(a.options.data).enter().append("radialGradient").attr("gradientUnits","userSpaceOnUse").attr("cx",0).attr("cy",0).attr("r","120%").attr("id",function(b,c){return a.cssPrefix+"grad"+c});b.append("stop").attr("offset","0%").style("stop-color",function(b,c){return a.options.colors[c]}),b.append("stop").attr("offset",a.options.misc.gradient.percentage+"%").style("stop-color",a.options.misc.gradient.color)},addSegmentEventHandlers:function(b){var c=a("."+b.cssPrefix+"arc,."+b.cssPrefix+"labelGroup-inner,."+b.cssPrefix+"labelGroup-outer");c.on("click",function(c){var d;if(d3.select(c.currentTarget).attr("class")===b.cssPrefix+"arc")d=a(c.currentTarget).find("path");else{var e=a(c.currentTarget).data("index");d=a("#"+b.cssPrefix+"segment"+e)}var f=d.attr("class")===b.cssPrefix+"expanded";j.onSegmentEvent(b,b.options.callbacks.onClickSegment,d,f),"none"!==b.options.effects.pullOutSegmentOnClick.effect&&(f?j.closeSegment(b,d[0]):j.openSegment(b,d[0]))}),c.on("mouseover",function(c){var d;if(d3.select(c.currentTarget).attr("class")===b.cssPrefix+"arc")d=a(c.currentTarget).find("path");else{var e=a(c.currentTarget).data("index");d=a("#"+b.cssPrefix+"segment"+e)}if(b.options.effects.highlightSegmentOnMouseover){var e=d.data("index"),f=b.options.colors[e];d3.select(d[0]).style("fill",g.getColorShade(f,b.options.effects.highlightLuminosity))}var h=d.attr("class")===b.cssPrefix+"expanded";j.onSegmentEvent(b,b.options.callbacks.onMouseoverSegment,d,h)}),c.on("mouseout",function(c){var d;if(d3.select(c.currentTarget).attr("class")===b.cssPrefix+"arc")d=a(c.currentTarget).find("path");else{var e=a(c.currentTarget).data("index");d=a("#"+b.cssPrefix+"segment"+e)}if(b.options.effects.highlightSegmentOnMouseover){var e=d.data("index"),f=b.options.colors[e];b.options.misc.gradient.enabled&&(f="url(#"+b.cssPrefix+"grad"+e+")"),d3.select(d[0]).style("fill",f)}var g=d.attr("class")===b.cssPrefix+"expanded";j.onSegmentEvent(b,b.options.callbacks.onMouseoutSegment,d,g)})},onSegmentEvent:function(b,c,d,e){if(a.isFunction(c))try{var f=parseInt(d.data("index"),10);c({segment:d[0],index:f,expanded:e,data:b.options.data[f]})}catch(g){}},openSegment:function(b,c){b.isOpeningSegment||(b.isOpeningSegment=!0,a("."+b.cssPrefix+"expanded").length>0&&j.closeSegment(b,a("."+b.cssPrefix+"expanded")[0]),d3.select(c).transition().ease(b.options.effects.pullOutSegmentOnClick.effect).duration(b.options.effects.pullOutSegmentOnClick.speed).attr("transform",function(a){var c=b.arc.centroid(a),d=c[0],e=c[1],f=Math.sqrt(d*d+e*e),g=parseInt(b.options.effects.pullOutSegmentOnClick.size,10);return"translate("+d/f*g+","+e/f*g+")"}).each("end",function(){b.currentlyOpenSegment=c,b.isOpeningSegment=!1,a(this).attr("class",b.cssPrefix+"expanded")}))},closeSegment:function(b,c){d3.select(c).transition().duration(400).attr("transform","translate(0,0)").each("end",function(){a(this).attr("class",""),b.currentlyOpenSegment=null})},getCentroid:function(a){var b=a.getBBox();return{x:b.x+b.width/2,y:b.y+b.height/2}},getSegmentAngle:function(b,c,d,e){var f,g=a.extend({compounded:!0,midpoint:!1},e),h=c[b].value;if(g.compounded){f=0;for(var i=0;b>=i;i++)f+=c[i].value}"undefined"==typeof f&&(f=h);var j=f/d*360;if(g.midpoint){var k=h/d*360;j-=k/2}return j},getPercentage:function(a,b){return Math.floor(a.options.data[b].value/a.totalSize*100)}},k={offscreenCoord:-1e4,addTitle:function(a){a.svg.selectAll("."+a.cssPrefix+"title").data([a.options.header.title]).enter().append("text").text(function(a){return a.text}).attr("id",a.cssPrefix+"title").attr("class",a.cssPrefix+"title").attr("x",k.offscreenCoord).attr("y",k.offscreenCoord).attr("text-anchor",function(){var b;return b="top-center"===a.options.header.location||"pie-center"===a.options.header.location?"middle":"left"}).attr("fill",function(a){return a.color}).style("font-size",function(a){return a.fontSize+"px"}).style("font-family",function(a){return a.font})},positionTitle:function(a){var b,c=a.textComponents,d=a.options.header.location,e=a.options.misc.canvasPadding,f=a.options.size.canvasWidth,g=a.options.header.titleSubtitlePadding;b="top-left"===d?e.left:(f-e.right)/2+e.left;var h=e.top+c.title.h;if("pie-center"===d)if(h=a.pieCenter.y,c.subtitle.exists){var i=c.title.h+g+c.subtitle.h;h=h-i/2+c.title.h}else h+=c.title.h/4;a.svg.select("#"+a.cssPrefix+"title").attr("x",b).attr("y",h)},addSubtitle:function(a){var b=a.options.header.location;a.svg.selectAll("."+a.cssPrefix+"subtitle").data([a.options.header.subtitle]).enter().append("text").text(function(a){return a.text}).attr("x",k.offscreenCoord).attr("y",k.offscreenCoord).attr("id",a.cssPrefix+"subtitle").attr("class",a.cssPrefix+"subtitle").attr("text-anchor",function(){var a;return a="top-center"===b||"pie-center"===b?"middle":"left"}).attr("fill",function(a){return a.color}).style("font-size",function(a){return a.fontSize+"px"}).style("font-family",function(a){return a.font})},positionSubtitle:function(a){var b,c=a.options.misc.canvasPadding,d=a.options.size.canvasWidth;b="top-left"===a.options.header.location?c.left:(d-c.right)/2+c.left;var e=k.getHeaderHeight(a);a.svg.select("#"+a.cssPrefix+"subtitle").attr("x",b).attr("y",e)},addFooter:function(a){a.svg.selectAll("."+a.cssPrefix+"footer").data([a.options.footer]).enter().append("text").text(function(a){return a.text}).attr("x",k.offscreenCoord).attr("y",k.offscreenCoord).attr("id",a.cssPrefix+"footer").attr("class",a.cssPrefix+"footer").attr("text-anchor",function(){var b="left";return"bottom-center"===a.options.footer.location?b="middle":"bottom-right"===a.options.footer.location&&(b="left"),b}).attr("fill",function(a){return a.color}).style("font-size",function(a){return a.fontSize+"px"}).style("font-family",function(a){return a.font})},positionFooter:function(a){var b,c=a.options.footer.location,d=a.textComponents.footer.w,e=a.options.size.canvasWidth,f=a.options.size.canvasHeight,g=a.options.misc.canvasPadding;b="bottom-left"===c?g.left:"bottom-right"===c?e-d-g.right:e/2,a.svg.select("#"+a.cssPrefix+"footer").attr("x",b).attr("y",f-g.bottom)},getHeaderHeight:function(a){var b;if(a.textComponents.title.exists){var c=a.textComponents.title.h+a.options.header.titleSubtitlePadding+a.textComponents.subtitle.h;b="pie-center"===a.options.header.location?a.pieCenter.y-c/2+c:c+a.options.misc.canvasPadding.top}else if("pie-center"===a.options.header.location){var d=a.options.misc.canvasPadding.bottom+a.textComponents.footer.h;b=(a.options.size.canvasHeight-d)/2+a.options.misc.canvasPadding.top+a.textComponents.subtitle.h/2}else b=a.options.misc.canvasPadding.top+a.textComponents.subtitle.h;return b}},l=function(i,j){this.element=i,"string"==typeof i&&(this.element=/#/.test(i)?a(i)[0]:a("#"+i)[0]),this.options=a.extend(!0,{},e,j),null!==this.options.misc.cssPrefix?this.cssPrefix=this.options.misc.cssPrefix:(this.cssPrefix="p"+d+"_",d++),f.initialCheck(this)&&(a(this.element).data(b,c),this.options.data=h.sortPieData(this),this.options.colors=g.initSegmentColors(this),this.totalSize=h.getTotalPieSize(this.options.data),m.call(this))};l.prototype.redraw=function(){this.element.innerHTML="",m.call(this)},l.prototype.destroy=function(){this.element.innerHTML="",a(this.element).removeData(b)},l.prototype.getOpenSegment=function(){var b=this.currentlyOpenSegment;if(null!==b&&"undefined"!=typeof b){var c=parseInt(a(b).data("index"),10);return{element:b,index:c,data:this.options.data[c]}}return null},l.prototype.openSegment=function(b){var b=parseInt(b,10);0>b||b>this.options.data.length-1||j.openSegment(this,a("#"+this.cssPrefix+"segment"+b)[0])},l.prototype.closeSegment=function(){var a=this.currentlyOpenSegment;a&&j.closeSegment(this,a)},l.prototype.updateProp=function(b,c){switch(b){case"header.title.text":var d=g.processObj(this.options,b);g.processObj(this.options,b,c),a("#"+this.cssPrefix+"title").html(c),(""===d&&""!==c||""!==d&&""===c)&&this.redraw();break;case"header.subtitle.text":var e=g.processObj(this.options,b);g.processObj(this.options,b,c),a("#"+this.cssPrefix+"subtitle").html(c),(""===e&&""!==c||""!==e&&""===c)&&this.redraw();break;case"callbacks.onload":case"callbacks.onMouseoverSegment":case"callbacks.onMouseoutSegment":case"callbacks.onClickSegment":case"effects.pullOutSegmentOnClick.effect":case"effects.pullOutSegmentOnClick.speed":case"effects.pullOutSegmentOnClick.size":case"effects.highlightSegmentOnMouseover":case"effects.highlightLuminosity":g.processObj(this.options,b,c)}};var m=function(){this.svg=g.addSVGSpace(this),this.textComponents={headerHeight:0,title:{exists:""!==this.options.header.title.text,h:0,w:0},subtitle:{exists:""!==this.options.header.subtitle.text,h:0,w:0},footer:{exists:""!==this.options.footer.text,h:0,w:0}},this.outerLabelGroupData=[],this.textComponents.title.exists&&k.addTitle(this),this.textComponents.subtitle.exists&&k.addSubtitle(this),k.addFooter(this);var a=this;g.whenIdExists(this.cssPrefix+"footer",function(){k.positionFooter(a);var b=g.getDimensions(a.cssPrefix+"footer");a.textComponents.footer.h=b.h,a.textComponents.footer.w=b.w});var b=[];this.textComponents.title.exists&&b.push(this.cssPrefix+"title"),this.textComponents.subtitle.exists&&b.push(this.cssPrefix+"subtitle"),this.textComponents.footer.exists&&b.push(this.cssPrefix+"footer"),g.whenElementsExist(b,function(){if(a.textComponents.title.exists){var b=g.getDimensions(a.cssPrefix+"title");a.textComponents.title.h=b.h,a.textComponents.title.w=b.w}if(a.textComponents.subtitle.exists){var c=g.getDimensions(a.cssPrefix+"subtitle");a.textComponents.subtitle.h=c.h,a.textComponents.subtitle.w=c.w}if(a.textComponents.title.exists||a.textComponents.subtitle.exists){var d=0;a.textComponents.title.exists&&(d+=a.textComponents.title.h,a.textComponents.subtitle.exists&&(d+=a.options.header.titleSubtitlePadding)),a.textComponents.subtitle.exists&&(d+=a.textComponents.subtitle.h),a.textComponents.headerHeight=d}h.computePieRadius(a),h.calculatePieCenter(a),k.positionTitle(a),k.positionSubtitle(a),a.options.misc.gradient.enabled&&j.addGradients(a),j.create(a),i.add(a,"inner",a.options.labels.inner.format),i.add(a,"outer",a.options.labels.outer.format),i.positionLabelElements(a,"inner",a.options.labels.inner.format),i.positionLabelElements(a,"outer",a.options.labels.outer.format),i.computeOuterLabelCoords(a),i.positionLabelGroups(a,"outer"),i.computeLabelLinePositions(a),a.options.labels.lines.enabled&&"none"!==a.options.labels.outer.format&&i.addLabelLines(a),i.positionLabelGroups(a,"inner"),i.fadeInLabelsAndLines(a),j.addSegmentEventHandlers(a)})};window.d3pie=l}(jQuery); \ No newline at end of file diff --git a/website/examples/single.html b/website/examples/single.html index 493f4fd..c42a48f 100644 --- a/website/examples/single.html +++ b/website/examples/single.html @@ -9,7 +9,7 @@

- A pie chart with a single item. Thrilling stuff, really. + A pie chart with a single item. Thrilling stuff, really. This is just for testing purposes.