log.js 2.9 KB

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