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('axis-base', function (Y, NAME) {
11 * The Charts widget provides an api for displaying data
19 * Provides functionality for the handling of axis data in a chart.
22 * @submodule axis-base
27 * The Renderer class is a base class for chart components that use the `styles`
38 * Style properties for class
47 this._styles = this._styles || this._getDefaultStyles();
53 this._styles = this._setStyles(val);
58 * The graphic in which drawings will be rendered.
65 Renderer.NAME = "renderer";
67 Renderer.prototype = {
69 * Storage for `styles` attribute.
78 * Method used by `styles` setter.
81 * @param {Object} newStyles Hash of properties to update.
85 _setStyles: function(newstyles)
87 var styles = this.get("styles");
88 return this._mergeStyles(newstyles, styles);
92 * Merges to object literals so that only specified properties are
95 * @method _mergeStyles
96 * @param {Object} a Hash of new styles
97 * @param {Object} b Hash of original styles
101 _mergeStyles: function(a, b)
107 var newstyles = Y.merge(b, {});
108 Y.Object.each(a, function(value, key)
110 if(b.hasOwnProperty(key) && Y_Lang.isObject(value) && !Y_Lang.isFunction(value) && !Y_Lang.isArray(value))
112 newstyles[key] = this._mergeStyles(value, b[key]);
116 newstyles[key] = value;
123 * Gets the default value for the `styles` attribute.
125 * @method _getDefaultStyles
129 _getDefaultStyles: function()
140 Y.augment(Renderer, Y.Attribute);
141 Y.Renderer = Renderer;
144 * The axis-base submodule contains functionality for the handling of axis data in a chart.
147 * @submodule axis-base
150 * An abstract class that provides the core functionality used by the following classes:
152 * <li>{{#crossLink "CategoryAxisBase"}}{{/crossLink}}</li>
153 * <li>{{#crossLink "NumericAxisBase"}}{{/crossLink}}</li>
154 * <li>{{#crossLink "StackedAxisBase"}}{{/crossLink}}</li>
155 * <li>{{#crossLink "TimeAxisBase"}}{{/crossLink}}</li>
156 * <li>{{#crossLink "CategoryAxis"}}{{/crossLink}}</li>
157 * <li>{{#crossLink "NumericAxis"}}{{/crossLink}}</li>
158 * <li>{{#crossLink "StackedAxis"}}{{/crossLink}}</li>
159 * <li>{{#crossLink "TimeAxis"}}{{/crossLink}}</li>
166 * @param {Object} config (optional) Configuration parameters.
167 * @submodule axis-base
169 Y.AxisBase = Y.Base.create("axisBase", Y.Base, [Y.Renderer], {
171 * @method initializer
174 initializer: function()
176 this.after("minimumChange", Y.bind(this._keyChangeHandler, this));
177 this.after("maximumChange", Y.bind(this._keyChangeHandler, this));
178 this.after("keysChange", this._keyChangeHandler);
179 this.after("dataProviderChange", this._dataProviderChangeHandler);
183 * Returns the value corresponding to the origin on the axis.
188 getOrigin: function() {
189 return this.get("minimum");
193 * Handles changes to `dataProvider`.
195 * @method _dataProviderChangeHandler
196 * @param {Object} e Event object.
199 _dataProviderChangeHandler: function()
201 var keyCollection = this.get("keyCollection").concat(),
202 keys = this.get("keys"),
208 if(keys.hasOwnProperty(i))
214 if(keyCollection && keyCollection.length)
216 this.set("keys", keyCollection);
221 * Calculates the maximum and minimum values for the `Data`.
223 * @method _updateMinAndMax
226 _updateMinAndMax: function() {
230 * Constant used to generate unique id.
239 * Type of data used in `Axis`.
249 * Storage for `setMaximum` attribute.
251 * @property _setMaximum
258 * Storage for `setMinimum` attribute.
260 * @property _setMinimum
267 * Reference to data array.
276 * Indicates whether the all data is up to date.
278 * @property _updateTotalDataFlag
282 _updateTotalDataFlag: true,
285 * Storage for `dataReady` attribute.
287 * @property _dataReady
295 * Adds an array to the key hash.
298 * @param value Indicates what key to use in retrieving
301 addKey: function (value)
303 this.set("keys", value);
307 * Gets an array of values based on a key.
309 * @method _getKeyArray
310 * @param {String} key Value key associated with the data array.
311 * @param {Array} data Array in which the data resides.
315 _getKeyArray: function(key, data)
324 keyArray[i] = obj[key];
330 * Updates the total data array.
332 * @method _updateTotalData
335 _updateTotalData: function()
337 var keys = this.get("keys"),
342 if(keys.hasOwnProperty(i))
344 this._data = this._data.concat(keys[i]);
347 this._updateTotalDataFlag = false;
351 * Removes an array from the key hash.
354 * @param {String} value Indicates what key to use in removing from
357 removeKey: function(value)
359 var keys = this.get("keys");
360 if(keys.hasOwnProperty(value))
363 this._keyChangeHandler();
368 * Returns a value based of a key value and an index.
370 * @method getKeyValueAt
371 * @param {String} key value used to look up the correct array
372 * @param {Number} index within the array
375 getKeyValueAt: function(key, index)
378 keys = this.get("keys");
379 if(keys[key] && Y_Lang.isNumber(parseFloat(keys[key][index])))
381 value = keys[key][index];
383 return parseFloat(value);
387 * Returns values based on key identifiers. When a string is passed as an argument, an array of values is returned.
388 * When an array of keys is passed as an argument, an object literal with an array of values mapped to each key is
391 * @method getDataByKey
392 * @param {String|Array} value value used to identify the array
393 * @return Array|Object
395 getDataByKey: function (value)
401 keys = this.get("keys");
402 if(Y_Lang.isArray(value))
406 for(i = 0; i < len; i = i + 1)
411 obj[key] = this.getDataByKey(key);
427 * Returns the total number of majorUnits that will appear on an axis.
429 * @method getTotalMajorUnits
432 getTotalMajorUnits: function()
435 majorUnit = this.get("styles").majorUnit;
436 units = majorUnit.count;
441 * Gets the distance that the first and last ticks are offset from there respective
444 * @method getEdgeOffset
445 * @param {Number} ct Number of ticks on the axis.
446 * @param {Number} l Length (in pixels) of the axis.
449 getEdgeOffset: function(ct, l)
452 if(this.get("calculateEdgeOffset")) {
453 edgeOffset = (l/ct)/2;
461 * Updates the `Axis` after a change in keys.
463 * @method _keyChangeHandler
464 * @param {Object} e Event object.
467 _keyChangeHandler: function()
469 this._updateMinAndMax();
470 this._updateTotalDataFlag = true;
471 this.fire("dataUpdate");
475 * Gets the default value for the `styles` attribute. Overrides
476 * base implementation.
478 * @method _getDefaultStyles
482 _getDefaultStyles: function()
495 * Getter method for maximum attribute.
497 * @method _maximumGetter
501 _maximumGetter: function ()
503 var max = this.get("dataMaximum"),
504 min = this.get("minimum");
505 //If all values are zero, force a range so that the Axis and related series
507 if(min === 0 && max === 0)
511 if(Y_Lang.isNumber(this._setMaximum))
513 max = this._setMaximum;
515 return parseFloat(max);
519 * Setter method for maximum attribute.
521 * @method _maximumSetter
522 * @param {Object} value
525 _maximumSetter: function (value)
527 this._setMaximum = parseFloat(value);
532 * Getter method for minimum attribute.
534 * @method _minimumGetter
538 _minimumGetter: function ()
540 var min = this.get("dataMinimum");
541 if(Y_Lang.isNumber(this._setMinimum))
543 min = this._setMinimum;
545 return parseFloat(min);
549 * Setter method for minimum attribute.
551 * @method _minimumSetter
552 * @param {Object} value
555 _minimumSetter: function(val)
557 this._setMinimum = parseFloat(val);
562 * Indicates whether or not the maximum attribute has been explicitly set.
568 _getSetMax: function()
570 return Y_Lang.isNumber(this._setMaximum);
575 * Returns and array of coordinates corresponding to an array of data values.
577 * @method _getCoordsFromValues
578 * @param {Number} min The minimum for the axis.
579 * @param {Number} max The maximum for the axis.
580 * @param {length} length The distance that the axis spans.
581 * @param {Array} dataValues An array of values.
582 * @param {Number} offset Value in which to offset the coordinates.
583 * @param {Boolean} reverse Indicates whether the coordinates should start from
584 * the end of an axis. Only used in the numeric implementation.
588 _getCoordsFromValues: function(min, max, length, dataValues, offset, reverse)
592 len = dataValues.length;
593 for(i = 0; i < len; i = i + 1)
595 valuecoords.push(this._getCoordFromValue.apply(this, [min, max, length, dataValues[i], offset, reverse]));
601 * Returns and array of data values based on the axis' range and number of values.
603 * @method _getDataValuesByCount
604 * @param {Number} count The number of values to be used.
605 * @param {Number} min The minimum value of the axis.
606 * @param {Number} max The maximum value of the axis.
610 _getDataValuesByCount: function(count, min, max)
618 for(i = 0; i < len; i = i + 1)
620 dataValues.push(dataValue);
621 dataValue = dataValue + increm;
623 dataValues.push(max);
628 * Indicates whether or not the minimum attribute has been explicitly set.
634 _getSetMin: function()
636 return Y_Lang.isNumber(this._setMinimum);
641 * Determines whether and offset is automatically calculated for the edges of the axis.
643 * @attribute calculateEdgeOffset
646 calculateEdgeOffset: {
651 valueFn: function() {
652 return this.formatLabel;
657 * Hash of array identifed by a string value.
665 setter: function(val)
670 data = this.get("dataProvider");
671 if(Y_Lang.isArray(val))
674 for(i = 0; i < len; ++i)
676 keys[val[i]] = this._getKeyArray(val[i], data);
680 else if(Y_Lang.isString(val))
682 keys = this.get("keys");
683 keys[val] = this._getKeyArray(val, data);
689 if(val.hasOwnProperty(i))
691 keys[i] = this._getKeyArray(i, data);
695 this._updateTotalDataFlag = true;
701 *Returns the type of axis data
703 * <dt>time</dt><dd>Manages time data</dd>
704 * <dt>stacked</dt><dd>Manages stacked numeric data</dd>
705 * <dt>numeric</dt><dd>Manages numeric data</dd>
706 * <dt>category</dt><dd>Manages categorical data</dd>
723 * Instance of `ChartDataProvider` that the class uses
724 * to build its own data.
726 * @attribute dataProvider
730 setter: function (value)
737 * The maximum value contained in the `data` array. Used for
738 * `maximum` when `autoMax` is true.
740 * @attribute dataMaximum
746 if(!Y_Lang.isNumber(this._dataMaximum))
748 this._updateMinAndMax();
750 return this._dataMaximum;
755 * The maximum value that will appear on an axis.
763 getter: "_maximumGetter",
765 setter: "_maximumSetter"
769 * The minimum value contained in the `data` array. Used for
770 * `minimum` when `autoMin` is true.
772 * @attribute dataMinimum
778 if(!Y_Lang.isNumber(this._dataMinimum))
780 this._updateMinAndMax();
782 return this._dataMinimum;
787 * The minimum value that will appear on an axis.
795 getter: "_minimumGetter",
797 setter: "_minimumSetter"
801 * Determines whether the maximum is calculated or explicitly
814 * Determines whether the minimum is calculated or explicitly
835 if(!this._data || this._updateTotalDataFlag)
837 this._updateTotalData();
844 * Array containing all the keys in the axis.
846 * @attribute keyCollection
852 var keys = this.get("keys"),
857 if(keys.hasOwnProperty(i))
868 * Object which should have by the labelFunction
870 * @attribute labelFunctionScope
873 labelFunctionScope: {}
878 }, '3.13.0', {"requires": ["classnamemanager", "datatype-number", "datatype-date", "base", "event-custom"]});