base.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var util_1 = require("@antv/util");
  4. var register_1 = require("./tick-method/register");
  5. var Scale = /** @class */ (function () {
  6. function Scale(cfg) {
  7. /**
  8. * 度量的类型
  9. */
  10. this.type = 'base';
  11. /**
  12. * 是否分类类型的度量
  13. */
  14. this.isCategory = false;
  15. /**
  16. * 是否线性度量,有linear, time 度量
  17. */
  18. this.isLinear = false;
  19. /**
  20. * 是否连续类型的度量,linear,time,log, pow, quantile, quantize 都支持
  21. */
  22. this.isContinuous = false;
  23. /**
  24. * 是否是常量的度量,传入和传出一致
  25. */
  26. this.isIdentity = false;
  27. this.values = [];
  28. this.range = [0, 1];
  29. this.ticks = [];
  30. this.__cfg__ = cfg;
  31. this.initCfg();
  32. this.init();
  33. }
  34. // 对于原始值的必要转换,如分类、时间字段需转换成数值,用transform/map命名可能更好
  35. Scale.prototype.translate = function (v) {
  36. return v;
  37. };
  38. /** 重新初始化 */
  39. Scale.prototype.change = function (cfg) {
  40. // 覆盖配置项,而不替代
  41. util_1.assign(this.__cfg__, cfg);
  42. this.init();
  43. };
  44. Scale.prototype.clone = function () {
  45. return this.constructor(this.__cfg__);
  46. };
  47. /** 获取坐标轴需要的ticks */
  48. Scale.prototype.getTicks = function () {
  49. var _this = this;
  50. return util_1.map(this.ticks, function (tick, idx) {
  51. if (util_1.isObject(tick)) {
  52. // 仅当符合Tick类型时才有意义
  53. return tick;
  54. }
  55. return {
  56. text: _this.getText(tick, idx),
  57. tickValue: tick,
  58. value: _this.scale(tick),
  59. };
  60. });
  61. };
  62. /** 获取Tick的格式化结果 */
  63. Scale.prototype.getText = function (value, key) {
  64. var formatter = this.formatter;
  65. var res = formatter ? formatter(value, key) : value;
  66. if (util_1.isNil(res) || !util_1.isFunction(res.toString)) {
  67. return '';
  68. }
  69. return res.toString();
  70. };
  71. // 获取配置项中的值,当前 scale 上的值可能会被修改
  72. Scale.prototype.getConfig = function (key) {
  73. return this.__cfg__[key];
  74. };
  75. // scale初始化
  76. Scale.prototype.init = function () {
  77. util_1.assign(this, this.__cfg__);
  78. this.setDomain();
  79. if (util_1.isEmpty(this.getConfig('ticks'))) {
  80. this.ticks = this.calculateTicks();
  81. }
  82. };
  83. // 子类上覆盖某些属性,不能直接在类上声明,否则会被覆盖
  84. Scale.prototype.initCfg = function () { };
  85. Scale.prototype.setDomain = function () { };
  86. Scale.prototype.calculateTicks = function () {
  87. var tickMethod = this.tickMethod;
  88. var ticks = [];
  89. if (util_1.isString(tickMethod)) {
  90. var method = register_1.getTickMethod(tickMethod);
  91. if (!method) {
  92. throw new Error('There is no method to to calculate ticks!');
  93. }
  94. ticks = method(this);
  95. }
  96. else if (util_1.isFunction(tickMethod)) {
  97. ticks = tickMethod(this);
  98. }
  99. return ticks;
  100. };
  101. // range 的最小值
  102. Scale.prototype.rangeMin = function () {
  103. return this.range[0];
  104. };
  105. // range 的最大值
  106. Scale.prototype.rangeMax = function () {
  107. return this.range[1];
  108. };
  109. /** 定义域转 0~1 */
  110. Scale.prototype.calcPercent = function (value, min, max) {
  111. if (util_1.isNumber(value)) {
  112. return (value - min) / (max - min);
  113. }
  114. return NaN;
  115. };
  116. /** 0~1转定义域 */
  117. Scale.prototype.calcValue = function (percent, min, max) {
  118. return min + percent * (max - min);
  119. };
  120. return Scale;
  121. }());
  122. exports.default = Scale;
  123. //# sourceMappingURL=base.js.map