time.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { __extends } from "tslib";
  2. import { each, isNumber } from '@antv/util';
  3. import { timeFormat, toTimeStamp } from '../util/time';
  4. import Category from './base';
  5. /**
  6. * 时间分类度量
  7. * @class
  8. */
  9. var TimeCat = /** @class */ (function (_super) {
  10. __extends(TimeCat, _super);
  11. function TimeCat() {
  12. var _this = _super !== null && _super.apply(this, arguments) || this;
  13. _this.type = 'timeCat';
  14. return _this;
  15. }
  16. /**
  17. * @override
  18. */
  19. TimeCat.prototype.translate = function (value) {
  20. value = toTimeStamp(value);
  21. var index = this.values.indexOf(value);
  22. if (index === -1) {
  23. if (isNumber(value) && value < this.values.length) {
  24. index = value;
  25. }
  26. else {
  27. index = NaN;
  28. }
  29. }
  30. return index;
  31. };
  32. /**
  33. * 由于时间类型数据需要转换一下,所以复写 getText
  34. * @override
  35. */
  36. TimeCat.prototype.getText = function (value, tickIndex) {
  37. var index = this.translate(value);
  38. if (index > -1) {
  39. var result = this.values[index];
  40. var formatter = this.formatter;
  41. result = formatter ? formatter(result, tickIndex) : timeFormat(result, this.mask);
  42. return result;
  43. }
  44. return value;
  45. };
  46. TimeCat.prototype.initCfg = function () {
  47. this.tickMethod = 'time-cat';
  48. this.mask = 'YYYY-MM-DD';
  49. this.tickCount = 7; // 一般时间数据会显示 7, 14, 30 天的数字
  50. };
  51. TimeCat.prototype.setDomain = function () {
  52. var values = this.values;
  53. // 针对时间分类类型,会将时间统一转换为时间戳
  54. each(values, function (v, i) {
  55. values[i] = toTimeStamp(v);
  56. });
  57. values.sort(function (v1, v2) {
  58. return v1 - v2;
  59. });
  60. _super.prototype.setDomain.call(this);
  61. };
  62. return TimeCat;
  63. }(Category));
  64. export default TimeCat;
  65. //# sourceMappingURL=time.js.map