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-numeric-base', function (Y, NAME) {
11 * Provides functionality for the handling of numeric axis data for a chart.
14 * @submodule axis-numeric-base
19 * NumericImpl contains logic for numeric data. NumericImpl is used by the following classes:
21 * <li>{{#crossLink "NumericAxisBase"}}{{/crossLink}}</li>
22 * <li>{{#crossLink "NumericAxis"}}{{/crossLink}}</li>
27 * @submodule axis-numeric-base
29 function NumericImpl()
33 NumericImpl.NAME = "numericImpl";
37 * Indicates whether 0 should always be displayed.
39 * @attribute alwaysShowZero
47 * Method used for formatting a label. This attribute allows for the default label formatting method to overridden.
48 * The method use would need to implement the arguments below and return a `String` or an `HTMLElement`. The default
49 * implementation of the method returns a `String`. The output of this method will be rendered to the DOM using
50 * `appendChild`. If you override the `labelFunction` method and return an html string, you will also need to override
51 * the Data' `appendLabelFunction` to accept html as a `String`.
53 * <dt>val</dt><dd>Label to be formatted. (`String`)</dd>
54 * <dt>format</dt><dd>Object containing properties used to format the label. (optional)</dd>
57 * @attribute labelFunction
62 * Object containing properties used by the `labelFunction` to format a
65 * @attribute labelFormat
71 thousandsSeparator: "",
79 *Indicates how to round unit values.
81 * <dt>niceNumber</dt><dd>Units will be smoothed based on the number of ticks and data range.</dd>
82 * <dt>auto</dt><dd>If the range is greater than 1, the units will be rounded.</dd>
83 * <dt>numeric value</dt><dd>Units will be equal to the numeric value.</dd>
84 * <dt>null</dt><dd>No rounding will occur.</dd>
87 * @attribute roundingMethod
96 * Indicates the scaling for the chart. The default value is `linear`. For a logarithmic axis, set the value
108 NumericImpl.prototype = {
110 * @method initializer
113 initializer: function() {
114 this.after("alwaysShowZeroChange", this._keyChangeHandler);
115 this.after("roundingMethodChange", this._keyChangeHandler);
116 this.after("scaleTypeChange", this._keyChangeHandler);
120 * Formats a label based on the axis type and optionally specified format.
123 * @param {Object} value
124 * @param {Object} format Pattern used to format the value.
127 formatLabel: function(val, format)
131 return Y.DataType.Number.format(val, format);
137 * Returns the sum of all values per key.
139 * @method getTotalByKey
140 * @param {String} key The identifier for the array whose values will be calculated.
143 getTotalByKey: function(key)
146 values = this.getDataByKey(key),
149 len = values ? values.length : 0;
152 val = parseFloat(values[i]);
162 * Returns the value corresponding to the origin on the axis.
167 getOrigin: function() {
169 min = this.get("minimum"),
170 max = this.get("maximum");
171 origin = Math.max(origin, min);
172 origin = Math.min(origin, max);
177 * Type of data used in `Data`.
186 * Helper method for getting a `roundingUnit` when calculating the minimum and maximum values.
188 * @method _getMinimumUnit
189 * @param {Number} max Maximum number
190 * @param {Number} min Minimum number
191 * @param {Number} units Number of units on the axis
195 _getMinimumUnit:function(max, min, units)
197 return this._getNiceNumber(Math.ceil((max - min)/units));
201 * Calculates a nice rounding unit based on the range.
203 * @method _getNiceNumber
204 * @param {Number} roundingUnit The calculated rounding unit.
208 _getNiceNumber: function(roundingUnit)
210 var tempMajorUnit = roundingUnit,
211 order = Math.ceil(Math.log(tempMajorUnit) * 0.4342944819032518),
212 roundedMajorUnit = Math.pow(10, order),
215 if (roundedMajorUnit / 2 >= tempMajorUnit)
217 roundedDiff = Math.floor((roundedMajorUnit / 2 - tempMajorUnit) / (Math.pow(10,order-1)/2));
218 tempMajorUnit = roundedMajorUnit/2 - roundedDiff*Math.pow(10,order-1)/2;
222 tempMajorUnit = roundedMajorUnit;
224 if(!isNaN(tempMajorUnit))
226 return tempMajorUnit;
233 * Calculates the maximum and minimum values for the `Data`.
235 * @method _updateMinAndMax
238 _updateMinAndMax: function()
240 var data = this.get("data"),
246 setMax = this.get("setMax"),
247 setMin = this.get("setMin");
248 if(!setMax || !setMin)
250 if(data && data.length && data.length > 0)
258 max = setMax ? this._setMaximum : max;
259 min = setMin ? this._setMinimum : min;
265 min = this._setMinimum;
267 else if(min === undefined)
273 min = Math.min(num, min);
277 max = this._setMaximum;
279 else if(max === undefined)
285 max = Math.max(num, max);
288 this._actualMaximum = max;
289 this._actualMinimum = min;
292 if(this.get("scaleType") !== "logarithmic")
294 this._roundMinAndMax(min, max, setMin, setMax);
298 this._dataMaximum = max;
299 this._dataMinimum = min;
305 * Rounds the mimimum and maximum values based on the `roundingUnit` attribute.
307 * @method _roundMinAndMax
308 * @param {Number} min Minimum value
309 * @param {Number} max Maximum value
312 _roundMinAndMax: function(min, max, setMin, setMax)
316 minGreaterThanZero = min >= 0,
317 maxGreaterThanZero = max > 0,
325 units = this.getTotalMajorUnits() - 1,
326 alwaysShowZero = this.get("alwaysShowZero"),
327 roundingMethod = this.get("roundingMethod"),
328 useIntegers = (max - min)/units >= 1;
331 if(roundingMethod === "niceNumber")
333 roundingUnit = this._getMinimumUnit(max, min, units);
334 if(minGreaterThanZero && maxGreaterThanZero)
336 if((alwaysShowZero || min < roundingUnit) && !setMin)
339 roundingUnit = this._getMinimumUnit(max, min, units);
343 min = this._roundDownToNearest(min, roundingUnit);
349 min = max - (roundingUnit * units);
354 max = min + (roundingUnit * units);
358 max = this._roundUpToNearest(max, roundingUnit);
361 else if(maxGreaterThanZero && !minGreaterThanZero)
365 topTicks = Math.round(units/((-1 * min)/max + 1));
366 topTicks = Math.max(Math.min(topTicks, units - 1), 1);
367 botTicks = units - topTicks;
368 tempMax = Math.ceil( max/topTicks );
369 tempMin = Math.floor( min/botTicks ) * -1;
373 while(tempMin < tempMax && botTicks >= 0)
377 tempMax = Math.ceil( max/topTicks );
378 tempMin = Math.floor( min/botTicks ) * -1;
380 //if there are any bottom ticks left calcualate the maximum by multiplying by the tempMin value
381 //if not, it's impossible to ensure that a zero is shown. skip it
384 max = tempMin * topTicks;
388 max = min + (roundingUnit * units);
393 while(tempMax < tempMin && topTicks >= 0)
397 tempMin = Math.floor( min/botTicks ) * -1;
398 tempMax = Math.ceil( max/topTicks );
400 //if there are any top ticks left calcualate the minimum by multiplying by the tempMax value
401 //if not, it's impossible to ensure that a zero is shown. skip it
404 min = tempMax * botTicks * -1;
408 min = max - (roundingUnit * units);
413 roundingUnit = Math.max(tempMax, tempMin);
414 roundingUnit = this._getNiceNumber(roundingUnit);
415 max = roundingUnit * topTicks;
416 min = roundingUnit * botTicks * -1;
423 min = max - (roundingUnit * units);
427 max = min + (roundingUnit * units);
431 min = this._roundDownToNearest(min, roundingUnit);
432 max = this._roundUpToNearest(max, roundingUnit);
446 max = min + (roundingUnit * units);
451 if(alwaysShowZero || max === 0 || max + roundingUnit > 0)
454 roundingUnit = this._getMinimumUnit(max, min, units);
455 min = max - (roundingUnit * units);
459 min = this._roundDownToNearest(min, roundingUnit);
460 max = this._roundUpToNearest(max, roundingUnit);
465 min = max - (roundingUnit * units);
469 else if(roundingMethod === "auto")
471 if(minGreaterThanZero && maxGreaterThanZero)
473 if((alwaysShowZero || min < (max-min)/units) && !setMin)
478 roundingUnit = (max - min)/units;
481 roundingUnit = Math.ceil(roundingUnit);
482 max = min + (roundingUnit * units);
486 max = min + Math.ceil(roundingUnit * units * 100000)/100000;
490 else if(maxGreaterThanZero && !minGreaterThanZero)
494 topTicks = Math.round( units / ( (-1 * min) /max + 1) );
495 topTicks = Math.max(Math.min(topTicks, units - 1), 1);
496 botTicks = units - topTicks;
500 tempMax = Math.ceil( max/topTicks );
501 tempMin = Math.floor( min/botTicks ) * -1;
502 roundingUnit = Math.max(tempMax, tempMin);
503 max = roundingUnit * topTicks;
504 min = roundingUnit * botTicks * -1;
508 tempMax = max/topTicks;
509 tempMin = min/botTicks * -1;
510 roundingUnit = Math.max(tempMax, tempMin);
511 max = Math.ceil(roundingUnit * topTicks * 100000)/100000;
512 min = Math.ceil(roundingUnit * botTicks * 100000)/100000 * -1;
517 roundingUnit = (max - min)/units;
520 roundingUnit = Math.ceil(roundingUnit);
522 min = Math.round(this._roundDownToNearest(min, roundingUnit) * 100000)/100000;
523 max = Math.round(this._roundUpToNearest(max, roundingUnit) * 100000)/100000;
528 roundingUnit = (max - min)/units;
531 roundingUnit = Math.ceil(roundingUnit);
533 if(alwaysShowZero || max === 0 || max + roundingUnit > 0)
536 roundingUnit = (max - min)/units;
539 Math.ceil(roundingUnit);
540 min = max - (roundingUnit * units);
544 min = max - Math.ceil(roundingUnit * units * 100000)/100000;
549 min = this._roundDownToNearest(min, roundingUnit);
550 max = this._roundUpToNearest(max, roundingUnit);
555 else if(!isNaN(roundingMethod) && isFinite(roundingMethod))
557 roundingUnit = roundingMethod;
558 minimumRange = roundingUnit * units;
559 dataRangeGreater = (max - min) > minimumRange;
560 minRound = this._roundDownToNearest(min, roundingUnit);
561 maxRound = this._roundUpToNearest(max, roundingUnit);
564 min = max - minimumRange;
568 max = min + minimumRange;
570 else if(minGreaterThanZero && maxGreaterThanZero)
572 if(alwaysShowZero || minRound <= 0)
580 max = min + minimumRange;
582 else if(maxGreaterThanZero && !minGreaterThanZero)
589 if(alwaysShowZero || maxRound >= 0)
597 min = max - minimumRange;
601 this._dataMaximum = max;
602 this._dataMinimum = min;
606 * Rounds a Number to the nearest multiple of an input. For example, by rounding
607 * 16 to the nearest 10, you will receive 20. Similar to the built-in function Math.round().
609 * @method _roundToNearest
610 * @param {Number} number Number to round
611 * @param {Number} nearest Multiple to round towards.
615 _roundToNearest: function(number, nearest)
617 nearest = nearest || 1;
618 var roundedNumber = Math.round(this._roundToPrecision(number / nearest, 10)) * nearest;
619 return this._roundToPrecision(roundedNumber, 10);
623 * Rounds a Number up to the nearest multiple of an input. For example, by rounding
624 * 16 up to the nearest 10, you will receive 20. Similar to the built-in function Math.ceil().
626 * @method _roundUpToNearest
627 * @param {Number} number Number to round
628 * @param {Number} nearest Multiple to round towards.
632 _roundUpToNearest: function(number, nearest)
634 nearest = nearest || 1;
635 return Math.ceil(this._roundToPrecision(number / nearest, 10)) * nearest;
639 * Rounds a Number down to the nearest multiple of an input. For example, by rounding
640 * 16 down to the nearest 10, you will receive 10. Similar to the built-in function Math.floor().
642 * @method _roundDownToNearest
643 * @param {Number} number Number to round
644 * @param {Number} nearest Multiple to round towards.
648 _roundDownToNearest: function(number, nearest)
650 nearest = nearest || 1;
651 return Math.floor(this._roundToPrecision(number / nearest, 10)) * nearest;
655 * Returns a coordinate corresponding to a data values.
657 * @method _getCoordFromValue
658 * @param {Number} min The minimum for the axis.
659 * @param {Number} max The maximum for the axis.
660 * @param {length} length The distance that the axis spans.
661 * @param {Number} dataValue A value used to ascertain the coordinate.
662 * @param {Number} offset Value in which to offset the coordinates.
663 * @param {Boolean} reverse Indicates whether the coordinates should start from
664 * the end of an axis. Only used in the numeric implementation.
668 _getCoordFromValue: function(min, max, length, dataValue, offset, reverse)
673 isNumber = Y_Lang.isNumber;
674 dataValue = parseFloat(dataValue);
675 if(isNumber(dataValue))
677 if(this.get("scaleType") === "logarithmic" && min > 0)
681 dataValue = Math.log(dataValue);
684 multiplier = length/range;
685 valuecoord = (dataValue - min) * multiplier;
686 valuecoord = reverse ? offset - valuecoord : offset + valuecoord;
696 * Rounds a number to a certain level of precision. Useful for limiting the number of
697 * decimal places on a fractional number.
699 * @method _roundToPrecision
700 * @param {Number} number Number to round
701 * @param {Number} precision Multiple to round towards.
705 _roundToPrecision: function(number, precision)
707 precision = precision || 0;
708 var decimalPlaces = Math.pow(10, precision);
709 return Math.round(decimalPlaces * number) / decimalPlaces;
713 Y.NumericImpl = NumericImpl;
716 * NumericAxisBase manages numeric data for an axis.
718 * @class NumericAxisBase
722 * @param {Object} config (optional) Configuration parameters.
723 * @submodule axis-numeric-base
725 Y.NumericAxisBase = Y.Base.create("numericAxisBase", Y.AxisBase, [Y.NumericImpl]);
728 }, '3.13.0', {"requires": ["axis-base"]});