@visactor/vscale
Version:
Scales for visual encoding, used in VGrammar, VTable
31 lines (29 loc) • 1.1 kB
JavaScript
import { isNil, isValidNumber, bisect } from "@visactor/vutils";
import { ScaleEnum } from "./type";
export class ThresholdScale {
constructor() {
this.type = ScaleEnum.Threshold, this._range = [ 0, 1 ], this._domain = [ .5 ],
this.n = 1;
}
unknown(_) {
return arguments.length ? (this._unknown = _, this) : this._unknown;
}
scale(x) {
return !isNil(x) && isValidNumber(+x) ? this._range[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);
}
}