123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
- /**
- * @fileOverview The measurement of linear data scale function
- * @author dxq613@gmail.com
- */
- var isNil = require('@antv/util/lib/type/is-nil');
- var each = require('@antv/util/lib/each');
- var Base = require('./base');
- var numberAuto = require('./auto/number');
- /**
- * 线性度量
- * @class Scale.Linear
- */
- var Linear =
- /*#__PURE__*/
- function (_Base) {
- _inheritsLoose(Linear, _Base);
- function Linear() {
- return _Base.apply(this, arguments) || this;
- }
- var _proto = Linear.prototype;
- _proto._initDefaultCfg = function _initDefaultCfg() {
- _Base.prototype._initDefaultCfg.call(this);
- var self = this;
- self.type = 'linear';
- self.isLinear = true;
- /**
- * 是否为了用户习惯,优化min,max和ticks,如果进行优化,则会根据生成的ticks调整min,max,否则舍弃(min,max)范围之外的ticks
- * @type {Boolean}
- * @default false
- */
- self.nice = false;
- /**
- * min value of the scale
- * @type {Number}
- * @default null
- */
- self.min = null;
- /**
- * min value limitted of the scale
- * @type {Number}
- * @default null
- */
- self.minLimit = null;
- /**
- * max value of the scale
- * @type {Number}
- * @default null
- */
- self.max = null;
- /**
- * max value limitted of the scale
- * @type {Number}
- * @default null
- */
- self.maxLimit = null;
- /**
- * 自动生成标记时的个数
- * @type {Number}
- * @default null
- */
- self.tickCount = null;
- /**
- * 坐标轴点之间的间距,指的是真实数据的差值
- * @type {Number}
- * @default null
- */
- self.tickInterval = null;
- /**
- * 坐标轴点之间的最小间距,指的是真实数据的差值
- * @type {Number}
- * @default null
- */
- self.minTickInterval = null;
- /**
- * 用于计算坐标点时逼近的数组
- * @type {Array}
- */
- self.snapArray = null;
- }
- /**
- * @protected
- * @override
- */
- ;
- _proto.init = function init() {
- var self = this;
- if (!self.ticks) {
- self.min = self.translate(self.min);
- self.max = self.translate(self.max);
- self.initTicks();
- } else {
- var ticks = self.ticks;
- var firstValue = self.translate(ticks[0]);
- var lastValue = self.translate(ticks[ticks.length - 1]);
- if (isNil(self.min) || self.min > firstValue) {
- self.min = firstValue;
- }
- if (isNil(self.max) || self.max < lastValue) {
- self.max = lastValue;
- }
- }
- }
- /**
- * 计算坐标点
- * @protected
- * @return {Array} 计算完成的坐标点
- */
- ;
- _proto.calculateTicks = function calculateTicks() {
- var min = this.min,
- max = this.max,
- minLimit = this.minLimit,
- maxLimit = this.maxLimit,
- tickCount = this.tickCount,
- tickInterval = this.tickInterval,
- minTickInterval = this.minTickInterval,
- snapArray = this.snapArray;
- if (tickCount === 1) {
- throw new Error('linear scale\'tickCount should not be 1');
- }
- if (max < min) {
- throw new Error("max: " + max + " should not be less than min: " + min);
- }
- var tmp = numberAuto({
- min: min,
- max: max,
- minLimit: minLimit,
- maxLimit: maxLimit,
- minCount: tickCount,
- maxCount: tickCount,
- interval: tickInterval,
- minTickInterval: minTickInterval,
- snapArray: snapArray
- });
- return tmp.ticks;
- } // 初始化ticks
- ;
- _proto.initTicks = function initTicks() {
- var self = this;
- var calTicks = self.calculateTicks();
- if (self.nice) {
- // 如果需要优化显示的tick
- self.ticks = calTicks;
- self.min = calTicks[0];
- self.max = calTicks[calTicks.length - 1];
- } else {
- var ticks = [];
- each(calTicks, function (tick) {
- if (tick >= self.min && tick <= self.max) {
- ticks.push(tick);
- }
- }); // 如果 ticks 为空,直接输入最小值、最大值
- if (!ticks.length) {
- ticks.push(self.min);
- ticks.push(self.max);
- }
- self.ticks = ticks;
- }
- }
- /**
- * @override
- */
- ;
- _proto.scale = function scale(value) {
- if (isNil(value)) {
- return NaN;
- }
- var max = this.max;
- var min = this.min;
- if (max === min) {
- return 0;
- }
- var percent = (value - min) / (max - min);
- 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());
- return this.min + percent * (this.max - this.min);
- };
- return Linear;
- }(Base);
- Base.Linear = Linear;
- module.exports = Linear;
|