time-cat.js 3.5 KB

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