NuclearDispersionSystem/ant-design-vue-jeecg/node_modules/@antv/scale/lib/log.js
2023-09-14 14:47:11 +08:00

194 lines
4.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
/**
* @fileOverview 使用度量进行log转换
* @author dxq613@gmail.com
*/
var each = require('@antv/util/lib/each');
var Base = require('./base');
var Linear = require('./linear'); // 计算log
function log(a, b) {
if (a === 1) {
return 1;
}
return Math.log(b) / Math.log(a);
}
/**
* 度量的log计算
* @class Scale.Log
*/
var Log = /*#__PURE__*/function (_Linear) {
_inheritsLoose(Log, _Linear);
function Log() {
return _Linear.apply(this, arguments) || this;
}
var _proto = Log.prototype;
_proto._initDefaultCfg = function _initDefaultCfg() {
_Linear.prototype._initDefaultCfg.call(this);
this.type = 'log';
/**
* @override
* log 的坐标点的个数控制在10个以下
* @type {Number}
*/
this.tickCount = 10;
/**
* 进行log计算的基数
* @type {Number}
*/
this.base = 2; // 最小的tick仅内部使用
this._minTick = null;
}
/**
* @override
*/
;
_proto.calculateTicks = function calculateTicks() {
var self = this;
var base = self.base;
var minTick;
if (self.min < 0) {
throw new Error('The minimum value must be greater than zero!');
}
var maxTick = log(base, self.max);
if (self.min > 0) {
minTick = Math.floor(log(base, self.min));
} else {
var values = self.values;
var positiveMin = self.max; // 查找大于0的第一个值, 如果都小于0默认为1
each(values, function (value) {
if (value > 0 && value < positiveMin) {
positiveMin = value;
}
});
if (positiveMin === self.max) {
positiveMin = self.max / base;
}
if (positiveMin > 1) {
positiveMin = 1;
}
minTick = Math.floor(log(base, positiveMin));
self._minTick = minTick;
self.positiveMin = positiveMin;
}
var count = maxTick - minTick;
var tickCount = self.tickCount;
var avg = Math.ceil(count / tickCount);
var ticks = [];
for (var i = minTick; i < maxTick + avg; i = i + avg) {
ticks.push(Math.pow(base, i));
}
if (self.min === 0) {
ticks.unshift(0);
}
return ticks;
} // 获取度量计算时value占的定义域百分比
;
_proto._getScalePercent = function _getScalePercent(value) {
var max = this.max;
var min = this.min;
if (max === min) {
return 0;
} // 如果值小于等于0则按照0处理
if (value <= 0) {
return 0;
}
var base = this.base;
var positiveMin = this.positiveMin; // 如果min == 0, 则根据比0大的最小值计算比例关系。这个最小值作为坐标轴上的第二个tick第一个是0但是不显示
if (positiveMin) {
min = positiveMin * 1 / base;
}
var percent; // 如果数值小于次小值,那么就计算 value / 次小值 占整体的比例
if (value < positiveMin) {
percent = value / positiveMin / (log(base, max) - log(base, min));
} else {
percent = (log(base, value) - log(base, min)) / (log(base, max) - log(base, 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 base = this.base;
var max = log(base, this.max);
var rangeMin = this.rangeMin();
var range = this.rangeMax() - rangeMin;
var min;
var positiveMin = this.positiveMin;
if (positiveMin) {
if (value === 0) {
return 0;
}
min = log(base, positiveMin / base);
var appendPercent = 1 / (max - min) * range; // 0 到 positiveMin的占比
if (value < appendPercent) {
// 落到 0 - positiveMin 之间
return value / appendPercent * positiveMin;
}
} else {
min = log(base, this.min);
}
var percent = (value - rangeMin) / range;
var tmp = percent * (max - min) + min;
return Math.pow(base, tmp);
};
return Log;
}(Linear);
Base.Log = Log;
module.exports = Log;