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. ;
  63. _proto.calculateTicks = function calculateTicks() {
  64. var self = this;
  65. var count = self.tickCount;
  66. var ticks;
  67. if (count) {
  68. var temp = catAuto({
  69. maxCount: count,
  70. data: self.values,
  71. isRounding: self.isRounding
  72. });
  73. ticks = temp.ticks;
  74. } else {
  75. ticks = self.values;
  76. }
  77. return ticks;
  78. }
  79. /**
  80. * @override
  81. */
  82. ;
  83. _proto.translate = function translate(value) {
  84. value = this._toTimeStamp(value);
  85. var index = this.values.indexOf(value);
  86. if (index === -1) {
  87. if (isNumber(value) && value < this.values.length) {
  88. index = value;
  89. } else {
  90. index = NaN;
  91. }
  92. }
  93. return index;
  94. }
  95. /**
  96. * @override
  97. */
  98. ;
  99. _proto.scale = function scale(value) {
  100. var rangeMin = this.rangeMin();
  101. var rangeMax = this.rangeMax();
  102. var index = this.translate(value);
  103. var percent;
  104. if (this.values.length === 1 || isNaN(index)) {
  105. // is index is NAN should not be set as 0
  106. percent = index;
  107. } else if (index > -1) {
  108. percent = index / (this.values.length - 1);
  109. } else {
  110. percent = 0;
  111. }
  112. return rangeMin + percent * (rangeMax - rangeMin);
  113. }
  114. /**
  115. * @override
  116. */
  117. ;
  118. _proto.getText = function getText(value) {
  119. var result = '';
  120. var index = this.translate(value);
  121. if (index > -1) {
  122. result = this.values[index];
  123. } else {
  124. result = value;
  125. }
  126. var formatter = this.formatter;
  127. result = parseInt(result, 10);
  128. result = formatter ? formatter(result) : fecha.format(result, this.mask);
  129. return result;
  130. }
  131. /**
  132. * @override
  133. */
  134. ;
  135. _proto.getTicks = function getTicks() {
  136. var self = this;
  137. var ticks = this.ticks;
  138. var rst = [];
  139. each(ticks, function (tick) {
  140. var obj;
  141. if (isObject(tick)) {
  142. obj = tick;
  143. } else {
  144. obj = {
  145. text: isString(tick) ? tick : self.getText(tick),
  146. value: self.scale(tick),
  147. tickValue: tick // 用于坐标轴上文本动画时确定前后帧的对应关系
  148. };
  149. }
  150. rst.push(obj);
  151. });
  152. return rst;
  153. } // 将时间转换为时间戳
  154. ;
  155. _proto._toTimeStamp = function _toTimeStamp(value) {
  156. return TimeUtil.toTimeStamp(value);
  157. };
  158. return TimeCategory;
  159. }(Category);
  160. Base.TimeCat = TimeCategory;
  161. module.exports = TimeCategory;