129 lines
2.8 KiB
JavaScript
129 lines
2.8 KiB
JavaScript
![]() |
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
|
|||
|
|
|||
|
/**
|
|||
|
* @fileOverview 使用pow进行度量计算
|
|||
|
* @author dxq613@gmail.com
|
|||
|
*/
|
|||
|
var Base = require('./base');
|
|||
|
|
|||
|
var Linear = require('./linear'); // 求以a为次幂,结果为b的基数,如 x^^a = b;求x
|
|||
|
|
|||
|
|
|||
|
function calBase(a, b) {
|
|||
|
var e = Math.E;
|
|||
|
var value = Math.pow(e, Math.log(b) / a); // 使用换底公式求底
|
|||
|
|
|||
|
return value;
|
|||
|
}
|
|||
|
/**
|
|||
|
* 度量的Pow计算
|
|||
|
* @class Scale.Log
|
|||
|
*/
|
|||
|
|
|||
|
|
|||
|
var Pow = /*#__PURE__*/function (_Linear) {
|
|||
|
_inheritsLoose(Pow, _Linear);
|
|||
|
|
|||
|
function Pow() {
|
|||
|
return _Linear.apply(this, arguments) || this;
|
|||
|
}
|
|||
|
|
|||
|
var _proto = Pow.prototype;
|
|||
|
|
|||
|
_proto._initDefaultCfg = function _initDefaultCfg() {
|
|||
|
_Linear.prototype._initDefaultCfg.call(this);
|
|||
|
|
|||
|
this.type = 'pow';
|
|||
|
/**
|
|||
|
* @override
|
|||
|
* pow 的坐标点的个数控制在10个以下
|
|||
|
* @type {Number}
|
|||
|
*/
|
|||
|
|
|||
|
this.tickCount = 10;
|
|||
|
/**
|
|||
|
* 进行pow计算的基数
|
|||
|
* @type {Number}
|
|||
|
*/
|
|||
|
|
|||
|
this.exponent = 2;
|
|||
|
}
|
|||
|
/**
|
|||
|
* @override
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_proto.calculateTicks = function calculateTicks() {
|
|||
|
var self = this;
|
|||
|
var exponent = self.exponent;
|
|||
|
var min;
|
|||
|
var max = Math.ceil(calBase(exponent, self.max));
|
|||
|
|
|||
|
if (self.min >= 0) {
|
|||
|
min = Math.floor(calBase(exponent, self.min));
|
|||
|
} else {
|
|||
|
min = 0;
|
|||
|
}
|
|||
|
|
|||
|
if (min > max) {
|
|||
|
var tmp = max;
|
|||
|
max = min;
|
|||
|
min = tmp;
|
|||
|
}
|
|||
|
|
|||
|
var count = max - min;
|
|||
|
var tickCount = self.tickCount;
|
|||
|
var avg = Math.ceil(count / tickCount);
|
|||
|
var ticks = [];
|
|||
|
|
|||
|
for (var i = min; i < max + avg; i = i + avg) {
|
|||
|
ticks.push(Math.pow(i, exponent));
|
|||
|
}
|
|||
|
|
|||
|
return ticks;
|
|||
|
} // 获取度量计算时,value占的定义域百分比
|
|||
|
;
|
|||
|
|
|||
|
_proto._getScalePercent = function _getScalePercent(value) {
|
|||
|
var max = this.max;
|
|||
|
var min = this.min;
|
|||
|
|
|||
|
if (max === min) {
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
var exponent = this.exponent;
|
|||
|
var percent = (calBase(exponent, value) - calBase(exponent, min)) / (calBase(exponent, max) - calBase(exponent, min));
|
|||
|
return percent;
|
|||
|
}
|
|||
|
/**
|
|||
|
* @override
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_proto.scale = function scale(value) {
|
|||
|
var percent = this._getScalePercent(value);
|
|||
|
|
|||
|
var rangeMin = this.rangeMin();
|
|||
|
var rangeMax = this.rangeMax();
|
|||
|
return rangeMin + percent * (rangeMax - rangeMin);
|
|||
|
}
|
|||
|
/**
|
|||
|
* @override
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_proto.invert = function invert(value) {
|
|||
|
var percent = (value - this.rangeMin()) / (this.rangeMax() - this.rangeMin());
|
|||
|
var exponent = this.exponent;
|
|||
|
var max = calBase(exponent, this.max);
|
|||
|
var min = calBase(exponent, this.min);
|
|||
|
var tmp = percent * (max - min) + min;
|
|||
|
return Math.pow(tmp, exponent);
|
|||
|
};
|
|||
|
|
|||
|
return Pow;
|
|||
|
}(Linear);
|
|||
|
|
|||
|
Base.Pow = Pow;
|
|||
|
module.exports = Pow;
|