time.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. /**
  3. * @fileOverview The measurement of linear data scale function
  4. * @author dxq613@gmail.com
  5. */
  6. var fecha = require('fecha');
  7. var each = require('@antv/util/lib/each');
  8. var isNil = require('@antv/util/lib/type/is-nil');
  9. var isString = require('@antv/util/lib/type/is-string');
  10. var Base = require('./base');
  11. var Linear = require('./linear');
  12. var timeAuto = require('./auto/time');
  13. var TimeUtil = require('./time-util');
  14. /**
  15. * 时间度量的构造函数
  16. * @class Scale.Time
  17. */
  18. var Time =
  19. /*#__PURE__*/
  20. function (_Linear) {
  21. _inheritsLoose(Time, _Linear);
  22. function Time() {
  23. return _Linear.apply(this, arguments) || this;
  24. }
  25. var _proto = Time.prototype;
  26. _proto._initDefaultCfg = function _initDefaultCfg() {
  27. _Linear.prototype._initDefaultCfg.call(this);
  28. this.type = 'time';
  29. this.mask = 'YYYY-MM-DD';
  30. };
  31. /**
  32. * @override
  33. */
  34. _proto.init = function init() {
  35. var self = this;
  36. var values = self.values;
  37. if (values && values.length) {
  38. // 重新计算最大最小值
  39. var timeStamps = [];
  40. var min = Infinity; // 最小值
  41. var secondMin = min; // 次小值
  42. var max = 0; // 使用一个循环,计算min,max,secondMin
  43. each(values, function (v) {
  44. var timeStamp = self._toTimeStamp(v);
  45. if (isNaN(timeStamp)) {
  46. throw new TypeError("Invalid Time: " + v);
  47. }
  48. if (min > timeStamp) {
  49. secondMin = min;
  50. min = timeStamp;
  51. } else if (secondMin > timeStamp) {
  52. secondMin = timeStamp;
  53. }
  54. if (max < timeStamp) {
  55. max = timeStamp;
  56. }
  57. timeStamps.push(timeStamp);
  58. }); // 存在多个值时,设置最小间距
  59. if (values.length > 1) {
  60. self.minTickInterval = secondMin - min;
  61. }
  62. if (isNil(self.min) || self._toTimeStamp(self.min) > min) {
  63. self.min = min;
  64. }
  65. if (isNil(self.max) || self._toTimeStamp(self.max) < max) {
  66. self.max = max;
  67. }
  68. }
  69. _Linear.prototype.init.call(this);
  70. };
  71. _proto.calculateTicks = function calculateTicks() {
  72. var self = this;
  73. var min = self.min;
  74. var max = self.max;
  75. var count = self.tickCount;
  76. var interval = self.tickInterval;
  77. var tmp = timeAuto({
  78. min: min,
  79. max: max,
  80. minCount: count,
  81. maxCount: count,
  82. interval: interval,
  83. minInterval: self.minTickInterval
  84. });
  85. return tmp.ticks;
  86. };
  87. /**
  88. * @override
  89. */
  90. _proto.getText = function getText(value) {
  91. var formatter = this.formatter;
  92. value = this.translate(value);
  93. value = formatter ? formatter(value) : fecha.format(value, this.mask);
  94. return value;
  95. };
  96. /**
  97. * @override
  98. */
  99. _proto.scale = function scale(value) {
  100. if (isString(value)) {
  101. value = this.translate(value);
  102. }
  103. return _Linear.prototype.scale.call(this, value);
  104. };
  105. /**
  106. * @override
  107. */
  108. _proto.translate = function translate(value) {
  109. return this._toTimeStamp(value);
  110. }; // 将时间转换为时间戳
  111. _proto._toTimeStamp = function _toTimeStamp(value) {
  112. return TimeUtil.toTimeStamp(value);
  113. };
  114. return Time;
  115. }(Linear);
  116. Base.Time = Time;
  117. module.exports = Time;