pie.js 12 KB

1
  1. define("echarts/chart/pie","require ./base zrender/shape/Text zrender/shape/Ring zrender/shape/Circle zrender/shape/Sector zrender/shape/Polyline ../config ../util/ecData zrender/tool/util zrender/tool/math zrender/tool/color ../chart".split(" "),function(r){function B(b,d,e,a,c){D.call(this,b,d,e,a,c);var f=this;f.shapeHandler.onmouseover=function(a){a=a.target;var b=v.get(a,"seriesIndex"),c=v.get(a,"dataIndex"),e=v.get(a,"special"),d=[a.style.x,a.style.y],m=((a.style.endAngle+a.style.startAngle)/2+360)%360,u=a.highlightStyle.color;(e=f.getLabel(b,c,e,d,m,u,!0))&&f.zr.addHoverShape(e);(a=f.getLabelLine(b,c,d,a.style.r0,a.style.r,m,u,!0))&&f.zr.addHoverShape(a)};this.refresh(a)}var D=r("./base"),p=r("zrender/shape/Text"),q=r("zrender/shape/Ring"),F=r("zrender/shape/Circle"),G=r("zrender/shape/Sector"),H=r("zrender/shape/Polyline"),C=r("../config");C.pie={zlevel:0,z:2,clickable:!0,legendHoverLink:!0,center:["50%","50%"],radius:[0,"55%"],clockWise:!0,startAngle:90,minAngle:0,selectedOffset:10,itemStyle:{normal:{borderColor:"rgba(0,0,0,0)",borderWidth:1,label:{show:!0,position:"outer"},labelLine:{show:!0,length:10,lineStyle:{width:1,type:"solid"}}},emphasis:{borderColor:"rgba(0,0,0,0)",borderWidth:1,label:{show:!1},labelLine:{show:!1,length:10,lineStyle:{width:1,type:"solid"}}}}};var v=r("../util/ecData"),z=r("zrender/tool/util"),w=r("zrender/tool/math"),I=r("zrender/tool/color");return B.prototype={type:C.CHART_TYPE_PIE,_buildShape:function(){var b=this.series,d=this.component.legend;this.selectedMap={};this._selected={};var e,a,c;this._selectedMode=!1;for(var f,h=0,g=b.length;g>h;h++)b[h].type===C.CHART_TYPE_PIE&&(b[h]=this.reformOption(b[h]),this.legendHoverLink=b[h].legendHoverLink||this.legendHoverLink,f=b[h].name||"",this.selectedMap[f]=d?d.isSelected(f):!0,this.selectedMap[f])&&(e=this.parseCenter(this.zr,b[h].center),a=this.parseRadius(this.zr,b[h].radius),this._selectedMode=this._selectedMode||b[h].selectedMode,this._selected[h]=[],this.deepQuery([b[h],this.option],"calculable")&&(c={zlevel:this.getZlevelBase(),z:this.getZBase(),hoverable:!1,style:{x:e[0],y:e[1],r0:10>=a[0]?0:a[0]-10,r:a[1]+10,brushType:"stroke",lineWidth:1,strokeColor:b[h].calculableHolderColor||this.ecTheme.calculableHolderColor||C.calculableHolderColor}},v.pack(c,b[h],h,void 0,-1),this.setCalculable(c),c=10>=a[0]?new F(c):new q(c),this.shapeList.push(c)),this._buildSinglePie(h),this.buildMark(h));this.addShapeList()},_buildSinglePie:function(b){for(var d,e=this.series[b],a=e.data,c=this.component.legend,f=0,h=0,g=0,n=Number.NEGATIVE_INFINITY,l=[],k=0,m=a.length;m>k;k++)d=a[k].name,this.selectedMap[d]=c?c.isSelected(d):!0,this.selectedMap[d]&&!isNaN(a[k].value)&&(0!==+a[k].value?f++:h++,g+=+a[k].value,n=Math.max(n,+a[k].value));if(0!==g){for(var u,x,y,t,p,A=100,q=e.clockWise,r=(e.startAngle.toFixed(2)-0+360)%360,v=e.minAngle||.01,h=360-v*f-.01*h,E=e.roseType,k=0,m=a.length;m>k;k++)if(d=a[k].name,this.selectedMap[d]&&!isNaN(a[k].value)){if(f=c?c.getColor(d):this.zr.getColor(k),A=a[k].value/g,u="area"!=E?q?r-A*h-(0!==A?v:.01):A*h+r+(0!==A?v:.01):q?r-360/m:360/m+r,u=u.toFixed(2)-0,A=(100*A).toFixed(2),x=this.parseCenter(this.zr,e.center),y=this.parseRadius(this.zr,e.radius),t=+y[0],p=+y[1],"radius"===E?p=a[k].value/n*(p-t)*.8+.2*(p-t)+t:"area"===E&&(p=Math.sqrt(a[k].value/n)*(p-t)+t),q){var w;w=r;r=u;u=w}this._buildItem(l,b,k,A,a[k].selected,x,t,p,r,u,f);q||(r=u)}this._autoLabelLayout(l,x,p);k=0;for(m=l.length;m>k;k++)this.shapeList.push(l[k])}},_buildItem:function(b,d,e,a,c,f,h,g,n,l,k){var m=this.series,u=((l+n)/2+360)%360;c=this.getSector(d,e,a,c,f,h,g,n,l,k);v.pack(c,m[d],d,m[d].data[e],e,m[d].data[e].name,a);b.push(c);c=this.getLabel(d,e,a,f,u,k,!1);(f=this.getLabelLine(d,e,f,h,g,u,k,!1))&&(v.pack(f,m[d],d,m[d].data[e],e,m[d].data[e].name,a),b.push(f));c&&(v.pack(c,m[d],d,m[d].data[e],e,m[d].data[e].name,a),c._labelLine=f,b.push(c))},getSector:function(b,d,e,a,c,f,h,g,n,l){e=this.series[b];var k=e.data[d],m=[k,e],u=this.deepMerge(m,"itemStyle.normal")||{},x=this.deepMerge(m,"itemStyle.emphasis")||{};l=this.getItemStyleColor(u.color,b,d,k)||l;var y=this.getItemStyleColor(x.color,b,d,k)||("string"==typeof l?I.lift(l,-.2):l);c={zlevel:this.getZlevelBase(),z:this.getZBase(),clickable:this.deepQuery(m,"clickable"),style:{x:c[0],y:c[1],r0:f,r:h,startAngle:g,endAngle:n,brushType:"both",color:l,lineWidth:u.borderWidth,strokeColor:u.borderColor,lineJoin:"round"},highlightStyle:{color:y,lineWidth:x.borderWidth,strokeColor:x.borderColor,lineJoin:"round"},_seriesIndex:b,_dataIndex:d};a?(a=((c.style.startAngle+c.style.endAngle)/2).toFixed(2)-0,c.style._hasSelected=!0,c.style._x=c.style.x,c.style._y=c.style.y,f=this.query(e,"selectedOffset"),c.style.x+=w.cos(a,!0)*f,c.style.y-=w.sin(a,!0)*f,this._selected[b][d]=!0):this._selected[b][d]=!1;return this._selectedMode&&(c.onclick=this.shapeHandler.onclick),this.deepQuery([k,e,this.option],"calculable")&&(this.setCalculable(c),c.draggable=!0),(this._needLabel(e,k,!0)||this._needLabelLine(e,k,!0))&&(c.onmouseover=this.shapeHandler.onmouseover),new G(c)},getLabel:function(b,d,e,a,c,f,h){var g=this.series[b],n=g.data[d];if(this._needLabel(g,n,h)){var l,k,m,u=h?"emphasis":"normal",x=z.merge(z.clone(n.itemStyle)||{},g.itemStyle);h=x[u].label;var y=h.textStyle||{},t=a[0];a=a[1];g=this.parseRadius(this.zr,g.radius);h.position=h.position||x.normal.label.position;"center"===h.position?(l=t,k=a,m="center"):"inner"===h.position||"inside"===h.position?(g=(g[0]+g[1])*(h.distance||.5),l=Math.round(t+g*w.cos(c,!0)),k=Math.round(a-g*w.sin(c,!0)),f="#fff",m="center"):(g=g[1]- -x[u].labelLine.length,l=Math.round(t+g*w.cos(c,!0)),k=Math.round(a-g*w.sin(c,!0)),m=90<=c&&270>=c?"right":"left");"center"!=h.position&&"inner"!=h.position&&"inside"!=h.position&&(l+="left"===m?20:-20);n.__labelX=l-("left"===m?5:-5);n.__labelY=k;e=new p({zlevel:this.getZlevelBase(),z:this.getZBase()+1,hoverable:!1,style:{x:l,y:k,color:y.color||f,text:this.getLabelText(b,d,e,u),textAlign:y.align||m,textBaseline:y.baseline||"middle",textFont:this.getFont(y)},highlightStyle:{brushType:"fill"}});return e._radius=g,e._labelPosition=h.position||"outer",e._rect=e.getRect(e.style),e._seriesIndex=b,e._dataIndex=d,e}},getLabelText:function(b,d,e,a){var c=this.series[b],f=c.data[d];return(a=this.deepQuery([f,c],"itemStyle."+a+".label.formatter"))?"function"==typeof a?a.call(this.myChart,{seriesIndex:b,seriesName:c.name||"",series:c,dataIndex:d,data:f,name:f.name,value:f.value,percent:e}):"string"==typeof a?(a=a.replace("{a}","{a0}").replace("{b}","{b0}").replace("{c}","{c0}").replace("{d}","{d0}"),a.replace("{a0}",c.name).replace("{b0}",f.name).replace("{c0}",f.value).replace("{d0}",e)):void 0:f.name},getLabelLine:function(b,d,e,a,c,f,h,g){var n=this.series[b];a=n.data[d];if(this._needLabelLine(n,a,g)){g=g?"emphasis":"normal";var l=z.merge(z.clone(a.itemStyle)||{},n.itemStyle)[g].labelLine;g=l.lineStyle||{};var k=e[0];e=e[1];n=this.parseRadius(this.zr,n.radius)[1]- -l.length;l=w.cos(f,!0);f=w.sin(f,!0);return new H({zlevel:this.getZlevelBase(),z:this.getZBase()+1,hoverable:!1,style:{pointList:[[k+c*l,e-c*f],[k+n*l,e-n*f],[a.__labelX,a.__labelY]],strokeColor:g.color||h,lineType:g.type,lineWidth:g.width},_seriesIndex:b,_dataIndex:d})}},_needLabel:function(b,d,e){return this.deepQuery([d,b],"itemStyle."+(e?"emphasis":"normal")+".label.show")},_needLabelLine:function(b,d,e){return this.deepQuery([d,b],"itemStyle."+(e?"emphasis":"normal")+".labelLine.show")},_autoLabelLayout:function(b,d,e){for(var a=[],c=[],f=0,h=b.length;h>f;f++)("outer"===b[f]._labelPosition||"outside"===b[f]._labelPosition)&&(b[f]._rect._y=b[f]._rect.y,b[f]._rect.x<d[0]?a.push(b[f]):c.push(b[f]));this._layoutCalculate(a,d,e,-1);this._layoutCalculate(c,d,e,1)},_layoutCalculate:function(b,d,e,a){function c(a,c,e){for(var d=a;c>d;d++)if(b[d]._rect.y+=e,b[d].style.y+=e,b[d]._labelLine&&(b[d]._labelLine.style.pointList[1][1]+=e,b[d]._labelLine.style.pointList[2][1]+=e),d>a&&c>d+1&&b[d+1]._rect.y>b[d]._rect.y+b[d]._rect.height)return void f(d,e/2);f(c-1,e/2)}function f(a,d){for(var c=a;0<=c&&(b[c]._rect.y-=d,b[c].style.y-=d,b[c]._labelLine&&(b[c]._labelLine.style.pointList[1][1]-=d,b[c]._labelLine.style.pointList[2][1]-=d),!(0<c&&b[c]._rect.y>b[c-1]._rect.y+b[c-1]._rect.height));c--);}function h(a,b,c,d,e){var f,g,h=c[0];c=c[1];for(var l=b?Number.MAX_VALUE:0,k=0,m=a.length;m>k;k++)f=Math.abs(a[k]._rect.y-c),g=a[k]._radius-d,f=d+g>f?Math.sqrt((d+g+20)*(d+g+20)-Math.pow(a[k]._rect.y-c,2)):Math.abs(a[k]._rect.x+(0<e?0:a[k]._rect.width)-h),b&&f>=l&&(f=l-10),!b&&l>=f&&(f=l+10),a[k]._rect.x=a[k].style.x=h+f*e,a[k]._labelLine&&(a[k]._labelLine.style.pointList[2][0]=h+(f-5)*e,a[k]._labelLine.style.pointList[1][0]=h+(f-20)*e),l=f}b.sort(function(a,b){return a._rect.y-b._rect.y});var g;g=0;for(var n=b.length,l=[],k=[],m=0;n>m;m++)g=b[m]._rect.y-g,0>g&&c(m,n,-g,a),g=b[m]._rect.y+b[m]._rect.height;0>this.zr.getHeight()-g&&f(n-1,g-this.zr.getHeight());for(m=0;n>m;m++)b[m]._rect.y>=d[1]?k.push(b[m]):l.push(b[m]);h(k,!0,d,e,a);h(l,!1,d,e,a)},reformOption:function(b){var d=z.merge;return b=d(d(b||{},z.clone(this.ecTheme.pie||{})),z.clone(C.pie)),b.itemStyle.normal.label.textStyle=this.getTextStyle(b.itemStyle.normal.label.textStyle),b.itemStyle.emphasis.label.textStyle=this.getTextStyle(b.itemStyle.emphasis.label.textStyle),b},refresh:function(b){b&&(this.option=b,this.series=b.series);this.backupShapeList();this._buildShape()},addDataAnimation:function(b){for(var d=this.series,e={},a=0,c=b.length;c>a;a++)e[b[a][0]]=b[a];var f={},h={},g={},n=this.shapeList;this.shapeList=[];for(var l,k,m,p={},a=0,c=b.length;c>a;a++)l=b[a][0],k=b[a][2],m=b[a][3],d[l]&&d[l].type===C.CHART_TYPE_PIE&&(k?(m||(f[l+"_"+d[l].data.length]="delete"),p[l]=1):m?p[l]=0:(f[l+"_-1"]="delete",p[l]=-1),this._buildSinglePie(l));for(var r,q,a=0,c=this.shapeList.length;c>a;a++)switch(l=this.shapeList[a]._seriesIndex,r=this.shapeList[a]._dataIndex,q=l+"_"+r,this.shapeList[a].type){case "sector":f[q]=this.shapeList[a];break;case "text":h[q]=this.shapeList[a];break;case "polyline":g[q]=this.shapeList[a]}this.shapeList=[];for(var t,a=0,c=n.length;c>a;a++)if((l=n[a]._seriesIndex,e[l])&&(r=n[a]._dataIndex+p[l],q=l+"_"+r,t=f[q],t))if("sector"===n[a].type)"delete"!=t?this.zr.animate(n[a].id,"style").when(400,{startAngle:t.style.startAngle,endAngle:t.style.endAngle}).start():this.zr.animate(n[a].id,"style").when(400,0>p[l]?{startAngle:n[a].style.startAngle}:{endAngle:n[a].style.endAngle}).start();else if("text"===n[a].type||"polyline"===n[a].type)if("delete"===t)this.zr.delShape(n[a].id);else switch(n[a].type){case "text":t=h[q];this.zr.animate(n[a].id,"style").when(400,{x:t.style.x,y:t.style.y}).start();break;case "polyline":t=g[q],this.zr.animate(n[a].id,"style").when(400,{pointList:t.style.pointList}).start()}this.shapeList=n},onclick:function(b){var d=this.series;if(this.isClick&&b.target){this.isClick=!1;for(var e,a=b.target,c=a.style,f=v.get(a,"seriesIndex"),h=v.get(a,"dataIndex"),g=0,n=this.shapeList.length;n>g;g++)if(this.shapeList[g].id===a.id){if(f=v.get(a,"seriesIndex"),h=v.get(a,"dataIndex"),c._hasSelected)a.style.x=a.style._x,a.style.y=a.style._y,a.style._hasSelected=!1,this._selected[f][h]=!1;else{var l=((c.startAngle+c.endAngle)/2).toFixed(2)-0;a.style._hasSelected=!0;this._selected[f][h]=!0;a.style._x=a.style.x;a.style._y=a.style.y;e=this.query(d[f],"selectedOffset");a.style.x+=w.cos(l,!0)*e;a.style.y-=w.sin(l,!0)*e}this.zr.modShape(a.id,a)}else this.shapeList[g].style._hasSelected&&"single"===this._selectedMode&&(f=v.get(this.shapeList[g],"seriesIndex"),h=v.get(this.shapeList[g],"dataIndex"),this.shapeList[g].style.x=this.shapeList[g].style._x,this.shapeList[g].style.y=this.shapeList[g].style._y,this.shapeList[g].style._hasSelected=!1,this._selected[f][h]=!1,this.zr.modShape(this.shapeList[g].id,this.shapeList[g]));this.messageCenter.dispatch(C.EVENT.PIE_SELECTED,b.event,{selected:this._selected,target:v.get(a,"name")},this.myChart);this.zr.refreshNextFrame()}}},z.inherits(B,D),r("../chart").define("pie",B),B});define("zrender/shape/Ring",["require","./Base","../tool/util"],function(r){var B=r("./Base"),D=function(p){B.call(this,p)};return D.prototype={type:"ring",buildPath:function(p,q){p.arc(q.x,q.y,q.r,0,2*Math.PI,!1);p.moveTo(q.x+q.r0,q.y);p.arc(q.x,q.y,q.r0,0,2*Math.PI,!0)},getRect:function(p){if(p.__rect)return p.__rect;var q;return q="stroke"==p.brushType||"fill"==p.brushType?p.lineWidth||1:0,p.__rect={x:Math.round(p.x-p.r-q/2),y:Math.round(p.y-p.r-q/2),width:2*p.r+q,height:2*p.r+q},p.__rect}},r("../tool/util").inherits(D,B),D});