UNPKG

@antv/f2

Version:

Charts for mobile visualization.

93 lines (92 loc) 2.9 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _tslib = require("tslib"); var _util = require("@antv/util"); var _base = _interopRequireDefault(require("../base")); /** * 分类度量 * @class */ var Category = /** @class */function (_super) { (0, _tslib.__extends)(Category, _super); function Category() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.type = 'cat'; _this.isCategory = true; return _this; } Category.prototype.buildIndexMap = function () { if (!this.translateIndexMap) { this.translateIndexMap = new Map(); // 重新构建缓存 for (var i = 0; i < this.values.length; i++) { this.translateIndexMap.set(this.values[i], i); } } }; Category.prototype.translate = function (value) { // 按需构建 map this.buildIndexMap(); // 找得到 var idx = this.translateIndexMap.get(value); if (idx === undefined) { idx = (0, _util.isNumber)(value) ? value : NaN; } return idx; }; Category.prototype.scale = function (value) { var order = this.translate(value); // 分类数据允许 0.5 范围内调整 // if (order < this.min - 0.5 || order > this.max + 0.5) { // return NaN; // } var percent = this.calcPercent(order, this.min, this.max); return this.calcValue(percent, this.rangeMin(), this.rangeMax()); }; Category.prototype.invert = function (scaledValue) { var domainRange = this.max - this.min; var percent = this.calcPercent(scaledValue, this.rangeMin(), this.rangeMax()); var idx = Math.round(domainRange * percent) + this.min; if (idx < this.min || idx > this.max) { return NaN; } return this.values[idx]; }; Category.prototype.getText = function (value) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var v = value; // value为index if ((0, _util.isNumber)(value) && !this.values.includes(value)) { v = this.values[v]; } return _super.prototype.getText.apply(this, (0, _tslib.__spreadArray)([v], args, false)); }; // 复写属性 Category.prototype.initCfg = function () { this.tickMethod = 'cat'; }; // 设置 min, max Category.prototype.setDomain = function () { // 用户有可能设置 min if ((0, _util.isNil)(this.getConfig('min'))) { this.min = 0; } if ((0, _util.isNil)(this.getConfig('max'))) { var size = this.values.length; this.max = size > 1 ? size - 1 : size; } // scale.init 的时候清除缓存 if (this.translateIndexMap) { this.translateIndexMap = undefined; } }; return Category; }(_base.default); var _default = exports.default = Category;