API Docs for: 3.10.3
Show:

File: charts/js/TimeImpl.js

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