UNPKG

@antv/f2

Version:

Charts for mobile visualization.

231 lines (230 loc) 6.49 kB
import { __assign } from "tslib"; import { each, getRange, isFunction, isNil, isNumber, mix, valuesOfKey } from '@antv/util'; import { getScale, registerTickMethod, registerScale, Category, Identity, Linear, Log, Pow, Time, TimeCat, Quantize, Quantile } from '../deps/f2-scale/src'; import CatTick from './tick/cat-tick'; import LinearTick from './tick/linear-tick'; registerScale('cat', Category); registerScale('category', Category); registerScale('identity', Identity); registerScale('linear', Linear); registerScale('log', Log); registerScale('pow', Pow); registerScale('time', Time); registerScale('timeCat', TimeCat); registerScale('quantize', Quantize); registerScale('quantile', Quantile); // 覆盖0.3.x的 cat 方法 registerTickMethod('cat', CatTick); registerTickMethod('time-cat', CatTick); // 覆盖linear 度量的tick算法 registerTickMethod('wilkinson-extended', LinearTick); var ScaleController = /** @class */function () { function ScaleController(data) { this.data = data; this.options = {}; this.scales = {}; } ScaleController.prototype._getType = function (option) { var type = option.type, values = option.values, field = option.field; if (type) { return type; } if (isNumber(field) || isNil(values[0]) && field) { return 'identity'; } if (typeof values[0] === 'number') { return 'linear'; } return 'cat'; }; ScaleController.prototype._getOption = function (option) { var values = option.values, field = option.field, justifyContent = option.justifyContent; var type = this._getType(option); option.type = type; // identity if (type === 'identity') { option.field = field.toString(); option.values = [field]; return option; } // linear 类型 if (type === 'linear') { // 设置默认nice if (typeof option.nice !== 'boolean') { option.nice = true; } // 重置最大最小 var _a = getRange(values), min = _a.min, max = _a.max; if (isNil(option.min)) { option.min = min; } if (isNil(option.max)) { option.max = max; } option.values = values.sort(function (a, b) { return a - b; }); return option; } // 分类类型和 timeCat 类型,调整 range if (type === 'cat' || type === 'timeCat') { if (option.range) { return option; } var count = values.length; var range = [0, 1]; // 如果只有一项,显示在中间 if (count === 1) { range = [0.5, 1]; } else if (justifyContent) { // 居中 var offset = 1 / count * 0.5; range = [offset, 1 - offset]; } else { // 最后留 1 / count var offset = 1 / count; range = [0, 1 - offset]; } option.range = range; } return option; }; ScaleController.prototype.createScale = function (option) { var type = option.type; if (isFunction(type)) { return new type(option); } var ScaleClass = getScale(type); return new ScaleClass(option); }; // 更新或创建scale ScaleController.prototype.setScale = function (field, option) { var _a = this, options = _a.options, scales = _a.scales; options[field] = mix({}, options[field], option); // 如果scale有更新,scale 也需要重新创建 if (scales[field]) { scales[field].change(options[field]); // delete scales[field]; } }; ScaleController.prototype.create = function (options) { this.update(options); }; ScaleController.prototype.update = function (options) { var _this = this; if (!options) return; each(options, function (option, field) { _this.setScale(field, option); }); }; ScaleController.prototype.changeData = function (data) { this.data = data; this.scales = {}; }; ScaleController.prototype.getData = function () { return this.data; }; ScaleController.prototype.getScale = function (field) { var _a = this, scales = _a.scales, options = _a.options, data = _a.data; var scale = scales[field]; if (scale) { // for adjust=dodge, 需要更新 range var option_1 = this._getOption(__assign(__assign({}, options[field]), { values: scale.values })); if (option_1.range) { scale.range = option_1.range; } return scale; } var option = options[field]; if (!option) { return null; } var values = option.values ? option.values : data ? valuesOfKey(data, field) : []; var scaleOption = this._getOption(__assign(__assign({}, option), { field: field, values: values })); var newScale = this.createScale(scaleOption); scales[field] = newScale; return newScale; }; ScaleController.prototype.getScales = function () { var _this = this; var _a = this, options = _a.options, scales = _a.scales; each(options, function (option, field) { _this.getScale(field); }); return scales; }; ScaleController.prototype.getOptions = function () { var scales = this.scales; var options = {}; each(scales, function (scale, field) { options[field] = __assign({}, scale.__cfg__); }); return options; }; ScaleController.prototype.adjustStartZero = function (scale) { var options = this.options; var field = scale.field, min = scale.min, max = scale.max; var option = options[field]; // 如果有定义,则不处理 if (option && option.min) { return; } if (min > 0) { scale.change({ min: 0 }); } else if (max < 0) { scale.change({ max: 0 }); } }; // 饼图下的scale调整 ScaleController.prototype.adjustPieScale = function (scale) { var options = this.options; var field = scale.field; var option = options[field]; if (option && !isNil(option.nice)) { return null; } scale.change({ nice: false }); }; // 获取scale 在 0点对位置的值 ScaleController.prototype.getZeroValue = function (scale) { var min = scale.min, max = scale.max; var value; if (min >= 0) { value = min; } else if (max <= 0) { value = max; } else { value = 0; } return scale.scale(value); }; return ScaleController; }(); export default ScaleController;