diff --git a/dist/js/pizza.js b/dist/js/pizza.js index 9fa687b..9476a62 100644 --- a/dist/js/pizza.js +++ b/dist/js/pizza.js @@ -1,5 +1,5 @@ var Pizza = { - version : '0.1.2', + version : '0.2.0', settings : { donut: false, diff --git a/dist/js/pizza.min.js b/dist/js/pizza.min.js index 63a8ada..176d367 100644 --- a/dist/js/pizza.min.js +++ b/dist/js/pizza.min.js @@ -1 +1 @@ -var Pizza={version:"0.1.2",settings:{donut:!1,donut_inner_ratio:.4,percent_offset:35,show_text:!0,animation_speed:500,always_show_text:!1,show_grid:!0,bar_spacer:100,bar_intervals:6,animation_type:"elastic"},NS:"http://www.w3.org/2000/svg",init:function(a,b){var c=this;this.scope=a||document.body;var d=$("[data-pie-id], [data-line-id], [data-bar-id]",this.scope);$.extend(!0,this.settings,b),d.length>0?d.each(function(){return c.build($(this),b)}):($(this.scope).is("[data-pie-id]")||$(this.scope).is("[data-line-id]")||$(this.scope).is("[data-bar-id]"))&&this.build($(this.scope),b),this.events()},events:function(){var a=this;$(window).off(".pizza").on("resize.pizza",a.throttle(function(){a.init()},500)),$(this.scope).off(".pizza"),this.pie_events(),this.line_events(),this.bar_events()},build:function(a,b){a.data("settings",$.extend({},this.settings,b,a.data("options"))),this.data(a,b||{}),a.data("pie-id")?this.update_DOM(this.pie(a)):a.data("line-id")?this.update_DOM(this.line(a)):a.data("bar-id")&&this.update_DOM(this.bar(a))},data:function(a,b){var c=[];return $("li",a).each(function(){var a=$(this);c.push(b.data?{value:b.data[a.index()],text:a.data("text"),color:a.css("color"),segment:a}:{x:a.data("x"),y:a.data("y"),value:a.data("value"),text:a.data("text"),color:a.css("color"),segment:a})}),a.data("graph-data",c)},update_DOM:function(a){var b=a[0],c=a[1];return $(this.identifier(b)).html(c)},animate:function(a,b,c,d,e){var e=e||1.05;a.hover(function(a){var f=Snap(a.target),g=Snap(f.node.nextSibling);f.animate({transform:"s"+e+" "+e+" "+b+" "+c},d.animation_speed,mina[d.animation_type]),/text/.test(g.node.nodeName)&&(g.touchend(function(){Snap(f).animate({transform:"s"+e+" "+e+" "+b+" "+c},d.animation_speed,mina[d.animation_type])}),d.show_text&&(g.animate({opacity:1},d.animation_speed),g.touchend(function(){g.animate({opacity:1},d.animation_speed)})))},function(a){var e=Snap(a.target),f=Snap(e.node.nextSibling);e.animate({transform:"s1 1 "+b+" "+c},d.animation_speed,mina[d.animation_type]),/text/.test(f.node.nodeName)&&f.animate({opacity:0},d.animation_speed)})},parse_options:function(a,b,c){for(var d=a.match(/{{(percent|value)}}/g),e="",f=0;f0)d=d[0];else{var d=this.svg_obj("svg");d.width=e,d.height=g}if(f)var h="-"+b.percent_offset+" -"+b.percent_offset+" "+(e+1.5*b.percent_offset)+" "+(e+1.5*b.percent_offset);else var h="-"+b.percent_offset+" -"+b.percent_offset+" "+(e+1.6*b.percent_offset)+" "+(g+1.6*b.percent_offset);return this.set_attr(d,{width:"100%",height:"100%",viewBox:h}),d},identifier:function(a){return id=a.data("pie-id")||a.data("bar-id")||a.data("line-id"),"#"+id},throttle:function(a,b){var c=null;return function(){var d=this,e=arguments;clearTimeout(c),c=setTimeout(function(){a.apply(d,e)},b)}},svg_obj:function(a){return document.createElementNS(this.NS,a)},ticks:function(a,b,c){var d=b-a,e=Math.pow(10,Math.floor(Math.log(d/c)/Math.LN10)),f=c/d*e;.15>=f?e*=10:.35>=f?e*=5:.75>=f&&(e*=2);var g=Math.ceil(a/e)*e,h=Math.floor(b/e)*e+.5*e,j=[];for(i=g;h>i;i+=e)j.push(i);return j},set_attr:function(a,b){for(attr in b)a.setAttribute(attr,b[attr]);return this},flip:function(a,b){a.setAttribute("transform","translate(0, "+b+") scale(1, -1)")}};$.extend(Pizza,{bar:function(a){var b=a.data("settings"),c=this.svg(a,b),d=a.data("graph-data"),e=0,f=$(this.identifier(a)),g=f.outerWidth(),h=f.outerHeight(),i=min=0,j=0,k=b.bar_spacer*(b.bar_spacer/g),l=(g-d.length*k)/d.length;10>l&&(k=1,l=(g-d.length*k)/d.length);for(var m=0;md[m].value&&(min=d[m].value),j+=d[m].value;var n=$("g[data-id=bars]",c);if(n.length>0)var o=n[0];else{var o=this.svg_obj("g");o.setAttribute("data-id","bars")}b.show_grid&&this.assemble_grid(c,min,i,g,h,b),o.setAttribute("transform","translate(0, "+h+") scale(1, -1)");for(var m=0;m0)var r=q[0];else{var r=this.svg_obj("rect");r.setAttribute("data-id","r"+m)}if(0===e)var s=e;else var s=e+k;r.setAttribute("data-y",p),this.set_attr(r,{x:s,y:0,width:l,height:0,fill:d[m].color,stroke:b.stroke_color,strokeWidth:b.stroke_width}),Snap(r).animate({height:p},1500,mina[b.animation_type]),e=s+l,n.length<1&&(o.appendChild(r),this.animate_bar(Snap(r),p,b))}return n.length<1&&c.appendChild(o),[a,c]},animate_bar:function(a,b,c){var d=($(a),b+15);a.hover(function(a){var b=Snap(a.target);b.animate({height:d},c.animation_speed,mina[c.animation_type])},function(a){var d=Snap(a.target);d.animate({height:b},c.animation_speed,mina[c.animation_type])})},assemble_grid:function(a,b,c,d,e,f){var g=$("g[data-id=bars]",a);if(g.length>0)var h=$("g[data-id=grid]",a)[0],j=$("g[data-id=labels]",a)[0];else{var h=this.svg_obj("g"),j=this.svg_obj("g");h.setAttribute("data-id","grid"),j.setAttribute("data-id","labels")}for(var k=this.ticks(b,c,f.bar_intervals),l=i=k.length,m=e/(l-1),n=0;i--;){if(g.length>0)var o=$("line[data-id=l"+i+"]",h)[0],p=$("text[data-id=t"+i+"]",j)[0];else{var o=this.svg_obj("line"),p=this.svg_obj("text");o.setAttribute("data-id","l"+i),p.setAttribute("data-id","t"+i)}var q=n+m;this.set_attr(o,{x1:0,x2:d,y1:q,y2:q,stroke:"gray","stroke-width":1,"stroke-dasharray":"5,5"}).set_attr(p,{x:-8,y:q+5,"text-anchor":"end"}),p.innerHTML=k[i],g.length<1&&(h.appendChild(o),j.appendChild(p)),n=q}h.setAttribute("transform","translate(0, -"+n/l+")"),j.setAttribute("transform","translate(0, -"+n/l+")"),g.length<1&&(a.appendChild(h),a.appendChild(j))},bar_events:function(){$(this.scope).on("mouseenter.pizza mouseleave.pizza touchstart.pizza","[data-bar-id] li",function(a){var b=$(this).parent(),c=$("#"+b.attr("data-bar-id")+" rect[data-id=r"+$(this).index()+"]")[0],d=$(this).parent().data("settings"),e=parseInt(c.getAttribute("data-y"),10)+15;/start/i.test(a.type)&&$(c).siblings("rect").each(function(){this.nodeName&&Snap(c).animate({height:c.getAttribute("data-y")},d.animation_speed,mina[d.animation_type])}),/enter|start/i.test(a.type)?Snap(c).animate({height:e},d.animation_speed,mina[d.animation_type]):Snap(c).animate({height:c.getAttribute("data-y")},d.animation_speed,mina[d.animation_type])})}}),$.extend(Pizza,{line:function(a){for(var b=a.data("settings"),c=this.svg(a,b),d=$(this.identifier(a)),e=d.outerWidth(),f=d.outerHeight(),g=a.data("graph-data"),h=max_y=min_x=min_y=total_x=total_y=0,i=g.length,j="",i=0;ih&&(h=g[i].x),g[i].y>max_y&&(max_y=g[i].y),min_x>g[i].x&&(min_x=g[i].x),min_y>g[i].y&&(min_y=g[i].y),total_x+=g[i].x,total_y+=g[i].y;var k=$("g[data-id=line]",c);if(k.length>0)var l=$("g[data-id=line]",c)[0],m=$("g[data-id=points]",c)[0],n=$("path[data-id=path]",l)[0];else{var n=this.svg_obj("path"),l=this.svg_obj("g"),m=this.svg_obj("g");l.setAttribute("data-id","line"),m.setAttribute("data-id","points"),n.setAttribute("data-id","path")}for(var i=0;i0)var o=$("circle[data-id=c"+i+"]",m)[0];else{var o=this.svg_obj("circle");o.setAttribute("data-id","c"+i)}var p=g[i].x/h*e,q=g[i].y/max_y*f;j+=p+","+q+" ",this.set_attr(o,{cx:p,cy:q,r:0,fill:g[i.color],"data-value":g[i].x+", "+g[i].y,"data-tooltip":"",title:g[i].x+", "+g[i].y,"class":"has-tip tip-top"}),Snap(o).animate({r:4},1500,mina[b.animation_type]),this.animate(Snap(o),p,q,b,2),k.length<1&&m.appendChild(o)}this.flip(m,f),this.flip(l,f),b.show_grid&&(this.assemble_grid_x(c,min_x,h,e,f,b),this.assemble_grid_y(c,min_y,max_y,e,f,b));var r=this.points_to_path(j);return this.set_attr(n,{d:r,fill:"none",stroke:"black","stroke-width":2}),k.length<1&&(l.appendChild(n),c.appendChild(l)),k.length<1&&c.appendChild(m),[a,c]},assemble_grid_x:function(a,b,c,d,e,f){var g=$("g[data-id=gridx]",a);if(g.length>0)var h=g[0],j=$("g[data-id=labelx]",a)[0];else{var h=this.svg_obj("g"),j=this.svg_obj("g");h.setAttribute("data-id","gridx"),j.setAttribute("data-id","labelx")}for(var k=this.ticks(b,c,f.bar_intervals).reverse(),l=i=k.length,m=0,n=d/(l-1);i--;){if(g.length>0)var o=$("line[data-id=l"+i+"]",h)[0],p=$("text[data-id=t"+i+"]",j)[0];else{var o=this.svg_obj("line"),p=this.svg_obj("text");o.setAttribute("data-id","l"+i),p.setAttribute("data-id","t"+i)}var q=m+n;this.set_attr(o,{x1:q,x2:q,y1:0,y2:e,stroke:"gray","stroke-width":1,"stroke-dasharray":"5,5"}).set_attr(p,{y:e+20,x:q-n,"text-anchor":"middle"}),g.length<1&&(p.innerHTML=k[i],j.appendChild(p),h.appendChild(o)),m=q}h.setAttribute("transform","translate(-"+n+", 0)"),g.length<1&&(a.appendChild(h),a.appendChild(j))},assemble_grid_y:function(a,b,c,d,e,f){var g=$("g[data-id=gridy]",a);if(g.length>0)var h=g[0],j=$("g[data-id=labely]",a)[0];else{var h=this.svg_obj("g"),j=this.svg_obj("g");h.setAttribute("data-id","gridy"),j.setAttribute("data-id","labely")}for(var k=this.ticks(b,c,f.bar_intervals),l=i=k.length,m=0;i--;){if(g.length>0)var n=$("line[data-id=l"+i+"]",h)[0],o=$("text[data-id=t"+i+"]",j)[0];else{var n=this.svg_obj("line"),o=this.svg_obj("text");n.setAttribute("data-id","l"+i),o.setAttribute("data-id","t"+i)}var p=m+e/(l-1);this.set_attr(n,{x1:0,x2:d,y1:p,y2:p,stroke:"gray","stroke-width":1,"stroke-dasharray":"5,5"}).set_attr(o,{x:-8,y:p+5,"text-anchor":"end"}),g.length<1&&(j.appendChild(o),h.appendChild(n),o.innerHTML=k[i]),m=p}h.setAttribute("transform","translate(0, -"+m/l+")"),j.setAttribute("transform","translate(0, -"+m/l+")"),g.length<1&&(a.appendChild(h),a.appendChild(j))},points_to_path:function(a){{var a=a.split(/\s+|,/),b=a.shift(),c=a.shift();"M"+b+","+c+"L"+a.join(" ")}return["M"+b+","+c+"L"].concat(a).join(" ")},line_events:function(){$(this.scope).on("mouseenter.pizza mouseleave.pizza touchstart.pizza","[data-line-id] li",function(a){var b=$(this).parent(),c=$("#"+b.data("line-id")+' circle[data-id="c'+$(this).index()+'"]')[0],d=$(this).parent().data("settings");/start/i.test(a.type)&&$(c).siblings("circle").each(function(){this.nodeName&&Snap(c).animate({transform:"s1 1 "+c.getAttribute("cx")+" "+c.getAttribute("cy")},d.animation_speed,mina[d.animation_type])}),/enter|start/i.test(a.type)?(Snap(c).animate({transform:"s2 2 "+c.getAttribute("cx")+" "+c.getAttribute("cy")},d.animation_speed,mina[d.animation_type]),$(c).trigger("mouseenter")):(Snap(c).animate({transform:"s1 1 "+c.getAttribute("cx")+" "+c.getAttribute("cy")},d.animation_speed,mina[d.animation_type]),$(c).trigger("mouseout"))})}}),$.extend(Pizza,{pie:function(a){for(var b=a.data("settings"),c=this.svg(a,b),d=a.data("graph-data"),e=0,f=[],g=0,h=$(this.identifier(a)),i=h.outerWidth(),j=0;jMath.PI&&(s=1);var t="M"+l+","+m+" L"+o+","+p+" A"+n+","+n+" 0 "+s+" 1 "+q+","+r+" Z"}var u=$('path[data-id="s'+j+'"]',c);if(u.length>0)var v=u[0];else var v=this.svg_obj("path");var w=d[j].value/e*100,x=$('text[data-id="s'+j+'"]',c);if(x.length>0){var y=x[0];y.setAttribute("x",l+(n+b.percent_offset)*Math.sin(g+f[j]/2)),y.setAttribute("y",m-(n+b.percent_offset)*Math.cos(g+f[j]/2))}else{if(d[j].text)var z=this.parse_options(d[j].text,w,d[j].value);else var z=Math.ceil(w)+"%";var y=this.svg_obj("text");y.setAttribute("x",l+(n+b.percent_offset)*Math.sin(g+f[j]/2)),y.setAttribute("y",m-(n+b.percent_offset)*Math.cos(g+f[j]/2)),y.innerHTML=z}y.setAttribute("text-anchor","middle"),b.always_show_text?Snap(y).animate({opacity:1},b.animation_speed):Snap(y).attr({opacity:0},b.animation_speed),y.setAttribute("data-id","s"+j),b.donut?this.annular_sector(v,{centerX:l,centerY:m,startDegrees:g,endDegrees:k,innerRadius:n*b.donut_inner_ratio,outerRadius:n}):v.setAttribute("d",t),this.set_attr(v,{fill:d[j].color,stroke:b.stroke_color,strokeWidth:b.stroke_width,"data-cx":l,"data-cy":m,"data-id":"s"+j});var A=$("g[data-id=g"+j+"]",c);if(A.length<1){var B=this.svg_obj("g");B.setAttribute("data-id","g"+j),B.appendChild(v),B.appendChild(y),c.appendChild(B),this.animate(Snap(v),l,m,b)}g=k}return[a,c]},annular_sector:function(a,b){function c(a){var b={cx:a.centerX||0,cy:a.centerY||0,startRadians:a.startDegrees||0,closeRadians:a.endDegrees||0},c=void 0!==a.thickness?a.thickness:100;return b.r1=void 0!==a.innerRadius?a.innerRadius:void 0!==a.outerRadius?a.outerRadius-c:200-c,b.r2=void 0!==a.outerRadius?a.outerRadius:b.r1+c,b.r1<0&&(b.r1=0),b.r2<0&&(b.r2=0),b}var d=c(b),e=[[d.cx+d.r2*Math.sin(d.startRadians),d.cy-d.r2*Math.cos(d.startRadians)],[d.cx+d.r2*Math.sin(d.closeRadians),d.cy-d.r2*Math.cos(d.closeRadians)],[d.cx+d.r1*Math.sin(d.closeRadians),d.cy-d.r1*Math.cos(d.closeRadians)],[d.cx+d.r1*Math.sin(d.startRadians),d.cy-d.r1*Math.cos(d.startRadians)]],f=d.closeRadians-d.startRadians,g=f%(2*Math.PI)>Math.PI?1:0,h=[];h.push("M"+e[0].join()),h.push("A"+[d.r2,d.r2,0,g,1,e[1]].join()),h.push("L"+e[2].join()),h.push("A"+[d.r1,d.r1,0,g,0,e[3]].join()),h.push("z"),a.setAttribute("d",h.join(" "))},pie_events:function(){$(this.scope).on("mouseenter.pizza mouseleave.pizza touchstart.pizza","[data-pie-id] li",function(a){var b=$(this).parent(),c=$("#"+b.attr("data-pie-id")+' path[data-id="s'+$(this).index()+'"]')[0],d=c.nextSibling,e=$(this).parent().data("settings");/start/i.test(a.type)&&$(c).siblings("path").each(function(){this.nodeName&&(Snap(c).animate({transform:"s1 1 "+c.getAttribute("data-cx")+" "+c.getAttribute("data-cy")},e.animation_speed,mina[e.animation_type]),Snap($(this).next()[0]).animate({opacity:0},e.animation_speed))}),/enter|start/i.test(a.type)?(Snap(c).animate({transform:"s1.05 1.05 "+c.getAttribute("data-cx")+" "+c.getAttribute("data-cy")},e.animation_speed,mina[e.animation_type]),e.show_text&&Snap(d).animate({opacity:1},e.animation_speed)):(Snap(c).animate({transform:"s1 1 "+c.getAttribute("data-cx")+" "+c.getAttribute("data-cy")},e.animation_speed,mina[e.animation_type]),Snap(d).animate({opacity:0},e.animation_speed))})}}); \ No newline at end of file +var Pizza={version:"0.2.0",settings:{donut:!1,donut_inner_ratio:.4,percent_offset:35,show_text:!0,animation_speed:500,always_show_text:!1,show_grid:!0,bar_spacer:100,bar_intervals:6,animation_type:"elastic"},NS:"http://www.w3.org/2000/svg",init:function(a,b){var c=this;this.scope=a||document.body;var d=$("[data-pie-id], [data-line-id], [data-bar-id]",this.scope);$.extend(!0,this.settings,b),d.length>0?d.each(function(){return c.build($(this),b)}):($(this.scope).is("[data-pie-id]")||$(this.scope).is("[data-line-id]")||$(this.scope).is("[data-bar-id]"))&&this.build($(this.scope),b),this.events()},events:function(){var a=this;$(window).off(".pizza").on("resize.pizza",a.throttle(function(){a.init()},500)),$(this.scope).off(".pizza"),this.pie_events(),this.line_events(),this.bar_events()},build:function(a,b){a.data("settings",$.extend({},this.settings,b,a.data("options"))),this.data(a,b||{}),a.data("pie-id")?this.update_DOM(this.pie(a)):a.data("line-id")?this.update_DOM(this.line(a)):a.data("bar-id")&&this.update_DOM(this.bar(a))},data:function(a,b){var c=[];return $("li",a).each(function(){var a=$(this);c.push(b.data?{value:b.data[a.index()],text:a.data("text"),color:a.css("color"),segment:a}:{x:a.data("x"),y:a.data("y"),value:a.data("value"),text:a.data("text"),color:a.css("color"),segment:a})}),a.data("graph-data",c)},update_DOM:function(a){var b=a[0],c=a[1];return $(this.identifier(b)).html(c)},animate:function(a,b,c,d,e){var e=e||1.05;a.hover(function(a){var f=Snap(a.target),g=Snap(f.node.nextSibling);f.animate({transform:"s"+e+" "+e+" "+b+" "+c},d.animation_speed,mina[d.animation_type]),/text/.test(g.node.nodeName)&&(g.touchend(function(){Snap(f).animate({transform:"s"+e+" "+e+" "+b+" "+c},d.animation_speed,mina[d.animation_type])}),d.show_text&&(g.animate({opacity:1},d.animation_speed),g.touchend(function(){g.animate({opacity:1},d.animation_speed)})))},function(a){var e=Snap(a.target),f=Snap(e.node.nextSibling);e.animate({transform:"s1 1 "+b+" "+c},d.animation_speed,mina[d.animation_type]),/text/.test(f.node.nodeName)&&f.animate({opacity:0},d.animation_speed)})},parse_options:function(a,b,c){for(var d=a.match(/{{(percent|value)}}/g),e="",f=0;f0)d=d[0];else{var d=this.svg_obj("svg");d.width=e,d.height=g}if(f)var h="-"+b.percent_offset+" -"+b.percent_offset+" "+(e+1.5*b.percent_offset)+" "+(e+1.5*b.percent_offset);else var h="-"+b.percent_offset+" -"+b.percent_offset+" "+(e+1.6*b.percent_offset)+" "+(g+1.6*b.percent_offset);return this.set_attr(d,{width:"100%",height:"100%",viewBox:h}),d},identifier:function(a){return id=a.data("pie-id")||a.data("bar-id")||a.data("line-id"),"#"+id},throttle:function(a,b){var c=null;return function(){var d=this,e=arguments;clearTimeout(c),c=setTimeout(function(){a.apply(d,e)},b)}},svg_obj:function(a){return document.createElementNS(this.NS,a)},ticks:function(a,b,c){var d=b-a,e=Math.pow(10,Math.floor(Math.log(d/c)/Math.LN10)),f=c/d*e;.15>=f?e*=10:.35>=f?e*=5:.75>=f&&(e*=2);var g=Math.ceil(a/e)*e,h=Math.floor(b/e)*e+.5*e,j=[];for(i=g;h>i;i+=e)j.push(i);return j},set_attr:function(a,b){for(attr in b)a.setAttribute(attr,b[attr]);return this},flip:function(a,b){a.setAttribute("transform","translate(0, "+b+") scale(1, -1)")}};$.extend(Pizza,{bar:function(a){var b=a.data("settings"),c=this.svg(a,b),d=a.data("graph-data"),e=0,f=$(this.identifier(a)),g=f.outerWidth(),h=f.outerHeight(),i=min=0,j=0,k=b.bar_spacer*(b.bar_spacer/g),l=(g-d.length*k)/d.length;10>l&&(k=1,l=(g-d.length*k)/d.length);for(var m=0;md[m].value&&(min=d[m].value),j+=d[m].value;var n=$("g[data-id=bars]",c);if(n.length>0)var o=n[0];else{var o=this.svg_obj("g");o.setAttribute("data-id","bars")}b.show_grid&&this.assemble_grid(c,min,i,g,h,b),o.setAttribute("transform","translate(0, "+h+") scale(1, -1)");for(var m=0;m0)var r=q[0];else{var r=this.svg_obj("rect");r.setAttribute("data-id","r"+m)}if(0===e)var s=e;else var s=e+k;r.setAttribute("data-y",p),this.set_attr(r,{x:s,y:0,width:l,height:0,fill:d[m].color,stroke:b.stroke_color,strokeWidth:b.stroke_width}),Snap(r).animate({height:p},1500,mina[b.animation_type]),e=s+l,n.length<1&&(o.appendChild(r),this.animate_bar(Snap(r),p,b))}return n.length<1&&c.appendChild(o),[a,c]},animate_bar:function(a,b,c){var d=($(a),b+15);a.hover(function(a){var b=Snap(a.target);b.animate({height:d},c.animation_speed,mina[c.animation_type])},function(a){var d=Snap(a.target);d.animate({height:b},c.animation_speed,mina[c.animation_type])})},assemble_grid:function(a,b,c,d,e,f){var g=$("g[data-id=bars]",a);if(g.length>0)var h=$("g[data-id=grid]",a)[0],j=$("g[data-id=labels]",a)[0];else{var h=this.svg_obj("g"),j=this.svg_obj("g");h.setAttribute("data-id","grid"),j.setAttribute("data-id","labels")}for(var k=this.ticks(b,c,f.bar_intervals),l=i=k.length,m=e/(l-1),n=0;i--;){if(g.length>0)var o=$("line[data-id=l"+i+"]",h)[0],p=$("text[data-id=t"+i+"]",j)[0];else{var o=this.svg_obj("line"),p=this.svg_obj("text");o.setAttribute("data-id","l"+i),p.setAttribute("data-id","t"+i)}var q=n+m;this.set_attr(o,{x1:0,x2:d,y1:q,y2:q,stroke:"gray","stroke-width":1,"stroke-dasharray":"5,5"}).set_attr(p,{x:-8,y:q+5,"text-anchor":"end"}),p.innerHTML=k[i],g.length<1&&(h.appendChild(o),j.appendChild(p)),n=q}h.setAttribute("transform","translate(0, -"+n/l+")"),j.setAttribute("transform","translate(0, -"+n/l+")"),g.length<1&&(a.appendChild(h),a.appendChild(j))},bar_events:function(){$(this.scope).on("mouseenter.pizza mouseleave.pizza touchstart.pizza","[data-bar-id] li",function(a){var b=$(this).parent(),c=$("#"+b.attr("data-bar-id")+" rect[data-id=r"+$(this).index()+"]")[0],d=$(this).parent().data("settings"),e=parseInt(c.getAttribute("data-y"),10)+15;/start/i.test(a.type)&&$(c).siblings("rect").each(function(){this.nodeName&&Snap(c).animate({height:c.getAttribute("data-y")},d.animation_speed,mina[d.animation_type])}),/enter|start/i.test(a.type)?Snap(c).animate({height:e},d.animation_speed,mina[d.animation_type]):Snap(c).animate({height:c.getAttribute("data-y")},d.animation_speed,mina[d.animation_type])})}}),$.extend(Pizza,{line:function(a){for(var b=a.data("settings"),c=this.svg(a,b),d=$(this.identifier(a)),e=d.outerWidth(),f=d.outerHeight(),g=a.data("graph-data"),h=max_y=min_x=min_y=total_x=total_y=0,i=g.length,j="",i=0;ih&&(h=g[i].x),g[i].y>max_y&&(max_y=g[i].y),min_x>g[i].x&&(min_x=g[i].x),min_y>g[i].y&&(min_y=g[i].y),total_x+=g[i].x,total_y+=g[i].y;var k=$("g[data-id=line]",c);if(k.length>0)var l=$("g[data-id=line]",c)[0],m=$("g[data-id=points]",c)[0],n=$("path[data-id=path]",l)[0];else{var n=this.svg_obj("path"),l=this.svg_obj("g"),m=this.svg_obj("g");l.setAttribute("data-id","line"),m.setAttribute("data-id","points"),n.setAttribute("data-id","path")}for(var i=0;i0)var o=$("circle[data-id=c"+i+"]",m)[0];else{var o=this.svg_obj("circle");o.setAttribute("data-id","c"+i)}var p=g[i].x/h*e,q=g[i].y/max_y*f;j+=p+","+q+" ",this.set_attr(o,{cx:p,cy:q,r:0,fill:g[i.color],"data-value":g[i].x+", "+g[i].y,"data-tooltip":"",title:g[i].x+", "+g[i].y,"class":"has-tip tip-top"}),Snap(o).animate({r:4},1500,mina[b.animation_type]),this.animate(Snap(o),p,q,b,2),k.length<1&&m.appendChild(o)}this.flip(m,f),this.flip(l,f),b.show_grid&&(this.assemble_grid_x(c,min_x,h,e,f,b),this.assemble_grid_y(c,min_y,max_y,e,f,b));var r=this.points_to_path(j);return this.set_attr(n,{d:r,fill:"none",stroke:"black","stroke-width":2}),k.length<1&&(l.appendChild(n),c.appendChild(l)),k.length<1&&c.appendChild(m),[a,c]},assemble_grid_x:function(a,b,c,d,e,f){var g=$("g[data-id=gridx]",a);if(g.length>0)var h=g[0],j=$("g[data-id=labelx]",a)[0];else{var h=this.svg_obj("g"),j=this.svg_obj("g");h.setAttribute("data-id","gridx"),j.setAttribute("data-id","labelx")}for(var k=this.ticks(b,c,f.bar_intervals).reverse(),l=i=k.length,m=0,n=d/(l-1);i--;){if(g.length>0)var o=$("line[data-id=l"+i+"]",h)[0],p=$("text[data-id=t"+i+"]",j)[0];else{var o=this.svg_obj("line"),p=this.svg_obj("text");o.setAttribute("data-id","l"+i),p.setAttribute("data-id","t"+i)}var q=m+n;this.set_attr(o,{x1:q,x2:q,y1:0,y2:e,stroke:"gray","stroke-width":1,"stroke-dasharray":"5,5"}).set_attr(p,{y:e+20,x:q-n,"text-anchor":"middle"}),g.length<1&&(p.innerHTML=k[i],j.appendChild(p),h.appendChild(o)),m=q}h.setAttribute("transform","translate(-"+n+", 0)"),g.length<1&&(a.appendChild(h),a.appendChild(j))},assemble_grid_y:function(a,b,c,d,e,f){var g=$("g[data-id=gridy]",a);if(g.length>0)var h=g[0],j=$("g[data-id=labely]",a)[0];else{var h=this.svg_obj("g"),j=this.svg_obj("g");h.setAttribute("data-id","gridy"),j.setAttribute("data-id","labely")}for(var k=this.ticks(b,c,f.bar_intervals),l=i=k.length,m=0;i--;){if(g.length>0)var n=$("line[data-id=l"+i+"]",h)[0],o=$("text[data-id=t"+i+"]",j)[0];else{var n=this.svg_obj("line"),o=this.svg_obj("text");n.setAttribute("data-id","l"+i),o.setAttribute("data-id","t"+i)}var p=m+e/(l-1);this.set_attr(n,{x1:0,x2:d,y1:p,y2:p,stroke:"gray","stroke-width":1,"stroke-dasharray":"5,5"}).set_attr(o,{x:-8,y:p+5,"text-anchor":"end"}),g.length<1&&(j.appendChild(o),h.appendChild(n),o.innerHTML=k[i]),m=p}h.setAttribute("transform","translate(0, -"+m/l+")"),j.setAttribute("transform","translate(0, -"+m/l+")"),g.length<1&&(a.appendChild(h),a.appendChild(j))},points_to_path:function(a){{var a=a.split(/\s+|,/),b=a.shift(),c=a.shift();"M"+b+","+c+"L"+a.join(" ")}return["M"+b+","+c+"L"].concat(a).join(" ")},line_events:function(){$(this.scope).on("mouseenter.pizza mouseleave.pizza touchstart.pizza","[data-line-id] li",function(a){var b=$(this).parent(),c=$("#"+b.data("line-id")+' circle[data-id="c'+$(this).index()+'"]')[0],d=$(this).parent().data("settings");/start/i.test(a.type)&&$(c).siblings("circle").each(function(){this.nodeName&&Snap(c).animate({transform:"s1 1 "+c.getAttribute("cx")+" "+c.getAttribute("cy")},d.animation_speed,mina[d.animation_type])}),/enter|start/i.test(a.type)?(Snap(c).animate({transform:"s2 2 "+c.getAttribute("cx")+" "+c.getAttribute("cy")},d.animation_speed,mina[d.animation_type]),$(c).trigger("mouseenter")):(Snap(c).animate({transform:"s1 1 "+c.getAttribute("cx")+" "+c.getAttribute("cy")},d.animation_speed,mina[d.animation_type]),$(c).trigger("mouseout"))})}}),$.extend(Pizza,{pie:function(a){for(var b=a.data("settings"),c=this.svg(a,b),d=a.data("graph-data"),e=0,f=[],g=0,h=$(this.identifier(a)),i=h.outerWidth(),j=0;jMath.PI&&(s=1);var t="M"+l+","+m+" L"+o+","+p+" A"+n+","+n+" 0 "+s+" 1 "+q+","+r+" Z"}var u=$('path[data-id="s'+j+'"]',c);if(u.length>0)var v=u[0];else var v=this.svg_obj("path");var w=d[j].value/e*100,x=$('text[data-id="s'+j+'"]',c);if(x.length>0){var y=x[0];y.setAttribute("x",l+(n+b.percent_offset)*Math.sin(g+f[j]/2)),y.setAttribute("y",m-(n+b.percent_offset)*Math.cos(g+f[j]/2))}else{if(d[j].text)var z=this.parse_options(d[j].text,w,d[j].value);else var z=Math.ceil(w)+"%";var y=this.svg_obj("text");y.setAttribute("x",l+(n+b.percent_offset)*Math.sin(g+f[j]/2)),y.setAttribute("y",m-(n+b.percent_offset)*Math.cos(g+f[j]/2)),y.innerHTML=z}y.setAttribute("text-anchor","middle"),b.always_show_text?Snap(y).animate({opacity:1},b.animation_speed):Snap(y).attr({opacity:0},b.animation_speed),y.setAttribute("data-id","s"+j),b.donut?this.annular_sector(v,{centerX:l,centerY:m,startDegrees:g,endDegrees:k,innerRadius:n*b.donut_inner_ratio,outerRadius:n}):v.setAttribute("d",t),this.set_attr(v,{fill:d[j].color,stroke:b.stroke_color,strokeWidth:b.stroke_width,"data-cx":l,"data-cy":m,"data-id":"s"+j});var A=$("g[data-id=g"+j+"]",c);if(A.length<1){var B=this.svg_obj("g");B.setAttribute("data-id","g"+j),B.appendChild(v),B.appendChild(y),c.appendChild(B),this.animate(Snap(v),l,m,b)}g=k}return[a,c]},annular_sector:function(a,b){function c(a){var b={cx:a.centerX||0,cy:a.centerY||0,startRadians:a.startDegrees||0,closeRadians:a.endDegrees||0},c=void 0!==a.thickness?a.thickness:100;return b.r1=void 0!==a.innerRadius?a.innerRadius:void 0!==a.outerRadius?a.outerRadius-c:200-c,b.r2=void 0!==a.outerRadius?a.outerRadius:b.r1+c,b.r1<0&&(b.r1=0),b.r2<0&&(b.r2=0),b}var d=c(b),e=[[d.cx+d.r2*Math.sin(d.startRadians),d.cy-d.r2*Math.cos(d.startRadians)],[d.cx+d.r2*Math.sin(d.closeRadians),d.cy-d.r2*Math.cos(d.closeRadians)],[d.cx+d.r1*Math.sin(d.closeRadians),d.cy-d.r1*Math.cos(d.closeRadians)],[d.cx+d.r1*Math.sin(d.startRadians),d.cy-d.r1*Math.cos(d.startRadians)]],f=d.closeRadians-d.startRadians,g=f%(2*Math.PI)>Math.PI?1:0,h=[];h.push("M"+e[0].join()),h.push("A"+[d.r2,d.r2,0,g,1,e[1]].join()),h.push("L"+e[2].join()),h.push("A"+[d.r1,d.r1,0,g,0,e[3]].join()),h.push("z"),a.setAttribute("d",h.join(" "))},pie_events:function(){$(this.scope).on("mouseenter.pizza mouseleave.pizza touchstart.pizza","[data-pie-id] li",function(a){var b=$(this).parent(),c=$("#"+b.attr("data-pie-id")+' path[data-id="s'+$(this).index()+'"]')[0],d=c.nextSibling,e=$(this).parent().data("settings");/start/i.test(a.type)&&$(c).siblings("path").each(function(){this.nodeName&&(Snap(c).animate({transform:"s1 1 "+c.getAttribute("data-cx")+" "+c.getAttribute("data-cy")},e.animation_speed,mina[e.animation_type]),Snap($(this).next()[0]).animate({opacity:0},e.animation_speed))}),/enter|start/i.test(a.type)?(Snap(c).animate({transform:"s1.05 1.05 "+c.getAttribute("data-cx")+" "+c.getAttribute("data-cy")},e.animation_speed,mina[e.animation_type]),e.show_text&&Snap(d).animate({opacity:1},e.animation_speed)):(Snap(c).animate({transform:"s1 1 "+c.getAttribute("data-cx")+" "+c.getAttribute("data-cy")},e.animation_speed,mina[e.animation_type]),Snap(d).animate({opacity:0},e.animation_speed))})}}); \ No newline at end of file diff --git a/js/pizza.js b/js/pizza.js index c77a641..7537bfd 100644 --- a/js/pizza.js +++ b/js/pizza.js @@ -1,5 +1,5 @@ var Pizza = { - version : '0.1.2', + version : '0.2.0', settings : { donut: false,