log.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var math_1 = require("../util/math");
  5. var base_1 = require("./base");
  6. /**
  7. * Log 度量,处理非均匀分布
  8. */
  9. var Log = /** @class */ (function (_super) {
  10. tslib_1.__extends(Log, _super);
  11. function Log() {
  12. var _this = _super !== null && _super.apply(this, arguments) || this;
  13. _this.type = 'log';
  14. return _this;
  15. }
  16. /**
  17. * @override
  18. */
  19. Log.prototype.invert = function (value) {
  20. var base = this.base;
  21. var max = math_1.log(base, this.max);
  22. var rangeMin = this.rangeMin();
  23. var range = this.rangeMax() - rangeMin;
  24. var min;
  25. var positiveMin = this.positiveMin;
  26. if (positiveMin) {
  27. if (value === 0) {
  28. return 0;
  29. }
  30. min = math_1.log(base, positiveMin / base);
  31. var appendPercent = (1 / (max - min)) * range; // 0 到 positiveMin的占比
  32. if (value < appendPercent) {
  33. // 落到 0 - positiveMin 之间
  34. return (value / appendPercent) * positiveMin;
  35. }
  36. }
  37. else {
  38. min = math_1.log(base, this.min);
  39. }
  40. var percent = (value - rangeMin) / range;
  41. var tmp = percent * (max - min) + min;
  42. return Math.pow(base, tmp);
  43. };
  44. Log.prototype.initCfg = function () {
  45. this.tickMethod = 'log';
  46. this.base = 10;
  47. this.tickCount = 6;
  48. this.nice = true;
  49. };
  50. // 设置
  51. Log.prototype.setDomain = function () {
  52. _super.prototype.setDomain.call(this);
  53. var min = this.min;
  54. if (min < 0) {
  55. throw new Error('When you use log scale, the minimum value must be greater than zero!');
  56. }
  57. if (min === 0) {
  58. this.positiveMin = math_1.getLogPositiveMin(this.values, this.base, this.max);
  59. }
  60. };
  61. // 根据当前值获取占比
  62. Log.prototype.getScalePercent = function (value) {
  63. var max = this.max;
  64. var min = this.min;
  65. if (max === min) {
  66. return 0;
  67. }
  68. // 如果值小于等于0,则按照0处理
  69. if (value <= 0) {
  70. return 0;
  71. }
  72. var base = this.base;
  73. var positiveMin = this.positiveMin;
  74. // 如果min == 0, 则根据比0大的最小值,计算比例关系。这个最小值作为坐标轴上的第二个tick,第一个是0但是不显示
  75. if (positiveMin) {
  76. min = (positiveMin * 1) / base;
  77. }
  78. var percent;
  79. // 如果数值小于次小值,那么就计算 value / 次小值 占整体的比例
  80. if (value < positiveMin) {
  81. percent = value / positiveMin / (math_1.log(base, max) - math_1.log(base, min));
  82. }
  83. else {
  84. percent = (math_1.log(base, value) - math_1.log(base, min)) / (math_1.log(base, max) - math_1.log(base, min));
  85. }
  86. return percent;
  87. };
  88. return Log;
  89. }(base_1.default));
  90. exports.default = Log;
  91. //# sourceMappingURL=log.js.map