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