UNPKG

@antv/f2

Version:

Charts for mobile visualization.

239 lines (238 loc) 8.33 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); var _util = require("@antv/util"); var Attrs = _interopRequireWildcard(require("../attr")); var _equal = _interopRequireDefault(require("../base/equal")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } var Identity = Attrs.Identity, Linear = Attrs.Linear, Category = Attrs.Category; // 需要映射的属性名 var ATTRS = ['x', 'y', 'color', 'size', 'shape']; // 分组处理的属性 var GROUP_ATTRS = ['color', 'size', 'shape']; function cloneScale(scale, scaleConfig) { // @ts-ignore return new scale.constructor((0, _objectSpread2.default)((0, _objectSpread2.default)({}, scale.__cfg__), scaleConfig)); } var AttrController = /*#__PURE__*/function () { function AttrController(scaleController, attrsRange) { (0, _classCallCheck2.default)(this, AttrController); this.scaleController = scaleController; this.attrsRange = attrsRange; this.options = {}; this.attrs = {}; } (0, _createClass2.default)(AttrController, [{ key: "parseOption", value: function parseOption(option, attrName) { if (!option) { return { type: 'identity' }; } if ((0, _util.isString)(option)) { return { field: option, type: 'category' }; } if ((0, _util.isNumber)(option)) { if (attrName === 'size') { return { type: 'identity', field: option }; } } if ((0, _util.isArray)(option)) { return { field: option[0], range: option[1] }; } return option; } }, { key: "getAttrOptions", value: function getAttrOptions(props, justifyContentCenter) { var _this = this; if (!props.x || !props.y) { throw new Error('x, y are required !'); } var options = {}; var ranges = this.attrsRange; ATTRS.forEach(function (attrName) { if (!props[attrName]) return; var option = _this.parseOption(props[attrName], attrName); if (!option.range) { option.range = ranges[attrName]; } options[attrName] = option; }); // @ts-ignore var x = options.x, y = options.y; x.justifyContent = justifyContentCenter; // x, y 都是固定Linear 映射 x.type = Linear; y.type = Linear; return options; } }, { key: "getDefaultAttrValues", value: function getDefaultAttrValues() { var _this$attrsRange = this.attrsRange, color = _this$attrsRange.color, shape = _this$attrsRange.shape; return { color: color[0], shape: shape && shape[0] }; } }, { key: "getGroupScales", value: function getGroupScales() { var attrs = this.attrs; var scales = []; (0, _util.each)(GROUP_ATTRS, function (attrName) { var attr = attrs[attrName]; if (!attr) { return; } var scale = attr.scale; if (scale && scale.isCategory && scales.indexOf(scale) === -1) { scales.push(scale); } }); return scales; } }, { key: "createAttr", value: function createAttr(option) { var type = option.type, field = option.field, scaleConfig = option.scale; if ((0, _util.isNil)(field) || type === Identity) { return new Identity(option); } var scale = this.scaleController.getScale(field); var attrOption = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, option), {}, { data: this.scaleController.getData(), // scaleConfig 只在属性映射中生效 scale: scaleConfig ? cloneScale(scale, scaleConfig) : scale }); // identity if (scale && scale.type === 'identity') { return new Identity(attrOption); } // Attr的默认类型和scale类型保持一致 var AttrConstructor = scale.isLinear ? Linear : Category; // custom Attr Constructor if ((0, _util.isFunction)(type)) { AttrConstructor = type; } if ((0, _util.isString)(type) && Attrs[(0, _util.upperFirst)(type)]) { AttrConstructor = Attrs[(0, _util.upperFirst)(type)]; } return new AttrConstructor(attrOption); } }, { key: "create", value: function create(options) { this.update(options); } }, { key: "update", value: function update(nextOptions) { var scaleController = this.scaleController, lastOptions = this.options, lastAttrs = this.attrs; var nextAttrs = {}; (0, _util.each)(nextOptions, function (nextOption, attrName) { var lastOption = lastOptions[attrName]; if ((0, _equal.default)(nextOption, lastOption)) { nextAttrs[attrName] = lastAttrs[attrName]; } var field = nextOption.field, justifyContent = nextOption.justifyContent; if (field) { scaleController.setScale(field, { justifyContent: justifyContent }); } }); this.options = nextOptions; this.attrs = nextAttrs; } }, { key: "getAttr", value: function getAttr(attrName) { var attrs = this.attrs, options = this.options; var attr = attrs[attrName]; if (attr) { return attr; } var option = options[attrName]; if (!option) { return null; } var newAttr = this.createAttr(option); attrs[attrName] = newAttr; return newAttr; } }, { key: "getAttrs", value: function getAttrs() { var _this2 = this; var options = this.options, attrs = this.attrs; (0, _util.each)(options, function (option, attrName) { _this2.getAttr(attrName); }); return attrs; } }, { key: "isGroupAttr", value: function isGroupAttr(attrName) { return GROUP_ATTRS.indexOf(attrName) !== -1; } }, { key: "getAttrsByLinear", value: function getAttrsByLinear() { var attrs = this.attrs; var attrNames = Object.keys(attrs); var linearAttrs = []; var nonlinearAttrs = []; attrNames.forEach(function (attrName) { if (attrName === 'x' || attrName === 'y') { linearAttrs.push(attrName); return; } var scale = attrs[attrName].scale; if (scale && scale.type === 'linear') { linearAttrs.push(attrName); } else { nonlinearAttrs.push(attrName); } }); return { linearAttrs: linearAttrs, nonlinearAttrs: nonlinearAttrs }; } }]); return AttrController; }(); var _default = AttrController; exports.default = _default;