root / trunk / web / dojo / dojox / charting / plot2d / Pie.js @ 9
History | View | Annotate | Download (6.71 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.charting.plot2d.Pie"]){ |
||
9 | dojo._hasResource["dojox.charting.plot2d.Pie"]=true; |
||
10 | dojo.provide("dojox.charting.plot2d.Pie");
|
||
11 | dojo.require("dojox.charting.Element");
|
||
12 | dojo.require("dojox.charting.axis2d.common");
|
||
13 | dojo.require("dojox.charting.plot2d.common");
|
||
14 | dojo.require("dojox.charting.plot2d._PlotEvents");
|
||
15 | dojo.require("dojox.lang.functional");
|
||
16 | dojo.require("dojox.lang.utils");
|
||
17 | dojo.require("dojox.gfx");
|
||
18 | dojo.require("dojo.number");
|
||
19 | (function(){
|
||
20 | var df=dojox.lang.functional,du=dojox.lang.utils,dc=dojox.charting.plot2d.common,da=dojox.charting.axis2d.common,g=dojox.gfx,m=g.matrix,_1=0.2; |
||
21 | dojo.declare("dojox.charting.plot2d.Pie",[dojox.charting.Element,dojox.charting.plot2d._PlotEvents],{defaultParams:{labels:true,ticks:false,fixed:true,precision:1,labelOffset:20,labelStyle:"default",htmlLabels:true,radGrad:"native",fanSize:5,startAngle:0},optionalParams:{radius:0,stroke:{},outline:{},shadow:{},fill:{},font:"",fontColor:""},constructor:function(_2,_3){ |
||
22 | this.opt=dojo.clone(this.defaultParams); |
||
23 | du.updateWithObject(this.opt,_3);
|
||
24 | du.updateWithPattern(this.opt,_3,this.optionalParams); |
||
25 | this.run=null; |
||
26 | this.dyn=[];
|
||
27 | },clear:function(){ |
||
28 | this.dirty=true; |
||
29 | this.dyn=[];
|
||
30 | this.run=null; |
||
31 | return this; |
||
32 | },setAxis:function(_4){ |
||
33 | return this; |
||
34 | },addSeries:function(_5){ |
||
35 | this.run=_5;
|
||
36 | return this; |
||
37 | },getSeriesStats:function(){ |
||
38 | return dojo.delegate(dc.defaultStats);
|
||
39 | },initializeScalers:function(){ |
||
40 | return this; |
||
41 | },getRequiredColors:function(){ |
||
42 | return this.run?this.run.data.length:0; |
||
43 | },render:function(_6,_7){ |
||
44 | if(!this.dirty){ |
||
45 | return this; |
||
46 | } |
||
47 | this.resetEvents();
|
||
48 | this.dirty=false; |
||
49 | this._eventSeries={};
|
||
50 | this.cleanGroup();
|
||
51 | var s=this.group,t=this.chart.theme; |
||
52 | if(!this.run||!this.run.data.length){ |
||
53 | return this; |
||
54 | } |
||
55 | var rx=(_6.width-_7.l-_7.r)/2,ry=(_6.height-_7.t-_7.b)/2,r=Math.min(rx,ry),_8="font" in this.opt?this.opt.font:t.axis.font,_9=_8?g.normalizedLength(g.splitFontString(_8).size):0,_a="fontColor" in this.opt?this.opt.fontColor:t.axis.fontColor,_b=m._degToRad(this.opt.startAngle),_c=_b,_d,_e,_f,_10,_11,_12,run=this.run.data,_13=this.events(); |
||
56 | if(typeof run[0]=="number"){ |
||
57 | _e=df.map(run,"x ? Math.max(x, 0) : 0");
|
||
58 | if(df.every(_e,"<= 0")){ |
||
59 | return this; |
||
60 | } |
||
61 | _f=df.map(_e,"/this",df.foldl(_e,"+",0)); |
||
62 | if(this.opt.labels){ |
||
63 | _10=dojo.map(_f,function(x){
|
||
64 | return x>0?this._getLabel(x*100)+"%":""; |
||
65 | },this);
|
||
66 | } |
||
67 | }else{
|
||
68 | _e=df.map(run,"x ? Math.max(x.y, 0) : 0");
|
||
69 | if(df.every(_e,"<= 0")){ |
||
70 | return this; |
||
71 | } |
||
72 | _f=df.map(_e,"/this",df.foldl(_e,"+",0)); |
||
73 | if(this.opt.labels){ |
||
74 | _10=dojo.map(_f,function(x,i){
|
||
75 | if(x<=0){ |
||
76 | return ""; |
||
77 | } |
||
78 | var v=run[i];
|
||
79 | return "text" in v?v.text:this._getLabel(x*100)+"%"; |
||
80 | },this);
|
||
81 | } |
||
82 | } |
||
83 | var _14=df.map(run,function(v,i){ |
||
84 | if(v===null||typeof v=="number"){ |
||
85 | return t.next("slice",[this.opt,this.run],true); |
||
86 | } |
||
87 | return t.next("slice",[this.opt,this.run,v],true); |
||
88 | },this);
|
||
89 | if(this.opt.labels){ |
||
90 | _11=df.foldl1(df.map(_10,function(_15,i){
|
||
91 | var _16=_14[i].series.font;
|
||
92 | return dojox.gfx._base._getTextBox(_15,{font:_16}).w; |
||
93 | },this),"Math.max(a, b)")/2; |
||
94 | if(this.opt.labelOffset<0){ |
||
95 | r=Math.min(rx-2*_11,ry-_9)+this.opt.labelOffset; |
||
96 | } |
||
97 | _12=r-this.opt.labelOffset;
|
||
98 | } |
||
99 | if("radius" in this.opt){ |
||
100 | r=this.opt.radius;
|
||
101 | _12=r-this.opt.labelOffset;
|
||
102 | } |
||
103 | var _17={cx:_7.l+rx,cy:_7.t+ry,r:r}; |
||
104 | this.dyn=[];
|
||
105 | var _18=new Array(_f.length); |
||
106 | dojo.some(_f,function(_19,i){
|
||
107 | if(_19<=0){ |
||
108 | return false; |
||
109 | } |
||
110 | var v=run[i],_1a=_14[i],_1b;
|
||
111 | if(_19>=1){ |
||
112 | _1b=this._plotFill(_1a.series.fill,_6,_7);
|
||
113 | _1b=this._shapeFill(_1b,{x:_17.cx-_17.r,y:_17.cy-_17.r,width:2*_17.r,height:2*_17.r}); |
||
114 | _1b=this._pseudoRadialFill(_1b,{x:_17.cx,y:_17.cy},_17.r); |
||
115 | var _1c=s.createCircle(_17).setFill(_1b).setStroke(_1a.series.stroke);
|
||
116 | this.dyn.push({fill:_1b,stroke:_1a.series.stroke}); |
||
117 | if(_13){
|
||
118 | var o={element:"slice",index:i,run:this.run,shape:_1c,x:i,y:typeof v=="number"?v:v.y,cx:_17.cx,cy:_17.cy,cr:r}; |
||
119 | this._connectEvents(o);
|
||
120 | _18[i]=o; |
||
121 | } |
||
122 | return true; |
||
123 | } |
||
124 | var end=_c+_19*2*Math.PI; |
||
125 | if(i+1==_f.length){ |
||
126 | end=_b+2*Math.PI;
|
||
127 | } |
||
128 | var _1d=end-_c,x1=_17.cx+r*Math.cos(_c),y1=_17.cy+r*Math.sin(_c),x2=_17.cx+r*Math.cos(end),y2=_17.cy+r*Math.sin(end);
|
||
129 | var _1e=m._degToRad(this.opt.fanSize); |
||
130 | if(_1a.series.fill&&_1a.series.fill.type==="radial"&&this.opt.radGrad==="fan"&&_1d>_1e){ |
||
131 | var _1f=s.createGroup(),_20=Math.ceil(_1d/_1e),_21=_1d/_20;
|
||
132 | _1b=this._shapeFill(_1a.series.fill,{x:_17.cx-_17.r,y:_17.cy-_17.r,width:2*_17.r,height:2*_17.r}); |
||
133 | for(var j=0;j<_20;++j){ |
||
134 | var _22=j==0?x1:_17.cx+r*Math.cos(_c+(j-_1)*_21),_23=j==0?y1:_17.cy+r*Math.sin(_c+(j-_1)*_21),_24=j==_20-1?x2:_17.cx+r*Math.cos(_c+(j+1+_1)*_21),_25=j==_20-1?y2:_17.cy+r*Math.sin(_c+(j+1+_1)*_21),fan=_1f.createPath({}).moveTo(_17.cx,_17.cy).lineTo(_22,_23).arcTo(r,r,0,_21>Math.PI,true,_24,_25).lineTo(_17.cx,_17.cy).closePath().setFill(this._pseudoRadialFill(_1b,{x:_17.cx,y:_17.cy},r,_c+(j+0.5)*_21,_c+(j+0.5)*_21)); |
||
135 | } |
||
136 | _1f.createPath({}).moveTo(_17.cx,_17.cy).lineTo(x1,y1).arcTo(r,r,0,_1d>Math.PI,true,x2,y2).lineTo(_17.cx,_17.cy).closePath().setStroke(_1a.series.stroke); |
||
137 | _1c=_1f; |
||
138 | }else{
|
||
139 | _1c=s.createPath({}).moveTo(_17.cx,_17.cy).lineTo(x1,y1).arcTo(r,r,0,_1d>Math.PI,true,x2,y2).lineTo(_17.cx,_17.cy).closePath().setStroke(_1a.series.stroke); |
||
140 | var _1b=_1a.series.fill;
|
||
141 | if(_1b&&_1b.type==="radial"){ |
||
142 | _1b=this._shapeFill(_1b,{x:_17.cx-_17.r,y:_17.cy-_17.r,width:2*_17.r,height:2*_17.r}); |
||
143 | if(this.opt.radGrad==="linear"){ |
||
144 | _1b=this._pseudoRadialFill(_1b,{x:_17.cx,y:_17.cy},r,_c,end); |
||
145 | } |
||
146 | }else{
|
||
147 | if(_1b&&_1b.type==="linear"){ |
||
148 | _1b=this._plotFill(_1b,_6,_7);
|
||
149 | _1b=this._shapeFill(_1b,_1c.getBoundingBox());
|
||
150 | } |
||
151 | } |
||
152 | _1c.setFill(_1b); |
||
153 | } |
||
154 | this.dyn.push({fill:_1b,stroke:_1a.series.stroke}); |
||
155 | if(_13){
|
||
156 | var o={element:"slice",index:i,run:this.run,shape:_1c,x:i,y:typeof v=="number"?v:v.y,cx:_17.cx,cy:_17.cy,cr:r}; |
||
157 | this._connectEvents(o);
|
||
158 | _18[i]=o; |
||
159 | } |
||
160 | _c=end; |
||
161 | return false; |
||
162 | },this);
|
||
163 | if(this.opt.labels){ |
||
164 | _c=_b; |
||
165 | dojo.some(_f,function(_26,i){
|
||
166 | if(_26<=0){ |
||
167 | return false; |
||
168 | } |
||
169 | var _27=_14[i];
|
||
170 | if(_26>=1){ |
||
171 | var v=run[i],_28=da.createText[this.opt.htmlLabels&&dojox.gfx.renderer!="vml"?"html":"gfx"](this.chart,s,_17.cx,_17.cy+_9/2,"middle",_10[i],_27.series.font,_27.series.fontColor); |
||
172 | if(this.opt.htmlLabels){ |
||
173 | this.htmlElements.push(_28);
|
||
174 | } |
||
175 | return true; |
||
176 | } |
||
177 | var end=_c+_26*2*Math.PI,v=run[i]; |
||
178 | if(i+1==_f.length){ |
||
179 | end=_b+2*Math.PI;
|
||
180 | } |
||
181 | var _29=(_c+end)/2,x=_17.cx+_12*Math.cos(_29),y=_17.cy+_12*Math.sin(_29)+_9/2; |
||
182 | var _28=da.createText[this.opt.htmlLabels&&dojox.gfx.renderer!="vml"?"html":"gfx"](this.chart,s,x,y,"middle",_10[i],_27.series.font,_27.series.fontColor); |
||
183 | if(this.opt.htmlLabels){ |
||
184 | this.htmlElements.push(_28);
|
||
185 | } |
||
186 | _c=end; |
||
187 | return false; |
||
188 | },this);
|
||
189 | } |
||
190 | var esi=0; |
||
191 | this._eventSeries[this.run.name]=df.map(run,function(v){ |
||
192 | return v<=0?null:_18[esi++]; |
||
193 | }); |
||
194 | return this; |
||
195 | },_getLabel:function(_2a){ |
||
196 | return this.opt.fixed?dojo.number.format(_2a,{places:this.opt.precision}):_2a.toString(); |
||
197 | }}); |
||
198 | })(); |
||
199 | } |