category.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. var Base = require('./base');
  3. var catAuto = require('./auto/cat');
  4. var each = require('@antv/util/lib/each');
  5. var isNumber = require('@antv/util/lib/type/is-number');
  6. var isString = require('@antv/util/lib/type/is-string');
  7. var Category =
  8. /*#__PURE__*/
  9. function (_Base) {
  10. _inheritsLoose(Category, _Base);
  11. function Category() {
  12. return _Base.apply(this, arguments) || this;
  13. }
  14. var _proto = Category.prototype;
  15. _proto._initDefaultCfg = function _initDefaultCfg() {
  16. _Base.prototype._initDefaultCfg.call(this);
  17. this.type = 'cat';
  18. /**
  19. * 是否分类度量
  20. * @type {Boolean}
  21. */
  22. this.isCategory = true;
  23. this.isRounding = true; // 是否进行取整操作
  24. }
  25. /**
  26. * @override
  27. */
  28. ;
  29. _proto.init = function init() {
  30. var self = this;
  31. var values = self.values;
  32. var tickCount = self.tickCount;
  33. each(values, function (v, i) {
  34. values[i] = v.toString();
  35. });
  36. if (!self.ticks) {
  37. var ticks = values;
  38. if (tickCount) {
  39. var temp = catAuto({
  40. maxCount: tickCount,
  41. data: values,
  42. isRounding: self.isRounding
  43. });
  44. ticks = temp.ticks;
  45. }
  46. this.ticks = ticks;
  47. }
  48. }
  49. /**
  50. * @override
  51. */
  52. ;
  53. _proto.getText = function getText(value) {
  54. if (this.values.indexOf(value) === -1 && isNumber(value)) {
  55. value = this.values[Math.round(value)];
  56. }
  57. return _Base.prototype.getText.call(this, value);
  58. }
  59. /**
  60. * @override
  61. */
  62. ;
  63. _proto.translate = function translate(value) {
  64. var index = this.values.indexOf(value);
  65. if (index === -1 && isNumber(value)) {
  66. index = value;
  67. } else if (index === -1) {
  68. index = NaN;
  69. }
  70. return index;
  71. }
  72. /**
  73. * @override
  74. */
  75. ;
  76. _proto.scale = function scale(value) {
  77. var rangeMin = this.rangeMin();
  78. var rangeMax = this.rangeMax();
  79. var percent;
  80. if (isString(value) || this.values.indexOf(value) !== -1) {
  81. value = this.translate(value);
  82. }
  83. if (this.values.length > 1) {
  84. percent = value / (this.values.length - 1);
  85. } else {
  86. percent = value;
  87. }
  88. return rangeMin + percent * (rangeMax - rangeMin);
  89. }
  90. /**
  91. * @override
  92. */
  93. ;
  94. _proto.invert = function invert(value) {
  95. if (isString(value)) {
  96. // 如果已经是字符串
  97. return value;
  98. }
  99. var min = this.rangeMin();
  100. var max = this.rangeMax(); // 归一到 范围内
  101. if (value < min) {
  102. value = min;
  103. }
  104. if (value > max) {
  105. value = max;
  106. }
  107. var percent = (value - min) / (max - min);
  108. var index = Math.round(percent * (this.values.length - 1)) % this.values.length;
  109. index = index || 0;
  110. return this.values[index];
  111. };
  112. return Category;
  113. }(Base);
  114. Base.Cat = Category;
  115. module.exports = Category;