base.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { __extends, __spreadArrays } from "tslib";
  2. import { isNil, isNumber } from '@antv/util';
  3. import Base from '../base';
  4. /**
  5. * 分类度量
  6. * @class
  7. */
  8. var Category = /** @class */ (function (_super) {
  9. __extends(Category, _super);
  10. function Category() {
  11. var _this = _super !== null && _super.apply(this, arguments) || this;
  12. _this.type = 'cat';
  13. _this.isCategory = true;
  14. return _this;
  15. }
  16. Category.prototype.buildIndexMap = function () {
  17. if (!this.translateIndexMap) {
  18. this.translateIndexMap = new Map();
  19. // 重新构建缓存
  20. for (var i = 0; i < this.values.length; i++) {
  21. this.translateIndexMap.set(this.values[i], i);
  22. }
  23. }
  24. };
  25. Category.prototype.translate = function (value) {
  26. // 按需构建 map
  27. this.buildIndexMap();
  28. // 找得到
  29. var idx = this.translateIndexMap.get(value);
  30. if (idx === undefined) {
  31. idx = isNumber(value) ? value : NaN;
  32. }
  33. return idx;
  34. };
  35. Category.prototype.scale = function (value) {
  36. var order = this.translate(value);
  37. // 分类数据允许 0.5 范围内调整
  38. // if (order < this.min - 0.5 || order > this.max + 0.5) {
  39. // return NaN;
  40. // }
  41. var percent = this.calcPercent(order, this.min, this.max);
  42. return this.calcValue(percent, this.rangeMin(), this.rangeMax());
  43. };
  44. Category.prototype.invert = function (scaledValue) {
  45. var domainRange = this.max - this.min;
  46. var percent = this.calcPercent(scaledValue, this.rangeMin(), this.rangeMax());
  47. var idx = Math.round(domainRange * percent) + this.min;
  48. if (idx < this.min || idx > this.max) {
  49. return NaN;
  50. }
  51. return this.values[idx];
  52. };
  53. Category.prototype.getText = function (value) {
  54. var args = [];
  55. for (var _i = 1; _i < arguments.length; _i++) {
  56. args[_i - 1] = arguments[_i];
  57. }
  58. var v = value;
  59. // value为index
  60. if (isNumber(value) && !this.values.includes(value)) {
  61. v = this.values[v];
  62. }
  63. return _super.prototype.getText.apply(this, __spreadArrays([v], args));
  64. };
  65. // 复写属性
  66. Category.prototype.initCfg = function () {
  67. this.tickMethod = 'cat';
  68. };
  69. // 设置 min, max
  70. Category.prototype.setDomain = function () {
  71. // 用户有可能设置 min
  72. if (isNil(this.getConfig('min'))) {
  73. this.min = 0;
  74. }
  75. if (isNil(this.getConfig('max'))) {
  76. var size = this.values.length;
  77. this.max = size > 1 ? size - 1 : size;
  78. }
  79. // scale.init 的时候清除缓存
  80. if (this.translateIndexMap) {
  81. this.translateIndexMap = undefined;
  82. }
  83. };
  84. return Category;
  85. }(Base));
  86. export default Category;
  87. //# sourceMappingURL=base.js.map