NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / axis-time-base / axis-time-base.js
blob8d1cc8ff29423021df3288eb143d45c91087d3c1
1 /*
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/
6 */
8 YUI.add('axis-time-base', function (Y, NAME) {
10 /**
11  * Provides functionality for the handling of time axis data for a chart.
12  *
13  * @module charts
14  * @submodule axis-time-base
15  */
16 var Y_Lang = Y.Lang;
18 /**
19  * TimeImpl contains logic for time data. TimeImpl is used by the following classes:
20  * <ul>
21  *      <li>{{#crossLink "TimeAxisBase"}}{{/crossLink}}</li>
22  *      <li>{{#crossLink "TimeAxis"}}{{/crossLink}}</li>
23  *  </ul>
24  *
25  * @class TimeImpl
26  * @constructor
27  * @submodule axis-time-base
28  */
29 function TimeImpl()
33 TimeImpl.NAME = "timeImpl";
35 TimeImpl.ATTRS =
37     /**
38      * Method used for formatting a label. This attribute allows for the default label formatting method to overridden.
39      * The method use would need to implement the arguments below and return a `String` or an `HTMLElement`. The default
40      * implementation of the method returns a `String`. The output of this method will be rendered to the DOM using
41      * `appendChild`. If you override the `labelFunction` method and return an html string, you will also need to override
42      * the Axis' `appendLabelFunction` to accept html as a `String`.
43      * <dl>
44      *      <dt>val</dt><dd>Label to be formatted. (`String`)</dd>
45      *      <dt>format</dt><dd>STRFTime string used to format the label. (optional)</dd>
46      * </dl>
47      *
48      * @attribute labelFunction
49      * @type Function
50      */
52     /**
53      * Pattern used by the `labelFunction` to format a label.
54      *
55      * @attribute labelFormat
56      * @type String
57      */
58     labelFormat: {
59         value: "%b %d, %y"
60     }
63 TimeImpl.prototype = {
64     /**
65      * Type of data used in `Data`.
66      *
67      * @property _type
68      * @readOnly
69      * @private
70      */
71     _type: "time",
73     /**
74      * Getter method for maximum attribute.
75      *
76      * @method _maximumGetter
77      * @return Number
78      * @private
79      */
80     _maximumGetter: function ()
81     {
82         var max = this._getNumber(this._setMaximum);
83         if(!Y_Lang.isNumber(max))
84         {
85             max = this._getNumber(this.get("dataMaximum"));
86         }
87         return parseFloat(max);
88     },
90     /**
91      * Setter method for maximum attribute.
92      *
93      * @method _maximumSetter
94      * @param {Object} value
95      * @private
96      */
97     _maximumSetter: function (value)
98     {
99         this._setMaximum = this._getNumber(value);
100         return value;
101     },
103     /**
104      * Getter method for minimum attribute.
105      *
106      * @method _minimumGetter
107      * @return Number
108      * @private
109      */
110     _minimumGetter: function ()
111     {
112         var min = this._getNumber(this._setMinimum);
113         if(!Y_Lang.isNumber(min))
114         {
115             min = this._getNumber(this.get("dataMinimum"));
116         }
117         return parseFloat(min);
118     },
120     /**
121      * Setter method for minimum attribute.
122      *
123      * @method _minimumSetter
124      * @param {Object} value
125      * @private
126      */
127     _minimumSetter: function (value)
128     {
129         this._setMinimum = this._getNumber(value);
130         return value;
131     },
133     /**
134      * Indicates whether or not the maximum attribute has been explicitly set.
135      *
136      * @method _getSetMax
137      * @return Boolean
138      * @private
139      */
140     _getSetMax: function()
141     {
142         var max = this._getNumber(this._setMaximum);
143         return (Y_Lang.isNumber(max));
144     },
146     /**
147      * Indicates whether or not the minimum attribute has been explicitly set.
148      *
149      * @method _getSetMin
150      * @return Boolean
151      * @private
152      */
153     _getSetMin: function()
154     {
155         var min = this._getNumber(this._setMinimum);
156         return (Y_Lang.isNumber(min));
157     },
159     /**
160      * Formats a label based on the axis type and optionally specified format.
161      *
162      * @method formatLabel
163      * @param {Object} value
164      * @param {Object} format Pattern used to format the value.
165      * @return String
166      */
167     formatLabel: function(val, format)
168     {
169         val = Y.DataType.Date.parse(val);
170         if(format)
171         {
172             return Y.DataType.Date.format(val, {format:format});
173         }
174         return val;
175     },
177     /**
178      * Constant used to generate unique id.
179      *
180      * @property GUID
181      * @type String
182      * @private
183      */
184     GUID: "yuitimeaxis",
186     /**
187      * Type of data used in `Axis`.
188      *
189      * @property _dataType
190      * @readOnly
191      * @private
192      */
193     _dataType: "time",
195     /**
196      * Gets an array of values based on a key.
197      *
198      * @method _getKeyArray
199      * @param {String} key Value key associated with the data array.
200      * @param {Array} data Array in which the data resides.
201      * @return Array
202      * @private
203      */
204     _getKeyArray: function(key, data)
205     {
206         var obj,
207             keyArray = [],
208             i = 0,
209             val,
210             len = data.length;
211         for(; i < len; ++i)
212         {
213             obj = data[i][key];
214             if(Y_Lang.isDate(obj))
215             {
216                 val = obj.valueOf();
217             }
218             else
219             {
220                 val = new Date(obj);
221                 if(Y_Lang.isDate(val))
222                 {
223                     val = val.valueOf();
224                 }
225                 else if(!Y_Lang.isNumber(obj))
226                 {
227                     if(Y_Lang.isNumber(parseFloat(obj)))
228                     {
229                         val = parseFloat(obj);
230                     }
231                     else
232                     {
233                         if(typeof obj !== "string")
234                         {
235                             obj = obj;
236                         }
237                         val = new Date(obj).valueOf();
238                     }
239                 }
240                 else
241                 {
242                     val = obj;
243                 }
244             }
245             keyArray[i] = val;
246         }
247         return keyArray;
248     },
250     /**
251      * Calculates the maximum and minimum values for the `Axis`.
252      *
253      * @method _updateMinAndMax
254      * @private
255      */
256     _updateMinAndMax: function()
257     {
258         var data = this.get("data"),
259             max = 0,
260             min = 0,
261             len,
262             num,
263             i;
264         if(data && data.length && data.length > 0)
265         {
266             len = data.length;
267             max = min = data[0];
268             if(len > 1)
269             {
270                 for(i = 1; i < len; i++)
271                 {
272                     num = data[i];
273                     if(isNaN(num))
274                     {
275                         continue;
276                     }
277                     max = Math.max(num, max);
278                     min = Math.min(num, min);
279                 }
280             }
281         }
282         this._dataMaximum = max;
283         this._dataMinimum = min;
284     },
286     /**
287      * Returns a coordinate corresponding to a data values.
288      *
289      * @method _getCoordFromValue
290      * @param {Number} min The minimum for the axis.
291      * @param {Number} max The maximum for the axis.
292      * @param {length} length The distance that the axis spans.
293      * @param {Number} dataValue A value used to ascertain the coordinate.
294      * @param {Number} offset Value in which to offset the coordinates.
295      * @param {Boolean} reverse Indicates whether the coordinates should start from
296      * the end of an axis. Only used in the numeric implementation.
297      * @return Number
298      * @private
299      */
300     _getCoordFromValue: function(min, max, length, dataValue, offset)
301     {
302         var range,
303             multiplier,
304             valuecoord,
305             isNumber = Y_Lang.isNumber;
306             dataValue = this._getNumber(dataValue);
307         if(isNumber(dataValue))
308         {
309             range = max - min;
310             multiplier = length/range;
311             valuecoord = (dataValue - min) * multiplier;
312             valuecoord = offset + valuecoord;
313         }
314         else
315         {
316             valuecoord = NaN;
317         }
318         return valuecoord;
319     },
321     /**
322      * Parses value into a number.
323      *
324      * @method _getNumber
325      * @param val {Object} Value to parse into a number
326      * @return Number
327      * @private
328      */
329     _getNumber: function(val)
330     {
331         if(Y_Lang.isDate(val))
332         {
333             val = val.valueOf();
334         }
335         else if(!Y_Lang.isNumber(val) && val)
336         {
337             val = new Date(val).valueOf();
338         }
340         return val;
341     }
344 Y.TimeImpl = TimeImpl;
347  * TimeAxisBase manages time data for an axis.
349  * @class TimeAxisBase
350  * @extends AxisBase
351  * @uses TimeImpl
352  * @constructor
353  * @param {Object} config (optional) Configuration parameters.
354  * @submodule axis-time-base
355  */
356 Y.TimeAxisBase = Y.Base.create("timeAxisBase", Y.AxisBase, [Y.TimeImpl]);
359 }, '3.13.0', {"requires": ["axis-base"]});