@visactor/vscale
Version:
Scales for visual encoding, used in VGrammar, VTable
56 lines (54 loc) • 3.31 kB
JavaScript
import { clamp, isNil, isValid } from "@visactor/vutils";
import { calculateWholeRangeFromRangeFactor } from "./utils/utils";
export class BaseScale {
constructor() {
this._rangeFactorStart = null, this._rangeFactorEnd = null;
}
_calculateWholeRange(range) {
return this._wholeRange ? this._wholeRange : isValid(this._rangeFactorStart) && isValid(this._rangeFactorEnd) && 2 === range.length ? (this._wholeRange = calculateWholeRangeFromRangeFactor(range, [ this._rangeFactorStart, this._rangeFactorEnd ]),
this._wholeRange) : range;
}
rangeFactor(_, slience, clear) {
return _ ? (2 === _.length && _.every((r => r >= 0 && r <= 1)) && (this._wholeRange = null,
0 === _[0] && 1 === _[1] ? (this._rangeFactorStart = null, this._rangeFactorEnd = null) : (this._rangeFactorStart = _[0],
this._rangeFactorEnd = _[1])), this) : clear ? (this._wholeRange = null, this._rangeFactorStart = null,
this._rangeFactorEnd = null, this) : isValid(this._rangeFactorStart) && isValid(this._rangeFactorEnd) ? [ this._rangeFactorStart, this._rangeFactorEnd ] : null;
}
rangeFactorStart(_, slience) {
var _a;
return isNil(_) ? this._rangeFactorStart : (_ >= 0 && _ <= 1 && (this._wholeRange = null,
0 !== _ || !isNil(this._rangeFactorEnd) && 1 !== this._rangeFactorEnd ? (this._rangeFactorStart = _,
this._rangeFactorEnd = null !== (_a = this._rangeFactorEnd) && void 0 !== _a ? _a : 1) : (this._rangeFactorStart = null,
this._rangeFactorEnd = null)), this);
}
rangeFactorEnd(_, slience) {
var _a;
return isNil(_) ? this._rangeFactorEnd : (_ >= 0 && _ <= 1 && (this._wholeRange = null,
0 !== _ || !isNil(this._rangeFactorStart) && 0 !== this._rangeFactorStart ? (this._rangeFactorEnd = _,
this._rangeFactorStart = null !== (_a = this._rangeFactorStart) && void 0 !== _a ? _a : 0) : (this._rangeFactorStart = null,
this._rangeFactorEnd = null)), this);
}
generateFishEyeTransform() {
var _a;
if (!this._fishEyeOptions) return void (this._fishEyeTransform = null);
const {distortion: distortion = 2, radiusRatio: radiusRatio = .1, radius: radius} = this._fishEyeOptions, range = this.range(), first = range[0], last = range[range.length - 1], min = Math.min(first, last), max = Math.max(first, last), focus = clamp(null !== (_a = this._fishEyeOptions.focus) && void 0 !== _a ? _a : 0, min, max), rangeRadius = isNil(radius) ? (max - min) * radiusRatio : radius;
let k0 = Math.exp(distortion);
k0 = k0 / (k0 - 1) * rangeRadius;
const k1 = distortion / rangeRadius;
this._fishEyeTransform = output => {
const delta = Math.abs(output - focus);
if (delta >= rangeRadius) return output;
if (delta <= 1e-6) return focus;
const k = k0 * (1 - Math.exp(-delta * k1)) / delta * .75 + .25;
return focus + (output - focus) * k;
};
}
unknown(_) {
return arguments.length ? (this._unknown = _, this) : this._unknown;
}
get(key, defaultValue) {
var _a;
return null !== (_a = null == this ? void 0 : this[key]) && void 0 !== _a ? _a : defaultValue;
}
}
//# sourceMappingURL=base-scale.js.map