time-cat.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. /**
  3. * @fileOverview 时间数据作为分类类型
  4. * @author dxq613@gmail.com
  5. */
  6. var Base = require('./base');
  7. var Category = require('./category');
  8. var fecha = require('fecha');
  9. var catAuto = require('./auto/cat');
  10. var TimeUtil = require('./time-util');
  11. var each = require('@antv/util/lib/each');
  12. var isNumber = require('@antv/util/lib/type/is-number');
  13. var isObject = require('@antv/util/lib/type/is-object');
  14. var isString = require('@antv/util/lib/type/is-string');
  15. /**
  16. * 度量的构造函数
  17. * @class Scale.TimeCategory
  18. */
  19. var TimeCategory =
  20. /*#__PURE__*/
  21. function (_Category) {
  22. _inheritsLoose(TimeCategory, _Category);
  23. function TimeCategory() {
  24. return _Category.apply(this, arguments) || this;
  25. }
  26. var _proto = TimeCategory.prototype;
  27. _proto._initDefaultCfg = function _initDefaultCfg() {
  28. _Category.prototype._initDefaultCfg.call(this);
  29. this.type = 'timeCat';
  30. /**
  31. * 是否需要排序,默认进行排序
  32. * @type {Boolean}
  33. */
  34. this.sortable = true;
  35. this.tickCount = 5;
  36. /**
  37. * 时间格式化
  38. * @type {String}
  39. */
  40. this.mask = 'YYYY-MM-DD';
  41. };
  42. _proto.init = function init() {
  43. var self = this;
  44. var values = this.values; // 针对时间分类类型,会将时间统一转换为时间戳
  45. each(values, function (v, i) {
  46. values[i] = self._toTimeStamp(v);
  47. });
  48. if (this.sortable) {
  49. // 允许排序
  50. values.sort(function (v1, v2) {
  51. return v1 - v2;
  52. });
  53. }
  54. if (!self.ticks) {
  55. self.ticks = this.calculateTicks();
  56. }
  57. };
  58. /**
  59. * 计算 ticks
  60. * @return {array} 返回 ticks 数组
  61. */
  62. _proto.calculateTicks = function calculateTicks() {
  63. var self = this;
  64. var count = self.tickCount;
  65. var ticks;
  66. if (count) {
  67. var temp = catAuto({
  68. maxCount: count,
  69. data: self.values,
  70. isRounding: self.isRounding
  71. });
  72. ticks = temp.ticks;
  73. } else {
  74. ticks = self.values;
  75. }
  76. return ticks;
  77. };
  78. /**
  79. * @override
  80. */
  81. _proto.translate = function translate(value) {
  82. value = this._toTimeStamp(value);
  83. var index = this.values.indexOf(value);
  84. if (index === -1) {
  85. if (isNumber(value) && value < this.values.length) {
  86. index = value;
  87. } else {
  88. index = NaN;
  89. }
  90. }
  91. return index;
  92. };
  93. /**
  94. * @override
  95. */
  96. _proto.scale = function scale(value) {
  97. var rangeMin = this.rangeMin();
  98. var rangeMax = this.rangeMax();
  99. var index = this.translate(value);
  100. var percent;
  101. if (this.values.length === 1 || isNaN(index)) {
  102. // is index is NAN should not be set as 0
  103. percent = index;
  104. } else if (index > -1) {
  105. percent = index / (this.values.length - 1);
  106. } else {
  107. percent = 0;
  108. }
  109. return rangeMin + percent * (rangeMax - rangeMin);
  110. };
  111. /**
  112. * @override
  113. */
  114. _proto.getText = function getText(value) {
  115. var result = '';
  116. var index = this.translate(value);
  117. if (index > -1) {
  118. result = this.values[index];
  119. } else {
  120. result = value;
  121. }
  122. var formatter = this.formatter;
  123. result = parseInt(result, 10);
  124. result = formatter ? formatter(result) : fecha.format(result, this.mask);
  125. return result;
  126. };
  127. /**
  128. * @override
  129. */
  130. _proto.getTicks = function getTicks() {
  131. var self = this;
  132. var ticks = this.ticks;
  133. var rst = [];
  134. each(ticks, function (tick) {
  135. var obj;
  136. if (isObject(tick)) {
  137. obj = tick;
  138. } else {
  139. obj = {
  140. text: isString(tick) ? tick : self.getText(tick),
  141. value: self.scale(tick),
  142. tickValue: tick // 用于坐标轴上文本动画时确定前后帧的对应关系
  143. };
  144. }
  145. rst.push(obj);
  146. });
  147. return rst;
  148. }; // 将时间转换为时间戳
  149. _proto._toTimeStamp = function _toTimeStamp(value) {
  150. return TimeUtil.toTimeStamp(value);
  151. };
  152. return TimeCategory;
  153. }(Category);
  154. Base.TimeCat = TimeCategory;
  155. module.exports = TimeCategory;