@antv/f2
Version:
Charts for mobile visualization.
233 lines • 6.35 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import { getScale, registerTickMethod } from '@antv/scale';
import { each, getRange, isFunction, isNil, isNumber, mix, valuesOfKey } from '@antv/util';
import CatTick from './scale/cat-tick';
import LinearTick from './scale/linear-tick';
// 覆盖0.3.x的 cat 方法
registerTickMethod('cat', CatTick);
registerTickMethod('time-cat', CatTick);
// 覆盖linear 度量的tick算法
registerTickMethod('wilkinson-extended', LinearTick);
var ScaleController = /*#__PURE__*/function () {
function ScaleController(data) {
_classCallCheck(this, ScaleController);
this.data = data;
this.options = {};
this.scales = {};
}
_createClass(ScaleController, [{
key: "_getType",
value: function _getType(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';
}
}, {
key: "_getOption",
value: function _getOption(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 _getRange = getRange(values),
min = _getRange.min,
max = _getRange.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;
}
}, {
key: "createScale",
value: function createScale(option) {
var type = option.type;
if (isFunction(type)) {
return new type(option);
}
var ScaleClass = getScale(type);
return new ScaleClass(option);
}
// 更新或创建scale
}, {
key: "setScale",
value: function setScale(field, option) {
var options = this.options,
scales = this.scales;
options[field] = mix({}, options[field], option);
// 如果scale有更新,scale 也需要重新创建
if (scales[field]) {
delete scales[field];
}
}
}, {
key: "create",
value: function create(options) {
this.update(options);
}
}, {
key: "update",
value: function update(options) {
var _this = this;
if (!options) return;
each(options, function (option, field) {
_this.setScale(field, option);
});
// 为了让外部感知到scale有变化
this.scales = _objectSpread({}, this.scales);
}
}, {
key: "changeData",
value: function changeData(data) {
this.data = data;
this.scales = {};
}
}, {
key: "getData",
value: function getData() {
return this.data;
}
}, {
key: "getScale",
value: function getScale(field) {
var scales = this.scales,
options = this.options,
data = this.data;
var scale = scales[field];
if (scale) {
return scale;
}
var option = options[field];
if (!option) {
return null;
}
var values = option.values ? option.values : data ? valuesOfKey(data, field) : [];
var scaleOption = this._getOption(_objectSpread(_objectSpread({}, option), {}, {
field: field,
values: values
}));
var newScale = this.createScale(scaleOption);
scales[field] = newScale;
return newScale;
}
}, {
key: "getScales",
value: function getScales() {
var _this2 = this;
var options = this.options,
scales = this.scales;
each(options, function (option, field) {
_this2.getScale(field);
});
return scales;
}
}, {
key: "adjustStartZero",
value: function adjustStartZero(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调整
}, {
key: "adjustPieScale",
value: function adjustPieScale(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点对位置的值
}, {
key: "getZeroValue",
value: function getZeroValue(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;