Time.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var zrUtil = require("zrender/lib/core/util");
  20. var numberUtil = require("../util/number");
  21. var formatUtil = require("../util/format");
  22. var scaleHelper = require("./helper");
  23. var IntervalScale = require("./Interval");
  24. /*
  25. * Licensed to the Apache Software Foundation (ASF) under one
  26. * or more contributor license agreements. See the NOTICE file
  27. * distributed with this work for additional information
  28. * regarding copyright ownership. The ASF licenses this file
  29. * to you under the Apache License, Version 2.0 (the
  30. * "License"); you may not use this file except in compliance
  31. * with the License. You may obtain a copy of the License at
  32. *
  33. * http://www.apache.org/licenses/LICENSE-2.0
  34. *
  35. * Unless required by applicable law or agreed to in writing,
  36. * software distributed under the License is distributed on an
  37. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  38. * KIND, either express or implied. See the License for the
  39. * specific language governing permissions and limitations
  40. * under the License.
  41. */
  42. /*
  43. * A third-party license is embeded for some of the code in this file:
  44. * The "scaleLevels" was originally copied from "d3.js" with some
  45. * modifications made for this project.
  46. * (See more details in the comment on the definition of "scaleLevels" below.)
  47. * The use of the source code of this file is also subject to the terms
  48. * and consitions of the license of "d3.js" (BSD-3Clause, see
  49. * </licenses/LICENSE-d3>).
  50. */
  51. // [About UTC and local time zone]:
  52. // In most cases, `number.parseDate` will treat input data string as local time
  53. // (except time zone is specified in time string). And `format.formateTime` returns
  54. // local time by default. option.useUTC is false by default. This design have
  55. // concidered these common case:
  56. // (1) Time that is persistent in server is in UTC, but it is needed to be diplayed
  57. // in local time by default.
  58. // (2) By default, the input data string (e.g., '2011-01-02') should be displayed
  59. // as its original time, without any time difference.
  60. var intervalScaleProto = IntervalScale.prototype;
  61. var mathCeil = Math.ceil;
  62. var mathFloor = Math.floor;
  63. var ONE_SECOND = 1000;
  64. var ONE_MINUTE = ONE_SECOND * 60;
  65. var ONE_HOUR = ONE_MINUTE * 60;
  66. var ONE_DAY = ONE_HOUR * 24; // FIXME 公用?
  67. var bisect = function (a, x, lo, hi) {
  68. while (lo < hi) {
  69. var mid = lo + hi >>> 1;
  70. if (a[mid][1] < x) {
  71. lo = mid + 1;
  72. } else {
  73. hi = mid;
  74. }
  75. }
  76. return lo;
  77. };
  78. /**
  79. * @alias module:echarts/coord/scale/Time
  80. * @constructor
  81. */
  82. var TimeScale = IntervalScale.extend({
  83. type: 'time',
  84. /**
  85. * @override
  86. */
  87. getLabel: function (val) {
  88. var stepLvl = this._stepLvl;
  89. var date = new Date(val);
  90. return formatUtil.formatTime(stepLvl[0], date, this.getSetting('useUTC'));
  91. },
  92. /**
  93. * @override
  94. */
  95. niceExtent: function (opt) {
  96. var extent = this._extent; // If extent start and end are same, expand them
  97. if (extent[0] === extent[1]) {
  98. // Expand extent
  99. extent[0] -= ONE_DAY;
  100. extent[1] += ONE_DAY;
  101. } // If there are no data and extent are [Infinity, -Infinity]
  102. if (extent[1] === -Infinity && extent[0] === Infinity) {
  103. var d = new Date();
  104. extent[1] = +new Date(d.getFullYear(), d.getMonth(), d.getDate());
  105. extent[0] = extent[1] - ONE_DAY;
  106. }
  107. this.niceTicks(opt.splitNumber, opt.minInterval, opt.maxInterval); // var extent = this._extent;
  108. var interval = this._interval;
  109. if (!opt.fixMin) {
  110. extent[0] = numberUtil.round(mathFloor(extent[0] / interval) * interval);
  111. }
  112. if (!opt.fixMax) {
  113. extent[1] = numberUtil.round(mathCeil(extent[1] / interval) * interval);
  114. }
  115. },
  116. /**
  117. * @override
  118. */
  119. niceTicks: function (approxTickNum, minInterval, maxInterval) {
  120. approxTickNum = approxTickNum || 10;
  121. var extent = this._extent;
  122. var span = extent[1] - extent[0];
  123. var approxInterval = span / approxTickNum;
  124. if (minInterval != null && approxInterval < minInterval) {
  125. approxInterval = minInterval;
  126. }
  127. if (maxInterval != null && approxInterval > maxInterval) {
  128. approxInterval = maxInterval;
  129. }
  130. var scaleLevelsLen = scaleLevels.length;
  131. var idx = bisect(scaleLevels, approxInterval, 0, scaleLevelsLen);
  132. var level = scaleLevels[Math.min(idx, scaleLevelsLen - 1)];
  133. var interval = level[1]; // Same with interval scale if span is much larger than 1 year
  134. if (level[0] === 'year') {
  135. var yearSpan = span / interval; // From "Nice Numbers for Graph Labels" of Graphic Gems
  136. // var niceYearSpan = numberUtil.nice(yearSpan, false);
  137. var yearStep = numberUtil.nice(yearSpan / approxTickNum, true);
  138. interval *= yearStep;
  139. }
  140. var timezoneOffset = this.getSetting('useUTC') ? 0 : new Date(+extent[0] || +extent[1]).getTimezoneOffset() * 60 * 1000;
  141. var niceExtent = [Math.round(mathCeil((extent[0] - timezoneOffset) / interval) * interval + timezoneOffset), Math.round(mathFloor((extent[1] - timezoneOffset) / interval) * interval + timezoneOffset)];
  142. scaleHelper.fixExtent(niceExtent, extent);
  143. this._stepLvl = level; // Interval will be used in getTicks
  144. this._interval = interval;
  145. this._niceExtent = niceExtent;
  146. },
  147. parse: function (val) {
  148. // val might be float.
  149. return +numberUtil.parseDate(val);
  150. }
  151. });
  152. zrUtil.each(['contain', 'normalize'], function (methodName) {
  153. TimeScale.prototype[methodName] = function (val) {
  154. return intervalScaleProto[methodName].call(this, this.parse(val));
  155. };
  156. });
  157. /**
  158. * This implementation was originally copied from "d3.js"
  159. * <https://github.com/d3/d3/blob/b516d77fb8566b576088e73410437494717ada26/src/time/scale.js>
  160. * with some modifications made for this program.
  161. * See the license statement at the head of this file.
  162. */
  163. var scaleLevels = [// Format interval
  164. ['hh:mm:ss', ONE_SECOND], // 1s
  165. ['hh:mm:ss', ONE_SECOND * 5], // 5s
  166. ['hh:mm:ss', ONE_SECOND * 10], // 10s
  167. ['hh:mm:ss', ONE_SECOND * 15], // 15s
  168. ['hh:mm:ss', ONE_SECOND * 30], // 30s
  169. ['hh:mm\nMM-dd', ONE_MINUTE], // 1m
  170. ['hh:mm\nMM-dd', ONE_MINUTE * 5], // 5m
  171. ['hh:mm\nMM-dd', ONE_MINUTE * 10], // 10m
  172. ['hh:mm\nMM-dd', ONE_MINUTE * 15], // 15m
  173. ['hh:mm\nMM-dd', ONE_MINUTE * 30], // 30m
  174. ['hh:mm\nMM-dd', ONE_HOUR], // 1h
  175. ['hh:mm\nMM-dd', ONE_HOUR * 2], // 2h
  176. ['hh:mm\nMM-dd', ONE_HOUR * 6], // 6h
  177. ['hh:mm\nMM-dd', ONE_HOUR * 12], // 12h
  178. ['MM-dd\nyyyy', ONE_DAY], // 1d
  179. ['MM-dd\nyyyy', ONE_DAY * 2], // 2d
  180. ['MM-dd\nyyyy', ONE_DAY * 3], // 3d
  181. ['MM-dd\nyyyy', ONE_DAY * 4], // 4d
  182. ['MM-dd\nyyyy', ONE_DAY * 5], // 5d
  183. ['MM-dd\nyyyy', ONE_DAY * 6], // 6d
  184. ['week', ONE_DAY * 7], // 7d
  185. ['MM-dd\nyyyy', ONE_DAY * 10], // 10d
  186. ['week', ONE_DAY * 14], // 2w
  187. ['week', ONE_DAY * 21], // 3w
  188. ['month', ONE_DAY * 31], // 1M
  189. ['week', ONE_DAY * 42], // 6w
  190. ['month', ONE_DAY * 62], // 2M
  191. ['week', ONE_DAY * 70], // 10w
  192. ['quarter', ONE_DAY * 95], // 3M
  193. ['month', ONE_DAY * 31 * 4], // 4M
  194. ['month', ONE_DAY * 31 * 5], // 5M
  195. ['half-year', ONE_DAY * 380 / 2], // 6M
  196. ['month', ONE_DAY * 31 * 8], // 8M
  197. ['month', ONE_DAY * 31 * 10], // 10M
  198. ['year', ONE_DAY * 380] // 1Y
  199. ];
  200. /**
  201. * @param {module:echarts/model/Model}
  202. * @return {module:echarts/scale/Time}
  203. */
  204. TimeScale.create = function (model) {
  205. return new TimeScale({
  206. useUTC: model.ecModel.get('useUTC')
  207. });
  208. };
  209. var _default = TimeScale;
  210. module.exports = _default;