time.js 2.7 KB

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