pow.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. /**
  3. * @fileOverview 使用pow进行度量计算
  4. * @author dxq613@gmail.com
  5. */
  6. var Base = require('./base');
  7. var Linear = require('./linear'); // 求以a为次幂,结果为b的基数,如 x^^a = b;求x
  8. function calBase(a, b) {
  9. var e = Math.E;
  10. var value = Math.pow(e, Math.log(b) / a); // 使用换底公式求底
  11. return value;
  12. }
  13. /**
  14. * 度量的Pow计算
  15. * @class Scale.Log
  16. */
  17. var Pow =
  18. /*#__PURE__*/
  19. function (_Linear) {
  20. _inheritsLoose(Pow, _Linear);
  21. function Pow() {
  22. return _Linear.apply(this, arguments) || this;
  23. }
  24. var _proto = Pow.prototype;
  25. _proto._initDefaultCfg = function _initDefaultCfg() {
  26. _Linear.prototype._initDefaultCfg.call(this);
  27. this.type = 'pow';
  28. /**
  29. * @override
  30. * pow 的坐标点的个数控制在10个以下
  31. * @type {Number}
  32. */
  33. this.tickCount = 10;
  34. /**
  35. * 进行pow计算的基数
  36. * @type {Number}
  37. */
  38. this.exponent = 2;
  39. }
  40. /**
  41. * @override
  42. */
  43. ;
  44. _proto.calculateTicks = function calculateTicks() {
  45. var self = this;
  46. var exponent = self.exponent;
  47. var min;
  48. var max = Math.ceil(calBase(exponent, self.max));
  49. if (self.min >= 0) {
  50. min = Math.floor(calBase(exponent, self.min));
  51. } else {
  52. min = 0;
  53. }
  54. if (min > max) {
  55. var tmp = max;
  56. max = min;
  57. min = tmp;
  58. }
  59. var count = max - min;
  60. var tickCount = self.tickCount;
  61. var avg = Math.ceil(count / tickCount);
  62. var ticks = [];
  63. for (var i = min; i < max + avg; i = i + avg) {
  64. ticks.push(Math.pow(i, exponent));
  65. }
  66. return ticks;
  67. } // 获取度量计算时,value占的定义域百分比
  68. ;
  69. _proto._getScalePercent = function _getScalePercent(value) {
  70. var max = this.max;
  71. var min = this.min;
  72. if (max === min) {
  73. return 0;
  74. }
  75. var exponent = this.exponent;
  76. var percent = (calBase(exponent, value) - calBase(exponent, min)) / (calBase(exponent, max) - calBase(exponent, min));
  77. return percent;
  78. }
  79. /**
  80. * @override
  81. */
  82. ;
  83. _proto.scale = function scale(value) {
  84. var percent = this._getScalePercent(value);
  85. var rangeMin = this.rangeMin();
  86. var rangeMax = this.rangeMax();
  87. return rangeMin + percent * (rangeMax - rangeMin);
  88. }
  89. /**
  90. * @override
  91. */
  92. ;
  93. _proto.invert = function invert(value) {
  94. var percent = (value - this.rangeMin()) / (this.rangeMax() - this.rangeMin());
  95. var exponent = this.exponent;
  96. var max = calBase(exponent, this.max);
  97. var min = calBase(exponent, this.min);
  98. var tmp = percent * (max - min) + min;
  99. return Math.pow(tmp, exponent);
  100. };
  101. return Pow;
  102. }(Linear);
  103. Base.Pow = Pow;
  104. module.exports = Pow;