1 if(!dojo._hasResource["dojox.charting.plot2d.Pie"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.charting.plot2d.Pie"] = true;
3 dojo.provide("dojox.charting.plot2d.Pie");
5 dojo.require("dojox.charting.Element");
6 dojo.require("dojox.charting.axis2d.common");
7 dojo.require("dojox.charting.plot2d.common");
9 dojo.require("dojox.lang.functional");
10 dojo.require("dojox.gfx");
13 var df = dojox.lang.functional, du = dojox.lang.utils,
14 dc = dojox.charting.plot2d.common,
15 da = dojox.charting.axis2d.common,
18 dojo.declare("dojox.charting.plot2d.Pie", dojox.charting.Element, {
25 labelStyle: "default", // default/rows/auto
26 htmlLabels: true // use HTML to draw labels
34 constructor: function(chart, kwArgs){
35 this.opt = dojo.clone(this.defaultParams);
36 du.updateWithObject(this.opt, kwArgs);
37 du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
46 setAxis: function(axis){
50 addSeries: function(run){
54 calculateAxes: function(dim){
58 getRequiredColors: function(){
59 return this.run ? this.run.data.length : 0;
61 render: function(dim, offsets){
62 if(!this.dirty){ return this; }
65 var s = this.group, color, t = this.chart.theme;
67 // calculate the geometry
68 var rx = (dim.width - offsets.l - offsets.r) / 2,
69 ry = (dim.height - offsets.t - offsets.b) / 2,
71 taFont = "font" in this.opt ? this.opt.font : t.axis.font,
72 size = taFont ? g.normalizedLength(g.splitFontString(taFont).size) : 0,
73 taFontColor = "fontColor" in this.opt ? this.opt.fontColor : t.axis.fontColor,
74 start = 0, step, sum, slices, labels, shift, labelR,
76 if(typeof run[0] == "number"){
77 sum = df.foldl1(run, "+");
78 slices = dojo.map(run, function(x){ return x / sum; });
80 labels = dojo.map(slices, function(x){
81 return this._getLabel(x * 100) + "%";
85 sum = df.foldl1(run, function(a, b){ return {y: a.y + b.y}; }).y;
86 slices = df.map(run, function(x){ return x.y / sum; });
88 labels = dojo.map(slices, function(x, i){
90 return "text" in v ? v.text : this._getLabel(x * 100) + "%";
95 shift = df.foldl1(df.map(labels, function(label){
96 return dojox.gfx._base._getTextBox(label, {font: taFont}).w;
97 }, this), "Math.max(a, b)") / 2;
98 if(this.opt.labelOffset < 0){
99 r = Math.min(rx - 2 * shift, ry - size) + this.opt.labelOffset;
101 labelR = r - this.opt.labelOffset;
103 if("radius" in this.opt){
105 labelR = r - this.opt.labelOffset;
114 if(!this.run || !run.length){
118 // need autogenerated color
119 color = new dojo.Color(t.next("color"));
120 var shape = s.createCircle(circle).
121 setFill(dc.augmentFill(t.run.fill, color)).
122 setStroke(dc.augmentStroke(t.series.stroke, color));
123 this.dyn.push({color: color, fill: shape.getFill(), stroke: shape.getStroke()});
126 var elem = da.createText[this.opt.htmlLabels && dojox.gfx.renderer != "vml" ? "html" : "gfx"]
127 (this.chart, s, circle.cx, circle.cy + size / 2, "middle",
128 "100%", taFont, taFontColor);
129 if(this.opt.htmlLabels){ this.htmlElements.push(elem); }
134 dojo.forEach(slices, function(x, i){
135 // calculate the geometry of the slice
136 var end = start + x * 2 * Math.PI, v = run[i];
137 if(i + 1 == slices.length){
140 var step = end - start,
141 x1 = circle.cx + r * Math.cos(start),
142 y1 = circle.cy + r * Math.sin(start),
143 x2 = circle.cx + r * Math.cos(end),
144 y2 = circle.cy + r * Math.sin(end);
146 var color, fill, stroke;
147 if(typeof v == "object"){
148 color = "color" in v ? v.color : new dojo.Color(t.next("color"));
149 fill = "fill" in v ? v.fill : dc.augmentFill(t.series.fill, color);
150 stroke = "stroke" in v ? v.stroke : dc.augmentStroke(t.series.stroke, color);
152 color = new dojo.Color(t.next("color"));
153 fill = dc.augmentFill(t.series.fill, color);
154 stroke = dc.augmentStroke(t.series.stroke, color);
156 var shape = s.createPath({}).
157 moveTo(circle.cx, circle.cy).
159 arcTo(r, r, 0, step > Math.PI, true, x2, y2).
160 lineTo(circle.cx, circle.cy).
164 this.dyn.push({color: color, fill: fill, stroke: stroke});
170 dojo.forEach(slices, function(slice, i){
171 // calculate the geometry of the slice
172 var end = start + slice * 2 * Math.PI, v = run[i];
173 if(i + 1 == slices.length){
176 var labelAngle = (start + end) / 2,
177 x = circle.cx + labelR * Math.cos(labelAngle),
178 y = circle.cy + labelR * Math.sin(labelAngle) + size / 2;
180 var elem = da.createText[this.opt.htmlLabels && dojox.gfx.renderer != "vml" ? "html" : "gfx"]
181 (this.chart, s, x, y, "middle",
183 (typeof v == "object" && "fontColor" in v)
184 ? v.fontColor : taFontColor);
185 if(this.opt.htmlLabels){ this.htmlElements.push(elem); }
193 _getLabel: function(number){
194 return this.opt.fixed ? number.toFixed(this.opt.precision) : number.toString();