@visactor/vscale
Version:
Scales for visual encoding, used in VGrammar, VTable
38 lines (34 loc) • 1.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.ThresholdScale = void 0;
const vutils_1 = require("@visactor/vutils"), type_1 = require("./type");
class ThresholdScale {
constructor() {
this.type = type_1.ScaleEnum.Threshold, this._range = [ 0, 1 ], this._domain = [ .5 ],
this.n = 1;
}
unknown(_) {
return arguments.length ? (this._unknown = _, this) : this._unknown;
}
scale(x) {
return !(0, vutils_1.isNil)(x) && (0, vutils_1.isValidNumber)(+x) ? this._range[(0,
vutils_1.bisect)(this._domain, x, 0, this.n)] : this._unknown;
}
invertExtent(y) {
const i = this._range.indexOf(y);
return [ this._domain[i - 1], this._domain[i] ];
}
domain(_) {
return _ ? (this._domain = Array.from(_), this.n = Math.min(this._domain.length, this._range.length - 1),
this) : this._domain.slice();
}
range(_) {
return _ ? (this._range = Array.from(_), this.n = Math.min(this._domain.length, this._range.length - 1),
this) : this._range.slice();
}
clone() {
return (new ThresholdScale).domain(this._domain).range(this._range).unknown(this._unknown);
}
}
exports.ThresholdScale = ThresholdScale;