From aa5e535480ec890043cd1a55bd1d6b8f00ff837e Mon Sep 17 00:00:00 2001 From: Gonzalo Almeida Date: Thu, 15 Mar 2012 12:40:54 -0300 Subject: [PATCH 1/3] fixing bug: if all votes are in one option the pie chart is not drawn (we must draw a circle not a path) --- g.pie.js | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/g.pie.js b/g.pie.js index df2afb3..c35c146 100644 --- a/g.pie.js +++ b/g.pie.js @@ -78,26 +78,38 @@ others && values.splice(len) && (values[cut].others = true); for (i = 0; i < len; i++) { - var mangle = angle - 360 * values[i] / total / 2; - - if (!i) { - angle = 90 - mangle; - mangle = angle - 360 * values[i] / total / 2; - } - - if (opts.init) { - var ipath = sector(cx, cy, 1, angle, angle - 360 * values[i] / total).join(","); + //if all votes are in only one option must draw a circle + if (values[i] / total === 1) { + var circle = paper.circle(cx, cy, r).attr({ fill: chartinst.colors[i], stroke: opts.stroke || "#fff", "stroke-width": opts.strokewidth == null ? 1 : opts.strokewidth }) + circle.value = values[i]; + series.push(circle); + sectors.push(circle); + covers.push(paper.circle(cx, cy, r).attr(chartinst.shim)); + total = values[i]; + series[i].middle = {x: cx, y: cy}; + series[i].mangle = 180; + } else { + var mangle = angle - 360 * values[i] / total / 2; + + if (!i) { + angle = 90 - mangle; + mangle = angle - 360 * values[i] / total / 2; + } + + if (opts.init) { + var ipath = sector(cx, cy, 1, angle, angle - 360 * values[i] / total).join(","); + } + + var path = sector(cx, cy, r, angle, angle -= 360 * values[i] / total); + var p = paper.path(opts.init ? ipath : path).attr({ fill: opts.colors && opts.colors[i] || chartinst.colors[i] || "#666", stroke: opts.stroke || "#fff", "stroke-width": (opts.strokewidth == null ? 1 : opts.strokewidth), "stroke-linejoin": "round" }); + + p.value = values[i]; + p.middle = path.middle; + p.mangle = mangle; + sectors.push(p); + series.push(p); + opts.init && p.animate({ path: path.join(",") }, (+opts.init - 1) || 1000, ">"); } - - var path = sector(cx, cy, r, angle, angle -= 360 * values[i] / total); - var p = paper.path(opts.init ? ipath : path).attr({ fill: opts.colors && opts.colors[i] || chartinst.colors[i] || "#666", stroke: opts.stroke || "#fff", "stroke-width": (opts.strokewidth == null ? 1 : opts.strokewidth), "stroke-linejoin": "round" }); - - p.value = values[i]; - p.middle = path.middle; - p.mangle = mangle; - sectors.push(p); - series.push(p); - opts.init && p.animate({ path: path.join(",") }, (+opts.init - 1) || 1000, ">"); } for (i = 0; i < len; i++) { From bbfab7205696162889ac62626a87749501cedf69 Mon Sep 17 00:00:00 2001 From: Gonzalo Almeida Date: Thu, 15 Mar 2012 12:49:14 -0300 Subject: [PATCH 2/3] minify the changes --- min/g.pie-min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/min/g.pie-min.js b/min/g.pie-min.js index 624adeb..a88af26 100644 --- a/min/g.pie-min.js +++ b/min/g.pie-min.js @@ -1 +1 @@ -(function(){function b(n,h,g,t,e,o){o=o||{};var c=this,q=[],k=n.set(),s=n.set(),m=n.set(),x=[],z=e.length,A=0,D=0,C=0,f=9,B=true;function w(I,H,i,K,G,P){var M=Math.PI/180,E=I+i*Math.cos(-K*M),p=I+i*Math.cos(-G*M),J=I+i/2*Math.cos(-(K+(G-K)/2)*M),O=H+i*Math.sin(-K*M),N=H+i*Math.sin(-G*M),F=H+i/2*Math.sin(-(K+(G-K)/2)*M),L=["M",I,H,"L",E,O,"A",i,i,0,+(Math.abs(G-K)>180),1,p,N,"z"];L.middle={x:J,y:F};return L}s.covers=k;if(z==1){m.push(n.circle(h,g,t).attr({fill:c.colors[0],stroke:o.stroke||"#fff","stroke-width":o.strokewidth==null?1:o.strokewidth}));k.push(n.circle(h,g,t).attr(c.shim));D=e[0];e[0]={value:e[0],order:0,valueOf:function(){return this.value}};m[0].middle={x:h,y:g};m[0].mangle=180}else{for(var y=0;yf){B=false;e[f].value+=e[y];e[f].others=true;C=e[f].value}}z=Math.min(f+1,e.length);C&&e.splice(z)&&(e[f].others=true);for(y=0;y")}for(y=0;y180),1,x2,y2,"z"];res.middle={x:xm,y:ym};return res}chart.covers=covers;if(len==1){series.push(paper.circle(cx,cy,r).attr({fill:chartinst.colors[0],stroke:opts.stroke||"#fff","stroke-width":opts.strokewidth==null?1:opts.strokewidth}));covers.push(paper.circle(cx,cy,r).attr(chartinst.shim));total=values[0];values[0]={value:values[0],order:0,valueOf:function(){return this.value}};series[0].middle={x:cx,y:cy};series[0].mangle=180}else{for(var i=0;icut){defcut=false;values[cut].value+=values[i];values[cut].others=true;others=values[cut].value}}len=Math.min(cut+1,values.length);others&&values.splice(len)&&(values[cut].others=true);for(i=0;i")}}for(i=0;i Date: Thu, 15 Mar 2012 12:57:54 -0300 Subject: [PATCH 3/3] fixing typo (missing ;) --- g.pie.js | 2 +- min/g.pie-min.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/g.pie.js b/g.pie.js index c35c146..3f82cb7 100644 --- a/g.pie.js +++ b/g.pie.js @@ -80,7 +80,7 @@ for (i = 0; i < len; i++) { //if all votes are in only one option must draw a circle if (values[i] / total === 1) { - var circle = paper.circle(cx, cy, r).attr({ fill: chartinst.colors[i], stroke: opts.stroke || "#fff", "stroke-width": opts.strokewidth == null ? 1 : opts.strokewidth }) + var circle = paper.circle(cx, cy, r).attr({ fill: chartinst.colors[i], stroke: opts.stroke || "#fff", "stroke-width": opts.strokewidth == null ? 1 : opts.strokewidth }); circle.value = values[i]; series.push(circle); sectors.push(circle); diff --git a/min/g.pie-min.js b/min/g.pie-min.js index a88af26..dc3c982 100644 --- a/min/g.pie-min.js +++ b/min/g.pie-min.js @@ -1 +1 @@ -(function(){function Piechart(paper,cx,cy,r,values,opts){opts=opts||{};var chartinst=this,sectors=[],covers=paper.set(),chart=paper.set(),series=paper.set(),order=[],len=values.length,angle=0,total=0,others=0,cut=9,defcut=true;function sector(cx,cy,r,startAngle,endAngle,fill){var rad=Math.PI/180,x1=cx+r*Math.cos(-startAngle*rad),x2=cx+r*Math.cos(-endAngle*rad),xm=cx+r/2*Math.cos(-(startAngle+(endAngle-startAngle)/2)*rad),y1=cy+r*Math.sin(-startAngle*rad),y2=cy+r*Math.sin(-endAngle*rad),ym=cy+r/2*Math.sin(-(startAngle+(endAngle-startAngle)/2)*rad),res=["M",cx,cy,"L",x1,y1,"A",r,r,0,+(Math.abs(endAngle-startAngle)>180),1,x2,y2,"z"];res.middle={x:xm,y:ym};return res}chart.covers=covers;if(len==1){series.push(paper.circle(cx,cy,r).attr({fill:chartinst.colors[0],stroke:opts.stroke||"#fff","stroke-width":opts.strokewidth==null?1:opts.strokewidth}));covers.push(paper.circle(cx,cy,r).attr(chartinst.shim));total=values[0];values[0]={value:values[0],order:0,valueOf:function(){return this.value}};series[0].middle={x:cx,y:cy};series[0].mangle=180}else{for(var i=0;icut){defcut=false;values[cut].value+=values[i];values[cut].others=true;others=values[cut].value}}len=Math.min(cut+1,values.length);others&&values.splice(len)&&(values[cut].others=true);for(i=0;i")}}for(i=0;i180),1,x2,y2,"z"];res.middle={x:xm,y:ym};return res}chart.covers=covers;if(len==1){series.push(paper.circle(cx,cy,r).attr({fill:chartinst.colors[0],stroke:opts.stroke||"#fff","stroke-width":opts.strokewidth==null?1:opts.strokewidth}));covers.push(paper.circle(cx,cy,r).attr(chartinst.shim));total=values[0];values[0]={value:values[0],order:0,valueOf:function(){return this.value}};series[0].middle={x:cx,y:cy};series[0].mangle=180}else{for(var i=0;icut){defcut=false;values[cut].value+=values[i];values[cut].others=true;others=values[cut].value}}len=Math.min(cut+1,values.length);others&&values.splice(len)&&(values[cut].others=true);for(i=0;i")}}for(i=0;i