base.js 3.0 KB

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