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-plot-util', function (Y, NAME) {
11 * Provides functionality for drawing plots in a series.
14 * @submodule series-plot-util
17 _getClassName = Y.ClassNameManager.getClassName,
18 SERIES_MARKER = _getClassName("seriesmarker");
21 * Utility class used for drawing markers.
25 * @submodule series-plot-util
37 this.addAttrs(attrs, cfg);
42 * Storage for default marker styles.
44 * @property _plotDefaults
58 if(!this.get("xcoords") || this.get("xcoords").length < 1)
62 var isNumber = Y_Lang.isNumber,
63 style = Y.clone(this.get("styles").marker),
66 xcoords = this.get("xcoords"),
67 ycoords = this.get("ycoords"),
79 graphOrder = this.get("graphOrder"),
80 groupMarkers = this.get("groupMarkers");
87 xvalues.push(parseFloat(xcoords[i] - offsetWidth));
88 yvalues.push(parseFloat(ycoords[i] - offsetHeight));
90 this._createGroupMarker({
99 graphOrder: graphOrder,
104 if(Y_Lang.isArray(style.fill.color))
106 fillColors = style.fill.color.concat();
108 if(Y_Lang.isArray(style.border.color))
110 borderColors = style.border.color.concat();
112 this._createMarkerCache();
115 top = parseFloat(ycoords[i] - offsetHeight);
116 left = parseFloat(xcoords[i] - offsetWidth);
117 if(!isNumber(left) || !isNumber(top))
119 this._markers.push(null);
124 style.fill.color = fillColors[i % fillColors.length];
128 style.border.color = borderColors[i % borderColors.length];
133 marker = this.getMarker(style, graphOrder, i);
135 this._clearMarkerCache();
139 * Pre-defined group shapes.
141 * @property _groupShapes
145 circle: Y.CircleGroup,
147 ellipse: Y.EllipseGroup,
148 diamond: Y.DiamondGroup
152 * Returns the correct group shape class.
154 * @method _getGroupShape
155 * @param {Shape | String} shape Indicates which shape class.
159 _getGroupShape: function(shape)
161 if(Y_Lang.isString(shape))
163 shape = this._groupShapes[shape];
169 * Gets the default values for series that use the utility. This method is used by
170 * the class' `styles` attribute's getter to get build default values.
172 * @method _getPlotDefaults
176 _getPlotDefaults: function()
194 defs.fill.color = this._getDefaultColor(this.get("graphOrder"), "fill");
195 defs.border.color = this._getDefaultColor(this.get("graphOrder"), "border");
200 * Collection of markers to be used in the series.
209 * Collection of markers to be re-used on a series redraw.
211 * @property _markerCache
218 * Gets and styles a marker. If there is a marker in cache, it will use it. Otherwise
219 * it will create one.
222 * @param {Object} styles Hash of style properties.
223 * @param {Number} order Order of the series.
224 * @param {Number} index Index within the series associated with the marker.
228 getMarker: function(styles, order, index)
231 border = styles.border;
232 styles.id = this._getChart().get("id") + "_" + order + "_" + index;
233 //fix name differences between graphic layer
234 border.opacity = border.alpha;
235 styles.stroke = border;
236 styles.fill.opacity = styles.fill.alpha;
237 if(this._markerCache.length > 0)
241 if(this._markerCache.length < 1)
243 marker = this._createMarker(styles);
246 marker = this._markerCache.shift();
253 marker = this._createMarker(styles);
255 this._markers.push(marker);
260 * Creates a shape to be used as a marker.
262 * @method _createMarker
263 * @param {Object} styles Hash of style properties.
267 _createMarker: function(styles)
269 var graphic = this.get("graphic"),
271 cfg = Y.clone(styles);
272 cfg.type = cfg.shape;
273 marker = graphic.addShape(cfg);
274 marker.addClass(SERIES_MARKER);
279 * Creates a cache of markers for reuse.
281 * @method _createMarkerCache
284 _createMarkerCache: function()
286 if(this._groupMarker)
288 this._groupMarker.destroy();
289 this._groupMarker = null;
291 if(this._markers && this._markers.length > 0)
293 this._markerCache = this._markers.concat();
297 this._markerCache = [];
303 * Draws a series of markers in a single shape instance.
305 * @method _createGroupMarkers
306 * @param {Object} styles Set of configuration properties used to create the markers.
309 _createGroupMarker: function(styles)
312 markers = this.get("markers"),
313 border = styles.border,
317 if(markers && markers.length > 0)
319 while(markers.length > 0)
321 marker = markers.shift();
324 this.set("markers", []);
326 //fix name differences between graphic layer
327 border.opacity = border.alpha;
329 id: this._getChart().get("id") + "_" + styles.graphOrder,
332 dimensions: styles.dimensions,
333 xvalues: styles.xvalues,
334 yvalues: styles.yvalues
336 cfg.fill.opacity = styles.fill.alpha;
337 shape = this._getGroupShape(styles.shape);
342 if(styles.hasOwnProperty("radius") && !isNaN(styles.radius))
344 cfg.dimensions.radius = styles.radius;
346 if(this._groupMarker)
348 this._groupMarker.destroy();
350 graphic = this.get("graphic");
351 this._groupMarker = graphic.addShape(cfg);
358 * @method _toggleVisible
359 * @param {Boolean} visible indicates visibilitye
362 _toggleVisible: function(visible)
365 markers = this.get("markers"),
370 len = markers.length;
376 marker.set("visible", visible);
383 * Removes unused markers from the marker cache
385 * @method _clearMarkerCache
388 _clearMarkerCache: function()
391 while(this._markerCache.length > 0)
393 marker = this._markerCache.shift();
402 * Resizes and positions markers based on a mouse interaction.
404 * @method updateMarkerState
405 * @param {String} type state of the marker
406 * @param {Number} i index of the marker
409 updateMarkerState: function(type, i)
411 if(this._markers && this._markers[i])
415 styles = Y.clone(this.get("styles").marker),
416 state = this._getState(type),
417 xcoords = this.get("xcoords"),
418 ycoords = this.get("ycoords"),
419 marker = this._markers[i],
420 markerStyles = state === "off" || !styles[state] ? styles : styles[state];
421 markerStyles.fill.color = this._getItemColor(markerStyles.fill.color, i);
422 markerStyles.border.color = this._getItemColor(markerStyles.border.color, i);
423 markerStyles.stroke = markerStyles.border;
424 marker.set(markerStyles);
425 w = markerStyles.width;
426 h = markerStyles.height;
427 marker.set("x", (xcoords[i] - w/2));
428 marker.set("y", (ycoords[i] - h/2));
429 marker.set("visible", this.get("visible"));
434 * Parses a color from an array.
436 * @method _getItemColor
437 * @param {Array} val collection of colors
438 * @param {Number} i index of the item
442 _getItemColor: function(val, i)
444 if(Y_Lang.isArray(val))
446 return val[i % val.length];
452 * Method used by `styles` setter. Overrides base implementation.
455 * @param {Object} newStyles Hash of properties to update.
459 _setStyles: function(val)
461 val = this._parseMarkerStyles(val);
462 return Y.Renderer.prototype._setStyles.apply(this, [val]);
466 * Combines new styles with existing styles.
468 * @method _parseMarkerStyles
469 * @param {Object} Object containing style properties for the marker.
473 _parseMarkerStyles: function(val)
477 var defs = this._getPlotDefaults();
478 val.marker = this._mergeStyles(val.marker, defs);
481 val.marker.over = this._mergeStyles(val.marker.over, val.marker);
485 val.marker.down = this._mergeStyles(val.marker.down, val.marker);
492 * Returns marker state based on event type
495 * @param {String} type event type
499 _getState: function(type)
521 * @property _statSyles
534 drawSeries: function()
542 * Gets the default value for the `styles` attribute. Overrides
543 * base implementation.
545 * @method _getDefaultStyles
548 _getDefaultStyles: function()
550 var styles = this._mergeStyles({marker:this._getPlotDefaults()}, this.constructor.superclass._getDefaultStyles());
555 Y.augment(Plots, Y.Attribute);