|
525
|
1 |
/* |
|
|
2 |
YUI 3.10.3 (build 2fb5187) |
|
|
3 |
Copyright 2013 Yahoo! Inc. All rights reserved. |
|
|
4 |
Licensed under the BSD License. |
|
|
5 |
http://yuilibrary.com/license/ |
|
|
6 |
*/ |
|
|
7 |
|
|
|
8 |
YUI.add('axis-time-base', function (Y, NAME) { |
|
|
9 |
|
|
|
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 |
|
|
|
17 |
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() |
|
|
30 |
{ |
|
|
31 |
} |
|
|
32 |
|
|
|
33 |
TimeImpl.NAME = "timeImpl"; |
|
|
34 |
|
|
|
35 |
TimeImpl.ATTRS = |
|
|
36 |
{ |
|
|
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 |
*/ |
|
|
51 |
|
|
|
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 |
} |
|
|
61 |
}; |
|
|
62 |
|
|
|
63 |
TimeImpl.prototype = { |
|
|
64 |
/** |
|
|
65 |
* Type of data used in `Data`. |
|
|
66 |
* |
|
|
67 |
* @property _type |
|
|
68 |
* @readOnly |
|
|
69 |
* @private |
|
|
70 |
*/ |
|
|
71 |
_type: "time", |
|
|
72 |
|
|
|
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 |
}, |
|
|
89 |
|
|
|
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 |
}, |
|
|
102 |
|
|
|
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 |
}, |
|
|
119 |
|
|
|
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 |
}, |
|
|
132 |
|
|
|
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 |
}, |
|
|
145 |
|
|
|
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 |
}, |
|
|
158 |
|
|
|
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 |
}, |
|
|
176 |
|
|
|
177 |
/** |
|
|
178 |
* Constant used to generate unique id. |
|
|
179 |
* |
|
|
180 |
* @property GUID |
|
|
181 |
* @type String |
|
|
182 |
* @private |
|
|
183 |
*/ |
|
|
184 |
GUID: "yuitimeaxis", |
|
|
185 |
|
|
|
186 |
/** |
|
|
187 |
* Type of data used in `Axis`. |
|
|
188 |
* |
|
|
189 |
* @property _dataType |
|
|
190 |
* @readOnly |
|
|
191 |
* @private |
|
|
192 |
*/ |
|
|
193 |
_dataType: "time", |
|
|
194 |
|
|
|
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 |
}, |
|
|
249 |
|
|
|
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 |
}, |
|
|
285 |
|
|
|
286 |
/** |
|
|
287 |
* Parses value into a number. |
|
|
288 |
* |
|
|
289 |
* @method _getNumber |
|
|
290 |
* @param val {Object} Value to parse into a number |
|
|
291 |
* @return Number |
|
|
292 |
* @private |
|
|
293 |
*/ |
|
|
294 |
_getNumber: function(val) |
|
|
295 |
{ |
|
|
296 |
if(Y_Lang.isDate(val)) |
|
|
297 |
{ |
|
|
298 |
val = val.valueOf(); |
|
|
299 |
} |
|
|
300 |
else if(!Y_Lang.isNumber(val) && val) |
|
|
301 |
{ |
|
|
302 |
val = new Date(val).valueOf(); |
|
|
303 |
} |
|
|
304 |
|
|
|
305 |
return val; |
|
|
306 |
} |
|
|
307 |
}; |
|
|
308 |
|
|
|
309 |
Y.TimeImpl = TimeImpl; |
|
|
310 |
|
|
|
311 |
/** |
|
|
312 |
* TimeAxisBase manages time data for an axis. |
|
|
313 |
* |
|
|
314 |
* @class TimeAxisBase |
|
|
315 |
* @extends AxisBase |
|
|
316 |
* @uses TimeImpl |
|
|
317 |
* @constructor |
|
|
318 |
* @param {Object} config (optional) Configuration parameters. |
|
|
319 |
* @submodule axis-time-base |
|
|
320 |
*/ |
|
|
321 |
Y.TimeAxisBase = Y.Base.create("timeAxisBase", Y.AxisBase, [Y.TimeImpl]); |
|
|
322 |
|
|
|
323 |
|
|
|
324 |
}, '3.10.3', {"requires": ["axis-base"]}); |