Debian Codegraph @

dojox/charting/plot2d/Pie.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/array" ,"dojo/_base/declare", "dojo/dom-geometry", "dojo/_base/Color",
2 "./Base", "./_PlotEvents", "./common",
3 "dojox/gfx", "dojox/gfx/matrix", "dojox/lang/functional", "dojox/lang/utils","dojo/has"],
4 function(lang, arr, declare, domGeom, Color, Base, PlotEvents, dc, g, m, df, du, has){
5
6 /*=====
7 declare("dojox.charting.plot2d.__PieCtorArgs", dojox.charting.plot2d.__DefaultCtorArgs, {
8 // summary:
9 // Specialized keyword arguments object for use in defining parameters on a Pie chart.
10
11 // labels: Boolean?
12 // Whether or not to draw labels for each pie slice. Default is true.
13 labels: true,
14
15 // ticks: Boolean?
16 // Whether or not to draw ticks to labels within each slice. Default is false.
17 ticks: false,
18
19 // fixed: Boolean?
20 // Whether a fixed precision must be applied to data values for display. Default is true.
21 fixed: true,
22
23 // precision: Number?
24 // The precision at which to round data values for display. Default is 0.
25 precision: 1,
26
27 // labelOffset: Number?
28 // The amount in pixels by which to offset labels. Default is 20.
29 labelOffset: 20,
30
31 // labelStyle: String?
32 // Options as to where to draw labels. Values include "default", and "columns". Default is "default".
33 labelStyle: "default", // default/columns
34
35 // omitLabels: Boolean?
36 // Whether labels of slices small to the point of not being visible are omitted. Default false.
37 omitLabels: false,
38
39 // htmlLabels: Boolean?
40 // Whether or not to use HTML to render slice labels. Default is true.
41 htmlLabels: true,
42
43 // radGrad: String?
44 // The type of radial gradient to use in rendering. Default is "native".
45 radGrad: "native",
46
47 // fanSize: Number?
48 // The amount for a radial gradient. Default is 5.
49 fanSize: 5,
50
51 // startAngle: Number?
52 // Where to being rendering gradients in slices, in degrees. Default is 0.
53 startAngle: 0,
54
55 // radius: Number?
56 // The size of the radial gradient. Default is 0.
57 radius: 0,
58
59 // shadow: dojox.gfx.Stroke?
60 // An optional stroke to use to draw any shadows for a series on a plot.
61 shadow: {},
62
63 // fill: dojox.gfx.Fill?
64 // Any fill to be used for elements on the plot.
65 fill: {},
66
67 // filter: dojox.gfx.Filter?
68 // An SVG filter to be used for elements on the plot. gfx SVG renderer must be used and dojox/gfx/svgext must
69 // be required for this to work.
70 filter: {},
71
72 // styleFunc: Function?
73 // A function that returns a styling object for the a given data item.
74 styleFunc: null,
75
76 // innerRadius: Number?
77 // The inner radius of a ring in percent (0-100). If value < 0
78 // then it is assumed to be pixels, not percent.
79 innerRadius: 0,
80
81 // minWidth: Number?
82 // The minimum width of a pie slice at its chord. The default is 10px.
83 minWidth: 10
84
85 });
86 =====*/
87
88 var FUDGE_FACTOR = 0.2; // use to overlap fans
89
90 return declare("dojox.charting.plot2d.Pie", [Base, PlotEvents], {
91 // summary:
92 // The plot that represents a typical pie chart.
93 defaultParams: {
94 labels: true,
95 ticks: false,
96 fixed: true,
97 precision: 1,
98 labelOffset: 20,
99 labelStyle: "default", // default/columns
100 htmlLabels: true, // use HTML to draw labels
101 radGrad: "native", // or "linear", or "fan"
102 fanSize: 5, // maximum fan size in degrees
103 startAngle: 0, // start angle for slices in degrees
104 innerRadius: 0, // inner radius in pixels
105 minWidth: 0, // minimal width of degenerated slices
106 zeroDataMessage: "" // The message to display when there is no data, if provided by the user.
107 },
108 optionalParams: {
109 radius: 0,
110 omitLabels: false,
111 // theme components
112 stroke: {},
113 outline: {},
114 shadow: {},
115 fill: {},
116 filter: {},
117 styleFunc: null,
118 font: "",
119 fontColor: "",
120 labelWiring: {}
121 },
122
123 constructor: function(chart, kwArgs){
124 // summary:
125 // Create a pie plot.
126 this.opt = lang.clone(this.defaultParams);
127 du.updateWithObject(this.opt, kwArgs);
128 du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
129 this.axes = [];
130 this.run = null;
131 this.dyn = [];
132 this.runFilter = [];
133 if(kwArgs && kwArgs.hasOwnProperty("innerRadius")){
134 this._plotSetInnerRadius = true;
135 }
136 },
137 clear: function(){
138 // summary:
139 // Clear out all of the information tied to this plot.
140 // returns: dojox/charting/plot2d/Pie
141 // A reference to this plot for functional chaining.
142 this.inherited(arguments);
143 this.dyn = [];
144 this.run = null;
145 return this; // dojox/charting/plot2d/Pie
146 },
147 setAxis: function(axis){
148 // summary:
149 // Dummy method, since axes are irrelevant with a Pie chart.
150 // returns: dojox/charting/plot2d/Pie
151 // The reference to this plot for functional chaining.
152 return this; // dojox/charting/plot2d/Pie
153 },
154 addSeries: function(run){
155 // summary:
156 // Add a series of data to this plot.
157 // returns: dojox/charting/plot2d/Pie
158 // The reference to this plot for functional chaining.
159 this.run = run;
160 return this; // dojox/charting/plot2d/Pie
161 },
162 getSeriesStats: function(){
163 // summary:
164 // Returns default stats (irrelevant for this type of plot).
165 // returns: Object
166 // {hmin, hmax, vmin, vmax} min/max in both directions.
167 return lang.delegate(dc.defaultStats); // Object
168 },
169 getRequiredColors: function(){
170 // summary:
171 // Return the number of colors needed to draw this plot.
172 return this.run ? this.run.data.length : 0;
173 },
174
175 render: function(dim, offsets){
176 // summary:
177 // Render the plot on the chart.
178 // dim: Object
179 // An object of the form { width, height }.
180 // offsets: Object
181 // An object of the form { l, r, t, b }.
182 // returns: dojox/charting/plot2d/Pie
183 // A reference to this plot for functional chaining.
184 if(!this.dirty){ return this; }
185 this.resetEvents();
186 this.dirty = false;
187 this._eventSeries = {};
188 this.cleanGroup();
189 var s = this.group, t = this.chart.theme;
190
191 if(!this._plotSetInnerRadius && t && t.pieInnerRadius){
192 this.opt.innerRadius = t.pieInnerRadius;
193 }
194
195 // calculate the geometry
196 var rx = (dim.width - offsets.l - offsets.r) / 2,
197 ry = (dim.height - offsets.t - offsets.b) / 2,
198 r = Math.min(rx, ry),
199 taFont = "font" in this.opt ? this.opt.font : t.axis.tick.titleFont || "",
200 size = taFont ? g.normalizedLength(g.splitFontString(taFont).size) : 0,
201 taFontColor = this.opt.hasOwnProperty("fontColor") ? this.opt.fontColor : t.axis.tick.fontColor,
202 startAngle = m._degToRad(this.opt.startAngle),
203 start = startAngle, filteredRun, slices, labels, shift, labelR,
204 run = this.run.data,
205 events = this.events();
206
207 /* Added to handle no data case */
208 var noDataFunc = lang.hitch(this, function(){
209 var ct = t.clone();
210 var themes = df.map(run, function(v){
211 var tMixin = [this.opt, this.run];
212 if(v !== null && typeof v != "number"){
213 tMixin.push(v);
214 }
215 if(this.opt.styleFunc){
216 tMixin.push(this.opt.styleFunc(v));
217 }
218 return ct.next("slice", tMixin, true);
219 }, this);
220
221 // Draw initial pie, with text in it noting 0 data.
222 if("radius" in this.opt){
223 r = this.opt.radius < r ? this.opt.radius : r;
224 }
225
226 var circle = {
227 cx: offsets.l + rx,
228 cy: offsets.t + ry,
229 r: r
230 };
231 var rColor = new Color(taFontColor);
232 // If we have a radius, we'll need to fade the ring some
233 if(this.opt.innerRadius){
234 rColor.a = 0.1;
235 }
236 var ring = this._createRing(s, circle).setStroke(rColor);
237 if(this.opt.innerRadius){
238 // If we have a radius, fill it with the faded color.
239 ring.setFill(rColor);
240 }
241 if(this.opt.zeroDataMessage){
242 this.renderLabel(s, circle.cx, circle.cy + size/3, this.opt.zeroDataMessage, {
243 series: {
244 font: taFont,
245 fontColor: taFontColor
246 }
247 }, null, "middle");
248 }
249 this.dyn = [];
250 arr.forEach(run, function(item, i){
251 this.dyn.push({
252 fill: this._plotFill(themes[i].series.fill, dim, offsets),
253 stroke: themes[i].series.stroke});
254 }, this);
255 });
256 /* END Added to handle no data case */
257
258 // Draw over circle!
259 if(!this.run && !this.run.data.ength){
260 noDataFunc();
261 return this;
262 }
263 if(typeof run[0] == "number"){
264 filteredRun = df.map(run, "x ? Math.max(x, 0) : 0");
265 if(df.every(filteredRun, "<= 0")){
266 noDataFunc();
267 return this;
268 }
269 slices = df.map(filteredRun, "/this", df.foldl(filteredRun, "+", 0));
270 if(this.opt.labels){
271 labels = arr.map(slices, function(x){
272 return x > 0 ? this._getLabel(x * 100) + "%" : "";
273 }, this);
274 }
275 }else{
276 filteredRun = df.map(run, "x ? Math.max(x.y, 0) : 0");
277 if(!filteredRun.length || df.every(filteredRun, "<= 0")){
278 noDataFunc();
279 return this;
280 }
281 slices = df.map(filteredRun, "/this", df.foldl(filteredRun, "+", 0));
282 if(this.opt.labels){
283 labels = arr.map(slices, function(x, i){
284 if(x < 0){ return ""; }
285 var v = run[i];
286 return v.hasOwnProperty("text") ? v.text : this._getLabel(x * 100) + "%";
287 }, this);
288 }
289 }
290 var themes = df.map(run, function(v){
291 var tMixin = [this.opt, this.run];
292 if(v !== null && typeof v != "number"){
293 tMixin.push(v);
294 }
295 if(this.opt.styleFunc){
296 tMixin.push(this.opt.styleFunc(v));
297 }
298 return t.next("slice", tMixin, true);
299 }, this);
300
301 if(this.opt.labels) {
302 shift = df.foldl1(df.map(labels, function(label, i){
303 var font = themes[i].series.font;
304 return g._base._getTextBox(label, {font: font}).w;
305 }, this), "Math.max(a, b)") / 2;
306
307 if(this.opt.labelOffset < 0){
308 r = Math.min(rx - 2 * shift, ry - size) + this.opt.labelOffset;
309 }
310 }
311 if(this.opt.hasOwnProperty("radius")){
312 r = this.opt.radius < r * 0.9 ? this.opt.radius : r * 0.9;
313 }
314
315 if (!this.opt.radius && this.opt.labels && this.opt.labelStyle == "columns") {
316 r = r / 2;
317 if (rx > ry && rx > r * 2) {
318 r *= rx / (r * 2);
319 }
320 if (r >= ry * 0.8) {
321 r = ry * 0.8;
322 }
323 } else {
324 if (r >= ry * 0.9) {
325 r = ry * 0.9;
326 }
327 }
328
329 labelR = r - this.opt.labelOffset;
330
331 var circle = {
332 cx: offsets.l + rx,
333 cy: offsets.t + ry,
334 r: r
335 };
336
337 this.dyn = [];
338 // draw slices
339 var eventSeries = new Array(slices.length);
340
341 // Calulate primarily size for each slice
342 var slicesSteps = [], localStart = start;
343 var minWidth = this.opt.minWidth;
344 arr.forEach(slices, function(slice, i){
345 if(slice === 0){
346 slicesSteps[i] = {
347 step: 0,
348 end: localStart,
349 start: localStart,
350 weak: false
351 };
352 return;
353 }
354 var end = localStart + slice * 2 * Math.PI;
355 if(i === slices.length - 1){
356 end = startAngle + 2 * Math.PI;
357 }
358 var step = end - localStart,
359 dist = step * r;
360 slicesSteps[i] = {
361 step: step,
362 start: localStart,
363 end: end,
364 weak: dist < minWidth
365 };
366 localStart = end;
367 });
368
369 if(minWidth > 0){
370 var weakCount = 0, weakCoef = minWidth / r, oldWeakCoefSum = 0, i;
371 for(i = slicesSteps.length - 1; i >= 0; i--){
372 if(slicesSteps[i].weak){
373 ++weakCount;
374 oldWeakCoefSum += slicesSteps[i].step;
375 slicesSteps[i].step = weakCoef;
376 }
377 }
378 // make sure that our steps are small enough
379 var weakCoefSum = weakCount * weakCoef;
380 if(weakCoefSum > Math.PI){
381 weakCoef = Math.PI / weakCount;
382 for(i = 0; i < slicesSteps.length; ++i){
383 if(slicesSteps[i].weak){
384 slicesSteps[i].step = weakCoef;
385 }
386 }
387 weakCoefSum = Math.PI;
388 }
389 // now let's redistribute percentage
390 if(weakCount > 0){
391 weakCoef = 1 - (weakCoefSum - oldWeakCoefSum) / 2 / Math.PI;
392 for(i = 0; i < slicesSteps.length; ++i){
393 if(!slicesSteps[i].weak){
394 slicesSteps[i].step = weakCoef * slicesSteps[i].step;
395 }
396 }
397 }
398 // now let's update start and end values
399 for(i = 0; i < slicesSteps.length; ++i){
400 slicesSteps[i].start = i ? slicesSteps[i].end : localStart;
401 slicesSteps[i].end = slicesSteps[i].start + slicesSteps[i].step;
402 }
403 // let's make sure that our last end is exactly 2 * Math.PI
404 for(i = slicesSteps.length - 1; i >= 0; --i){
405 if(slicesSteps[i].step !== 0){
406 slicesSteps[i].end = localStart + 2 * Math.PI;
407 break;
408 }
409 }
410 }
411
412 localStart = start;
413 var o, specialFill;
414 arr.some(slices, function(slice, i){
415 var shape;
416 var v = run[i], theme = themes[i];
417
418 if(slice >= 1){
419 // whole pie
420 specialFill = this._plotFill(theme.series.fill, dim, offsets);
421 specialFill = this._shapeFill(specialFill,
422 {
423 x: circle.cx - circle.r, y: circle.cy - circle.r,
424 width: 2 * circle.r, height: 2 * circle.r
425 });
426 specialFill = this._pseudoRadialFill(specialFill, {x: circle.cx, y: circle.cy}, circle.r);
427 shape = this._createRing(s, circle).setFill(specialFill).setStroke(theme.series.stroke);
428 this.dyn.push({fill: specialFill, stroke: theme.series.stroke});
429
430 if(events){
431 o = {
432 element: "slice",
433 index: i,
434 run: this.run,
435 shape: shape,
436 x: i,
437 y: typeof v == "number" ? v : v.y,
438 cx: circle.cx,
439 cy: circle.cy,
440 cr: r
441 };
442 this._connectEvents(o);
443 eventSeries[i] = o;
444 }
445
446 var k;
447 for(k = i + 1; k < slices.length; k++){
448 theme = themes[k];
449 this.dyn.push({fill: theme.series.fill, stroke: theme.series.stroke});
450 }
451 return true; // stop iteration
452 }
453
454 if(slicesSteps[i].step === 0){
455 // degenerated slice
456 // But we still want a fill since this will be skipped and we need the fill
457 // for the label.
458 this.dyn.push({fill: theme.series.fill, stroke: theme.series.stroke});
459 return false; // continue
460 }
461
462 // calculate the geometry of the slice
463 var step = slicesSteps[i].step,
464 x1 = circle.cx + r * Math.cos(localStart),
465 y1 = circle.cy + r * Math.sin(localStart),
466 x2 = circle.cx + r * Math.cos(localStart + step),
467 y2 = circle.cy + r * Math.sin(localStart + step);
468 // draw the slice
469 var fanSize = m._degToRad(this.opt.fanSize), stroke;
470 if(theme.series.fill && theme.series.fill.type === "radial" && this.opt.radGrad === "fan" && step > fanSize){
471 var group = s.createGroup(), nfans = Math.ceil(step / fanSize), delta = step / nfans;
472 specialFill = this._shapeFill(theme.series.fill,
473 {x: circle.cx - circle.r, y: circle.cy - circle.r, width: 2 * circle.r, height: 2 * circle.r});
474 var j, alpha, beta, fansx, fansy, fanex, faney;
475 for(j = 0; j < nfans; ++j){
476 alpha = localStart + (j - FUDGE_FACTOR) * delta;
477 beta = localStart + (j + 1 + FUDGE_FACTOR) * delta;
478 fansx = j == 0 ? x1 : circle.cx + r * Math.cos(alpha);
479 fansy = j == 0 ? y1 : circle.cy + r * Math.sin(alpha);
480 fanex = j == nfans - 1 ? x2 : circle.cx + r * Math.cos(beta);
481 faney = j == nfans - 1 ? y2 : circle.cy + r * Math.sin(beta);
482 this._createSlice(group, circle, r, fansx, fansy, fanex, faney, alpha, delta).
483 setFill(this._pseudoRadialFill(specialFill, {x: circle.cx, y: circle.cy}, r,
484 localStart + (j + 0.5) * delta, localStart + (j + 0.5) * delta));
485 }
486 stroke = theme.series.stroke;
487 this._createSlice(group, circle, r, x1, y1, x2, y2, localStart, step).setStroke(stroke);
488 shape = group;
489 }else{
490 stroke = theme.series.stroke;
491
492 shape = this._createSlice(s, circle, r, x1, y1, x2, y2, localStart, step).setStroke(stroke);
493
494 specialFill = theme.series.fill;
495 if(specialFill && specialFill.type === "radial"){
496 specialFill = this._shapeFill(specialFill, {x: circle.cx - circle.r, y: circle.cy - circle.r, width: 2 * circle.r, height: 2 * circle.r});
497 if(this.opt.radGrad === "linear"){
498 specialFill = this._pseudoRadialFill(specialFill, {x: circle.cx, y: circle.cy}, r, localStart, localStart + step);
499 }
500 }else if(specialFill && specialFill.type === "linear"){
501 var bbox = lang.clone(shape.getBoundingBox());
502 if(g.renderer === "svg"){
503 // Try to fix the bounding box calculations for
504 // height. Only really works for SVG.
505 var pos = {w: 0, h: 0};
506 try{
507 pos = domGeom.position(shape.rawNode);
508 }catch(ignore){}
509 if(pos.h > bbox.height){
510 bbox.height = pos.h;
511 }
512 if(pos.w > bbox.width){
513 bbox.width = pos.w;
514 }
515 }
516 specialFill = this._plotFill(specialFill, dim, offsets);
517 specialFill = this._shapeFill(specialFill, bbox);
518 }
519 shape.setFill(specialFill);
520 }
521 this.dyn.push({fill: specialFill, stroke: theme.series.stroke});
522
523 if(events){
524 o = {
525 element: "slice",
526 index: i,
527 run: this.run,
528 shape: shape,
529 x: i,
530 y: typeof v == "number" ? v : v.y,
531 cx: circle.cx,
532 cy: circle.cy,
533 cr: r
534 };
535 this._connectEvents(o);
536 eventSeries[i] = o;
537 }
538
539 localStart = localStart + step;
540
541 return false; // continue
542 }, this);
543 // draw labels
544 if(this.opt.labels){
545 var isRtl = has("dojo-bidi") && this.chart.isRightToLeft();
546 if(this.opt.labelStyle == "default"){
547 start = startAngle;
548 localStart = start;
549 arr.some(slices, function(slice, i){
550 if(slice <= 0 && !this.opt.minWidth){
551 // degenerated slice
552 return false; // continue
553 }
554 var theme = themes[i];
555 if(slice >= 1){
556 // whole pie
557 this.renderLabel(s, circle.cx, circle.cy + size / 2, labels[i], theme, this.opt.labelOffset > 0);
558 return true; // stop iteration
559 }
560 // calculate the geometry of the slice
561 var end = start + slice * 2 * Math.PI;
562 if(i + 1 == slices.length){
563 end = startAngle + 2 * Math.PI;
564 }
565
566 if(this.opt.omitLabels && end-start < 0.001){
567 return false; // continue
568 }
569
570 var labelAngle = localStart + (slicesSteps[i].step / 2),//(start + end) / 2,
571 x = circle.cx + labelR * Math.cos(labelAngle),
572 y = circle.cy + labelR * Math.sin(labelAngle) + size / 2;
573 // draw the label
574 this.renderLabel(s, isRtl ? dim.width - x : x, y, labels[i], theme, this.opt.labelOffset > 0);
575 localStart += slicesSteps[i].step;
576 start = end;
577 return false; // continue
578 }, this);
579 }else if(this.opt.labelStyle == "columns"){
580 //calculate label angles
581 var omitLabels = this.opt.omitLabels;
582 start = startAngle;
583 localStart = start;
584 var labeledSlices = [],
585 significantCount = 0, k;
586 for(k = slices.length - 1; k >= 0; --k){
587 if(slices[k]){
588 ++significantCount;
589 }
590 }
591 arr.forEach(slices, function(slice, i){
592 var end = start + slice * 2 * Math.PI;
593 if(i + 1 == slices.length){
594 end = startAngle + 2 * Math.PI;
595 }
596 if(this.minWidth !== 0 || end - start >= 0.001){
597 // var labelAngle = (start + end) / 2;
598 var labelAngle = localStart + (slicesSteps[i].step / 2);//(start + end) / 2,
599 if(significantCount === 1 && !this.opt.minWidth){
600 labelAngle = (start + end) / 2;
601 }
602 labeledSlices.push({
603 angle: labelAngle,
604 left: Math.cos(labelAngle) < 0,
605 theme: themes[i],
606 index: i,
607 omit: omitLabels? end - start < 0.001:false
608 });
609 }
610 start = end;
611 localStart += slicesSteps[i].step;
612 }, this);
613
614 //calculate label radius to each slice
615 var labelHeight = g._base._getTextBox("a", {font:taFont, whiteSpace: "nowrap"}).h;
616 this._getProperLabelRadius(labeledSlices, labelHeight, circle.r * 1.1);
617
618 //draw label and wiring
619 var leftColumn = circle.cx - circle.r * 2,
620 rightColumn = circle.cx + circle.r * 2;
621 arr.forEach(labeledSlices, function(slice){
622 if(slice.omit){
623 return;
624 }
625 var cTheme = themes[slice.index], lrPadding = 0;
626 if(cTheme && cTheme.axis && cTheme.axis.tick && cTheme.axis.tick.labelGap){
627 // Try to pad the lable a bit, the same as a tick gap.
628 lrPadding = cTheme.axis.tick.labelGap;
629 }
630 var labelWidth = g._base._getTextBox(labels[slice.index],
631 {font: cTheme.series.font, whiteSpace: "nowrap", paddingLeft: lrPadding + "px"}).w,
632 x = circle.cx + slice.labelR * Math.cos(slice.angle),
633 y = circle.cy + slice.labelR * Math.sin(slice.angle),
634 jointX = (slice.left) ? (leftColumn + labelWidth) : (rightColumn - labelWidth),
635 labelX = (slice.left) ? leftColumn : jointX + lrPadding,
636 newRadius = circle.r,
637 wiring = s.createPath().moveTo(circle.cx + newRadius * Math.cos(slice.angle),
638 circle.cy + newRadius * Math.sin(slice.angle));
639 if(Math.abs(slice.labelR * Math.cos(slice.angle)) < circle.r * 2 - labelWidth){
640 wiring.lineTo(x, y);
641 }
642 wiring.lineTo(jointX, y).setStroke(slice.theme.series.labelWiring);
643 // Push the wiring to the back so that highlight/magnify actions don't bleed the wire.
644 wiring.moveToBack();
645 // Try to adjust the wiring position here. The browser always adds a bit
646 // of padding on height, so divide by 3 instead of 2.
647 var mid = labelHeight/3 + y;
648 var elem = this.renderLabel(s, labelX, mid || 0, labels[slice.index], cTheme, false, "left");
649
650 if(events && !this.opt.htmlLabels){
651 var fontWidth = g._base._getTextBox(labels[slice.index], {font: slice.theme.series.font}).w || 0,
652 fontHeight = g.normalizedLength(g.splitFontString(slice.theme.series.font).size);
653 o = {
654 element: "labels",
655 index: slice.index,
656 run: this.run,
657 shape: elem,
658 x: labelX,
659 y: y,
660 label: labels[slice.index]
661 };
662
663 var shp = elem.getShape(),
664 lt = domGeom.position(this.chart.node, true),
665 aroundRect = lang.mixin({ type : 'rect' }, {
666 x: shp.x,
667 y: shp.y - 2 * fontHeight
668 });
669
670 aroundRect.x += lt.x;
671 aroundRect.y += lt.y;
672 aroundRect.x = Math.round(aroundRect.x);
673 aroundRect.y = Math.round(aroundRect.y);
674 aroundRect.width = Math.ceil(fontWidth);
675 aroundRect.height = Math.ceil(fontHeight);
676
677 o.aroundRect = aroundRect;
678
679 this._connectEvents(o);
680 eventSeries[slices.length + slice.index] = o;
681 }
682 }, this);
683 }
684 }
685 // post-process events to restore the original indexing
686 var esi = 0;
687 this._eventSeries[this.run.name] = df.map(run, function(v){
688 return v <= 0 ? null : eventSeries[esi++];
689 });
690
691 // chart mirroring starts
692 if(has("dojo-bidi")){
693 this._checkOrientation(this.group, dim, offsets);
694 }
695
696 return this; // dojox/charting/plot2d/Pie
697 },
698
699 _getProperLabelRadius: function(slices, labelHeight, minRadius){
700 if(slices.length == 1){
701 slices[0].labelR = minRadius;
702 return;
703 }
704 var leftCenterSlice = {}, rightCenterSlice = {}, leftMinSIN = 2, rightMinSIN = 2, i;
705 var tempSIN;
706 for(i = 0; i < slices.length; ++i){
707 tempSIN = Math.abs(Math.sin(slices[i].angle));
708 if(slices[i].left){
709 if(leftMinSIN > tempSIN){
710 leftMinSIN = tempSIN;
711 leftCenterSlice = slices[i];
712 }
713 }else{
714 if(rightMinSIN > tempSIN){
715 rightMinSIN = tempSIN;
716 rightCenterSlice = slices[i];
717 }
718 }
719 }
720 leftCenterSlice.labelR = rightCenterSlice.labelR = minRadius;
721 this._caculateLabelR(leftCenterSlice, slices, labelHeight);
722 this._caculateLabelR(rightCenterSlice, slices, labelHeight);
723 },
724
725 _caculateLabelR: function(firstSlice, slices, labelHeight){
726 var i, j, k, length = slices.length, currentLabelR = firstSlice.labelR, nextLabelR,
727 step = slices[firstSlice.index].left ? -labelHeight : labelHeight;
728 for(k = 0, i = firstSlice.index, j = (i + 1) % length; k < length && slices[i].left === slices[j].left; ++k){
729 nextLabelR = (Math.sin(slices[i].angle) * currentLabelR + step) / Math.sin(slices[j].angle);
730 currentLabelR = Math.max(firstSlice.labelR, nextLabelR);
731 slices[j].labelR = currentLabelR;
732 i = (i + 1) % length;
733 j = (j + 1) % length;
734 }
735 if(k >= length){
736 slices[0].labelR = firstSlice.labelR;
737 }
738 for(k = 0, i = firstSlice.index, j = (i || length) - 1; k < length && slices[i].left === slices[j].left; ++k){
739 nextLabelR = (Math.sin(slices[i].angle) * currentLabelR - step) / Math.sin(slices[j].angle);
740 currentLabelR = Math.max(firstSlice.labelR, nextLabelR);
741 slices[j].labelR = currentLabelR;
742 i = (i || length) - 1;
743 j = (j || length) - 1;
744 }
745 },
746
747 _createRing: function(group, circle){
748 var r = this.opt.innerRadius;
749 if(r > 0){
750 // Percentage, use circle. Anything < 0 for innerRadius
751 // is assumed to be a multiple of the radius. So 0.25 innerRadius value
752 // is computed to be 25% of the outer radius.
753 r = circle.r * (r/100);
754 }else if(r < 0){
755 r = -r; // Assume it is pixels, fixed size hole.
756 }
757 if(r){
758 return group.createPath({}).setAbsoluteMode(true).
759 moveTo(circle.cx, circle.cy - circle.r).
760 arcTo(circle.r, circle.r, 0, false, true, circle.cx + circle.r, circle.cy).
761 arcTo(circle.r, circle.r, 0, true, true, circle.cx, circle.cy - circle.r).
762 closePath().
763 moveTo(circle.cx, circle.cy - r).
764 arcTo(r, r, 0, false, true, circle.cx + r, circle.cy).
765 arcTo(r, r, 0, true, true, circle.cx, circle.cy - r).
766 closePath();
767 }
768 return group.createCircle(circle);
769 },
770 _createSlice: function(group, circle, R, x1, y1, x2, y2, fromAngle, stepAngle){
771 var r = this.opt.innerRadius;
772 if(r > 0){
773 // Percentage, use circle. Anything < 0 for innerRadius
774 // is assumed to be a multiple of the radius. So 0.25 innerRadius value
775 // is computed to be 25% of the outer radius.
776 r = circle.r * (r/100);
777 }else if(r < 0){
778 r = -r; // Assume it is pixels, fixed size hole.
779 }
780 if(r){
781 var innerX1 = circle.cx + r * Math.cos(fromAngle),
782 innerY1 = circle.cy + r * Math.sin(fromAngle),
783 innerX2 = circle.cx + r * Math.cos(fromAngle + stepAngle),
784 innerY2 = circle.cy + r * Math.sin(fromAngle + stepAngle);
785 return group.createPath({}).setAbsoluteMode(true).
786 moveTo(innerX1, innerY1).
787 lineTo(x1, y1).
788 arcTo(R, R, 0, stepAngle > Math.PI, true, x2, y2).
789 lineTo(innerX2, innerY2).
790 arcTo(r, r, 0, stepAngle > Math.PI, false, innerX1, innerY1).
791 closePath();
792 }
793 return group.createPath({}).setAbsoluteMode(true).
794 moveTo(circle.cx, circle.cy).
795 lineTo(x1, y1).
796 arcTo(R, R, 0, stepAngle > Math.PI, true, x2, y2).
797 lineTo(circle.cx, circle.cy).
798 closePath();
799 }
800 });
801 });