1 | import { __assign } from "tslib";
|
2 | import { each, isString, isNil, isFunction, isNumber, isArray, upperFirst } from '@antv/util';
|
3 | import * as Attrs from '../attr';
|
4 | import { isEqual } from '@antv/f-engine';
|
5 | var Identity = Attrs.Identity,
|
6 | Linear = Attrs.Linear,
|
7 | Category = Attrs.Category;
|
8 |
|
9 | export var ATTRS = ['x', 'y', 'color', 'size', 'shape'];
|
10 |
|
11 | var GROUP_ATTRS = ['color', 'size', 'shape'];
|
12 | function cloneScale(scale, scaleConfig) {
|
13 |
|
14 | return new scale.constructor(__assign(__assign({}, scale.__cfg__), scaleConfig));
|
15 | }
|
16 | var AttrController = function () {
|
17 | function AttrController(scaleController, attrsRange) {
|
18 | this.scaleController = scaleController;
|
19 | this.attrsRange = attrsRange;
|
20 | this.options = {};
|
21 | this.attrs = {};
|
22 | }
|
23 | AttrController.prototype.parseOption = function (option, attrName) {
|
24 | if (!option) {
|
25 | return {
|
26 | type: 'identity'
|
27 | };
|
28 | }
|
29 | if (isString(option)) {
|
30 | return {
|
31 | field: option,
|
32 | type: 'category'
|
33 | };
|
34 | }
|
35 | if (isNumber(option)) {
|
36 | if (attrName === 'size') {
|
37 | return {
|
38 | type: 'identity',
|
39 | field: option
|
40 | };
|
41 | }
|
42 | }
|
43 | if (isArray(option)) {
|
44 | return {
|
45 | field: option[0],
|
46 | range: option[1]
|
47 | };
|
48 | }
|
49 | return option;
|
50 | };
|
51 | AttrController.prototype.getAttrOptions = function (props, justifyContentCenter) {
|
52 | var _this = this;
|
53 | if (!props.x || !props.y) {
|
54 | throw new Error('x, y are required !');
|
55 | }
|
56 | var options = {};
|
57 | var ranges = this.attrsRange;
|
58 | ATTRS.forEach(function (attrName) {
|
59 | if (!props[attrName]) return;
|
60 | var option = _this.parseOption(props[attrName], attrName);
|
61 | if (!option.range) {
|
62 | option.range = ranges[attrName];
|
63 | }
|
64 | options[attrName] = option;
|
65 | });
|
66 |
|
67 | var x = options.x,
|
68 | y = options.y;
|
69 | x.justifyContent = justifyContentCenter;
|
70 |
|
71 | x.type = Linear;
|
72 | y.type = Linear;
|
73 | return options;
|
74 | };
|
75 | AttrController.prototype.getDefaultAttrValues = function () {
|
76 | var _a = this.attrsRange,
|
77 | color = _a.color,
|
78 | shape = _a.shape;
|
79 | return {
|
80 | color: color[0],
|
81 | shape: shape && shape[0]
|
82 | };
|
83 | };
|
84 | AttrController.prototype.getGroupScales = function () {
|
85 | var attrs = this.attrs;
|
86 | var scales = [];
|
87 | each(GROUP_ATTRS, function (attrName) {
|
88 | var attr = attrs[attrName];
|
89 | if (!attr) {
|
90 | return;
|
91 | }
|
92 | var scale = attr.scale;
|
93 | if (scale && scale.isCategory && scales.indexOf(scale) === -1) {
|
94 | scales.push(scale);
|
95 | }
|
96 | });
|
97 | return scales;
|
98 | };
|
99 | AttrController.prototype.createAttr = function (option) {
|
100 | var type = option.type,
|
101 | field = option.field,
|
102 | scaleConfig = option.scale;
|
103 | if (isNil(field) || type === Identity) {
|
104 | return new Identity(option);
|
105 | }
|
106 | var scale = this.scaleController.getScale(field);
|
107 | var attrOption = __assign(__assign({}, option), {
|
108 | data: this.scaleController.getData(),
|
109 |
|
110 | scale: scaleConfig ? cloneScale(scale, scaleConfig) : scale
|
111 | });
|
112 |
|
113 | if (scale && scale.type === 'identity') {
|
114 | return new Identity(attrOption);
|
115 | }
|
116 |
|
117 | var AttrConstructor = scale.isLinear ? Linear : Category;
|
118 |
|
119 | if (isFunction(type)) {
|
120 | AttrConstructor = type;
|
121 | }
|
122 | if (isString(type) && Attrs[upperFirst(type)]) {
|
123 | AttrConstructor = Attrs[upperFirst(type)];
|
124 | }
|
125 | return new AttrConstructor(attrOption);
|
126 | };
|
127 | AttrController.prototype.create = function (options) {
|
128 | this.update(options);
|
129 | };
|
130 | AttrController.prototype.update = function (nextOptions) {
|
131 | var _a = this,
|
132 | scaleController = _a.scaleController,
|
133 | lastOptions = _a.options,
|
134 | lastAttrs = _a.attrs;
|
135 | var nextAttrs = {};
|
136 | each(nextOptions, function (nextOption, attrName) {
|
137 | var lastOption = lastOptions[attrName];
|
138 | if (isEqual(nextOption, lastOption)) {
|
139 | nextAttrs[attrName] = lastAttrs[attrName];
|
140 | }
|
141 | var field = nextOption.field,
|
142 | justifyContent = nextOption.justifyContent;
|
143 | if (field) {
|
144 | scaleController.setScale(field, {
|
145 | justifyContent: justifyContent
|
146 | });
|
147 | }
|
148 | });
|
149 | this.options = nextOptions;
|
150 | this.attrs = nextAttrs;
|
151 | };
|
152 | AttrController.prototype.getAttr = function (attrName) {
|
153 | var _a = this,
|
154 | attrs = _a.attrs,
|
155 | options = _a.options;
|
156 | var attr = attrs[attrName];
|
157 | if (attr) {
|
158 | return attr;
|
159 | }
|
160 | var option = options[attrName];
|
161 | if (!option) {
|
162 | return null;
|
163 | }
|
164 | var newAttr = this.createAttr(option);
|
165 | attrs[attrName] = newAttr;
|
166 | return newAttr;
|
167 | };
|
168 | AttrController.prototype.getAttrs = function () {
|
169 | var _this = this;
|
170 | var _a = this,
|
171 | options = _a.options,
|
172 | attrs = _a.attrs;
|
173 | each(options, function (option, attrName) {
|
174 | _this.getAttr(attrName);
|
175 | });
|
176 | return attrs;
|
177 | };
|
178 | AttrController.prototype.isGroupAttr = function (attrName) {
|
179 | return GROUP_ATTRS.indexOf(attrName) !== -1;
|
180 | };
|
181 | AttrController.prototype.getAttrsByLinear = function () {
|
182 | var attrs = this.attrs;
|
183 | var attrNames = Object.keys(attrs);
|
184 | var linearAttrs = [];
|
185 | var nonlinearAttrs = [];
|
186 | attrNames.forEach(function (attrName) {
|
187 | if (attrName === 'x' || attrName === 'y') {
|
188 | linearAttrs.push(attrName);
|
189 | return;
|
190 | }
|
191 | var scale = attrs[attrName].scale;
|
192 | if (scale && scale.type === 'linear') {
|
193 | linearAttrs.push(attrName);
|
194 | } else {
|
195 | nonlinearAttrs.push(attrName);
|
196 | }
|
197 | });
|
198 | return {
|
199 | linearAttrs: linearAttrs,
|
200 | nonlinearAttrs: nonlinearAttrs
|
201 | };
|
202 | };
|
203 | return AttrController;
|
204 | }();
|
205 | export default AttrController; |
\ | No newline at end of file |