2
YUI 3.13.0 (build 508226d)
3
Copyright 2013 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
8
YUI.add('series-pie', function (Y, NAME) {
11
* Provides functionality for creating a pie series.
14
* @submodule series-pie
17
* PieSeries visualizes data as a circular chart divided into wedges which represent data as a
18
* percentage of a whole.
24
* @param {Object} config (optional) Configuration parameters.
25
* @submodule series-pie
27
var CONFIG = Y.config,
28
DOCUMENT = CONFIG.doc,
29
_getClassName = Y.ClassNameManager.getClassName,
30
SERIES_MARKER = _getClassName("seriesmarker");
31
Y.PieSeries = Y.Base.create("pieSeries", Y.SeriesBase, [Y.Plots], {
33
* Image map used for interactivity when rendered with canvas.
42
* Image used for image map when rendered with canvas.
51
* Creates or updates the image map when rendered with canvas.
58
var id = "pieHotSpotMapi_" + Math.round(100000 * Math.random()),
59
graph = this.get("graph"),
65
cb = graph.get("contentBox");
69
graphic = this.get("graphic");
70
cb = graphic.get("node");
74
cb.removeChild(this._image);
75
while(this._areaNodes && this._areaNodes.length > 0)
77
areaNode = this._areaNodes.shift();
78
this._map.removeChild(areaNode);
80
cb.removeChild(this._map);
82
this._image = DOCUMENT.createElement("img");
83
this._image.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAABCAYAAAD9yd/wAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSB" +
84
"JbWFnZVJlYWR5ccllPAAAABJJREFUeNpiZGBgSGPAAgACDAAIkABoFyloZQAAAABJRU5ErkJggg==";
85
cb.appendChild(this._image);
86
this._image.style.position = "absolute";
87
this._image.style.left = "0px";
88
this._image.style.top = "0px";
89
this._image.setAttribute("usemap", "#" + id);
90
this._image.style.zIndex = 3;
91
this._image.style.opacity = 0;
92
this._image.setAttribute("alt", "imagemap");
93
this._map = DOCUMENT.createElement("map");
94
cb.appendChild(this._map);
95
this._map.setAttribute("name", id);
96
this._map.setAttribute("id", id);
101
* Storage for `categoryDisplayName` attribute.
103
* @property _categoryDisplayName
106
_categoryDisplayName: null,
109
* Storage for `valueDisplayName` attribute.
111
* @property _valueDisplayName
114
_valueDisplayName: null,
117
* Adds event listeners.
119
* @method addListeners
122
addListeners: function()
124
var categoryAxis = this.get("categoryAxis"),
125
valueAxis = this.get("valueAxis");
128
categoryAxis.after("dataReady", Y.bind(this._categoryDataChangeHandler, this));
129
categoryAxis.after("dataUpdate", Y.bind(this._categoryDataChangeHandler, this));
133
valueAxis.after("dataReady", Y.bind(this._valueDataChangeHandler, this));
134
valueAxis.after("dataUpdate", Y.bind(this._valueDataChangeHandler, this));
136
this.after("categoryAxisChange", this.categoryAxisChangeHandler);
137
this.after("valueAxisChange", this.valueAxisChangeHandler);
138
this.after("stylesChange", this._updateHandler);
139
this._visibleChangeHandle = this.after("visibleChange", this._handleVisibleChange);
151
this._renderered = true;
155
* Event handler for the categoryAxisChange event.
157
* @method _categoryAxisChangeHandler
158
* @param {Object} e Event object.
161
_categoryAxisChangeHandler: function()
163
var categoryAxis = this.get("categoryAxis");
164
categoryAxis.after("dataReady", Y.bind(this._categoryDataChangeHandler, this));
165
categoryAxis.after("dataUpdate", Y.bind(this._categoryDataChangeHandler, this));
169
* Event handler for the valueAxisChange event.
171
* @method _valueAxisChangeHandler
172
* @param {Object} e Event object.
175
_valueAxisChangeHandler: function()
177
var valueAxis = this.get("valueAxis");
178
valueAxis.after("dataReady", Y.bind(this._valueDataChangeHandler, this));
179
valueAxis.after("dataUpdate", Y.bind(this._valueDataChangeHandler, this));
183
* Constant used to generate unique id.
192
* Event handler for categoryDataChange event.
194
* @method _categoryDataChangeHandler
195
* @param {Object} event Event object.
198
_categoryDataChangeHandler: function()
200
if(this._rendered && this.get("categoryKey") && this.get("valueKey"))
207
* Event handler for valueDataChange event.
209
* @method _valueDataChangeHandler
210
* @param {Object} event Event object.
213
_valueDataChangeHandler: function()
215
if(this._rendered && this.get("categoryKey") && this.get("valueKey"))
222
* Returns the sum of all values for the series.
224
* @method getTotalValues
227
getTotalValues: function()
229
var total = this.get("valueAxis").getTotalByKey(this.get("valueKey"));
234
* Draws the series. Overrides the base implementation.
241
var w = this.get("width"),
242
h = this.get("height");
243
if(isFinite(w) && isFinite(h) && w > 0 && h > 0)
245
this._rendered = true;
248
this._callLater = true;
251
this._drawing = true;
252
this._callLater = false;
254
this._drawing = false;
261
this.fire("drawingComplete");
272
drawPlots: function()
274
var values = this.get("valueAxis").getDataByKey(this.get("valueKey")).concat(),
276
itemCount = values.length,
277
styles = this.get("styles").marker,
278
fillColors = styles.fill.colors,
279
fillAlphas = styles.fill.alphas || ["1"],
280
borderColors = styles.border.colors,
281
borderWeights = [styles.border.weight],
282
borderAlphas = [styles.border.alpha],
283
tbw = borderWeights.concat(),
284
tbc = borderColors.concat(),
285
tba = borderAlphas.concat(),
288
padding = styles.padding,
289
graphic = this.get("graphic"),
290
minDimension = Math.min(graphic.get("width"), graphic.get("height")),
291
w = minDimension - (padding.left + padding.right),
292
h = minDimension - (padding.top + padding.bottom),
296
radius = Math.min(halfWidth, halfHeight),
305
graphOrder = this.get("graphOrder") || 0,
306
isCanvas = Y.Graphic.NAME === "canvasGraphic";
307
for(; i < itemCount; ++i)
309
value = parseFloat(values[i]);
318
tfc = fillColors ? fillColors.concat() : null;
319
tfa = fillAlphas ? fillAlphas.concat() : null;
320
this._createMarkerCache();
324
this._image.width = w;
325
this._image.height = h;
327
for(i = 0; i < itemCount; i++)
332
angle = 360 / values.length;
336
angle = 360 * (value / totalValue);
338
if(tfc && tfc.length < 1)
340
tfc = fillColors.concat();
342
if(tfa && tfa.length < 1)
344
tfa = fillAlphas.concat();
346
if(tbw && tbw.length < 1)
348
tbw = borderWeights.concat();
350
if(tbw && tbc.length < 1)
352
tbc = borderColors.concat();
354
if(tba && tba.length < 1)
356
tba = borderAlphas.concat();
358
lw = tbw ? tbw.shift() : null;
359
lc = tbc ? tbc.shift() : null;
360
la = tba ? tba.shift() : null;
369
color:tfc ? tfc.shift() : this._getDefaultColor(i, "slice"),
370
alpha:tfa ? tfa.shift() : null
375
startAngle: startAngle,
381
marker = this.getMarker(wedgeStyle, graphOrder, i);
384
this._addHotspot(wedgeStyle, graphOrder, i);
387
this._clearMarkerCache();
393
* Method used by `styles` setter. Overrides base implementation.
396
* @param {Object} newStyles Hash of properties to update.
399
_setStyles: function(val)
405
val = this._parseMarkerStyles(val);
406
return Y.PieSeries.superclass._mergeStyles.apply(this, [val, this._getDefaultStyles()]);
410
* Adds an interactive map when rendering in canvas.
412
* @method _addHotspot
413
* @param {Object} cfg Object containing data used to draw the hotspot
414
* @param {Number} seriesIndex Index of series in the `seriesCollection`.
415
* @param {Number} index Index of the marker using the hotspot.
418
_addHotspot: function(cfg, seriesIndex, index)
420
var areaNode = DOCUMENT.createElement("area"),
425
startAngle = cfg.startAngle - arc,
426
endAngle = cfg.startAngle,
428
ax = x + Math.cos(startAngle / 180 * Math.PI) * radius,
429
ay = y + Math.sin(startAngle / 180 * Math.PI) * radius,
430
bx = x + Math.cos(endAngle / 180 * Math.PI) * radius,
431
by = y + Math.sin(endAngle / 180 * Math.PI) * radius,
432
numPoints = Math.floor(arc/10) - 1,
433
divAngle = (arc/(Math.floor(arc/10)) / 180) * Math.PI,
434
angleCoord = Math.atan((ay - y)/(ax - x)),
435
pts = x + ", " + y + ", " + ax + ", " + ay,
439
for(i = 1; i <= numPoints; ++i)
441
multDivAng = divAngle * i;
442
cosAng = Math.cos(angleCoord + multDivAng);
443
sinAng = Math.sin(angleCoord + multDivAng);
446
pts += ", " + (x + (radius * Math.cos(angleCoord + (divAngle * i))));
447
pts += ", " + (y + (radius * Math.sin(angleCoord + (divAngle * i))));
451
pts += ", " + (x - (radius * Math.cos(angleCoord + (divAngle * i))));
452
pts += ", " + (y - (radius * Math.sin(angleCoord + (divAngle * i))));
455
pts += ", " + bx + ", " + by;
456
pts += ", " + x + ", " + y;
457
this._map.appendChild(areaNode);
458
areaNode.setAttribute("class", SERIES_MARKER);
459
areaNode.setAttribute("id", "hotSpot_" + seriesIndex + "_" + index);
460
areaNode.setAttribute("shape", "polygon");
461
areaNode.setAttribute("coords", pts);
462
this._areaNodes.push(areaNode);
467
* Resizes and positions markers based on a mouse interaction.
469
* @method updateMarkerState
470
* @param {String} type state of the marker
471
* @param {Number} i index of the marker
474
updateMarkerState: function(type, i)
478
var state = this._getState(type),
481
marker = this._markers[i],
482
styles = this.get("styles").marker;
483
markerStyles = state === "off" || !styles[state] ? styles : styles[state];
484
indexStyles = this._mergeStyles(markerStyles, {});
485
indexStyles.fill.color = indexStyles.fill.colors[i % indexStyles.fill.colors.length];
486
indexStyles.fill.alpha = indexStyles.fill.alphas[i % indexStyles.fill.alphas.length];
487
marker.set(indexStyles);
492
* Creates a shape to be used as a marker.
494
* @method _createMarker
495
* @param {Object} styles Hash of style properties.
499
_createMarker: function(styles)
501
var graphic = this.get("graphic"),
503
cfg = Y.clone(styles);
504
marker = graphic.addShape(cfg);
505
marker.addClass(SERIES_MARKER);
510
* Creates a cache of markers for reuse.
512
* @method _createMarkerCache
515
_clearMarkerCache: function()
517
var len = this._markerCache.length,
522
marker = this._markerCache[i];
528
this._markerCache = [];
532
* Gets the default style values for the markers.
534
* @method _getPlotDefaults
538
_getPlotDefaults: function()
555
defs.fill.colors = this._defaultSliceColors;
556
defs.border.colors = this._defaultBorderColors;
562
* Read-only attribute indicating the type of series.
573
* Order of this instance of this `type`.
581
* Reference to the `Graph` in which the series is drawn into.
589
* Reference to the `Axis` instance used for assigning
590
* category values to the graph.
592
* @attribute categoryAxis
598
validator: function(value)
600
return value !== this.get("categoryAxis");
605
* Reference to the `Axis` instance used for assigning
606
* series values to the graph.
608
* @attribute categoryAxis
614
validator: function(value)
616
return value !== this.get("valueAxis");
621
* Indicates which array to from the hash of value arrays in
622
* the category `Axis` instance.
624
* @attribute categoryKey
630
validator: function(value)
632
return value !== this.get("categoryKey");
636
* Indicates which array to from the hash of value arrays in
637
* the value `Axis` instance.
639
* @attribute valueKey
645
validator: function(value)
647
return value !== this.get("valueKey");
652
* Name used for for displaying category data
654
* @attribute categoryDisplayName
657
categoryDisplayName: {
658
setter: function(val)
660
this._categoryDisplayName = val;
666
return this._categoryDisplayName || this.get("categoryKey");
671
* Name used for for displaying value data
673
* @attribute valueDisplayName
677
setter: function(val)
679
this._valueDisplayName = val;
685
return this._valueDisplayName || this.get("valueKey");
697
* Style properties used for drawing markers. This attribute is inherited from `MarkerSeries`. Below are the default
700
* <dt>fill</dt><dd>A hash containing the following values:
702
* <dt>colors</dt><dd>An array of colors to be used for the marker fills. The color for each marker is
703
* retrieved from the array below:<br/>
704
* `["#66007f", "#a86f41", "#295454", "#996ab2", "#e8cdb7", "#90bdbd","#000000","#c3b8ca", "#968373", "#678585"]`
706
* <dt>alphas</dt><dd>An array of alpha references (Number from 0 to 1) indicating the opacity of each marker
707
* fill. The default value is [1].</dd>
710
* <dt>border</dt><dd>A hash containing the following values:
712
* <dt>color</dt><dd>An array of colors to be used for the marker borders. The color for each marker is
713
* retrieved from the array below:<br/>
714
* `["#205096", "#b38206", "#000000", "#94001e", "#9d6fa0", "#e55b00", "#5e85c9", "#adab9e", "#6ac291", "#006457"]`
715
* <dt>alpha</dt><dd>Number from 0 to 1 indicating the opacity of the marker border. The default value is 1.</dd>
716
* <dt>weight</dt><dd>Number indicating the width of the border. The default value is 1.</dd>
719
* <dt>over</dt><dd>hash containing styles for markers when highlighted by a `mouseover` event. The default
720
* values for each style is null. When an over style is not set, the non-over value will be used. For example,
721
* the default value for `marker.over.fill.color` is equivalent to `marker.fill.color`.</dd>
731
}, '3.13.0', {"requires": ["series-base", "series-plot-util"]});