base.js 3.8 KB

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