UNPKG

@visactor/vscale

Version:

Scales for visual encoding, used in VGrammar, VTable

71 lines (66 loc) 2.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: !0 }), exports.OrdinalScale = exports.implicit = void 0; const type_1 = require("./type"), base_scale_1 = require("./base-scale"), vutils_1 = require("@visactor/vutils"); exports.implicit = Symbol("implicit"); class OrdinalScale extends base_scale_1.BaseScale { specified(_) { var _a; return _ ? (this._specified = Object.assign(null !== (_a = this._specified) && void 0 !== _a ? _a : {}, _), this) : Object.assign({}, this._specified); } _getSpecifiedValue(input) { if (this._specified) return this._specified[input]; } constructor() { super(), this.type = type_1.ScaleEnum.Ordinal, this._index = new Map, this._domain = [], this._ordinalRange = [], this._unknown = exports.implicit; } clone() { const s = (new OrdinalScale).domain(this._domain).range(this._ordinalRange).unknown(this._unknown); return this._specified && s.specified(this._specified), s; } calculateVisibleDomain(range) { if ((0, vutils_1.isValid)(this._rangeFactorStart) && (0, vutils_1.isValid)(this._rangeFactorEnd) && 2 === range.length) { return [ this.invert(range[0]), this.invert(range[1]) ]; } return this._domain; } scale(d) { const key = `${d}`, special = this._getSpecifiedValue(key); if (void 0 !== special) return special; let i = this._index.get(key); if (!i) { if (this._unknown !== exports.implicit) return this._unknown; i = this._domain.push(d), this._index.set(key, i); } const output = this._ordinalRange[(i - 1) % this._ordinalRange.length]; return this._fishEyeTransform ? this._fishEyeTransform(output) : output; } invert(d) { let i = 0; for (;i < this._ordinalRange.length && this._ordinalRange[i] !== d; ) i++; return this._domain[(i - 1) % this._domain.length]; } domain(_) { if (!_) return this._domain.slice(); this._domain = [], this._index = new Map; for (const value of _) { const key = `${value}`; this._index.has(key) || this._index.set(key, this._domain.push(value)); } return this; } range(_) { if (!_) return this._ordinalRange.slice(); const nextRange = Array.from(_); return this._ordinalRange = nextRange, this; } index(x) { var _a; return this._index && null !== (_a = this._index.get(`${x}`)) && void 0 !== _a ? _a : -1; } } exports.OrdinalScale = OrdinalScale; //# sourceMappingURL=ordinal-scale.js.map