1 | import { deepMix, each, every, get, isNil, isNumber } from '@antv/util';
|
2 | import { LAYER } from '../constant';
|
3 | import { IGroup } from '../dependents';
|
4 | import { AxisCfg, Condition, Datum, FacetCfg, FacetData, FacetDataFilter, Region } from '../interface';
|
5 |
|
6 | import View from '../chart/view';
|
7 | import { getAxisOption } from '../util/axis';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | export abstract class Facet<C extends FacetCfg<FacetData> = FacetCfg<FacetData>, F extends FacetData = FacetData> {
|
34 |
|
35 | public view: View;
|
36 |
|
37 | public container: IGroup;
|
38 |
|
39 | public destroyed: boolean = false;
|
40 |
|
41 |
|
42 | protected cfg: C;
|
43 |
|
44 | protected facets: F[] = [];
|
45 |
|
46 | constructor(view: View, cfg: C) {
|
47 | this.view = view;
|
48 | this.cfg = deepMix({}, this.getDefaultCfg(), cfg);
|
49 | }
|
50 |
|
51 | |
52 |
|
53 |
|
54 | public init() {
|
55 |
|
56 | if (!this.container) {
|
57 | this.container = this.createContainer();
|
58 | }
|
59 |
|
60 |
|
61 | const data = this.view.getData();
|
62 | this.facets = this.generateFacets(data);
|
63 | }
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | public render() {
|
74 | this.renderViews();
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 | public update() {
|
81 |
|
82 | }
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 | public clear() {
|
89 | this.clearFacetViews();
|
90 | }
|
91 |
|
92 | |
93 |
|
94 |
|
95 | public destroy() {
|
96 | this.clear();
|
97 |
|
98 | if (this.container) {
|
99 | this.container.remove(true);
|
100 | this.container = undefined;
|
101 | }
|
102 |
|
103 | this.destroyed = true;
|
104 | this.view = undefined;
|
105 | this.facets = [];
|
106 | }
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 | protected facetToView(facet: F): View {
|
113 | const { region, data, padding = this.cfg.padding } = facet;
|
114 |
|
115 | const view = this.view.createView({
|
116 | region,
|
117 | padding,
|
118 | });
|
119 |
|
120 |
|
121 | view.data(data || []);
|
122 | facet.view = view;
|
123 |
|
124 |
|
125 | this.beforeEachView(view, facet);
|
126 |
|
127 | const { eachView } = this.cfg;
|
128 | if (eachView) {
|
129 | eachView(view, facet);
|
130 | }
|
131 |
|
132 |
|
133 | this.afterEachView(view, facet);
|
134 |
|
135 | return view;
|
136 | }
|
137 |
|
138 |
|
139 | private createContainer(): IGroup {
|
140 | const foregroundGroup = this.view.getLayer(LAYER.FORE);
|
141 | return foregroundGroup.addGroup();
|
142 | }
|
143 |
|
144 | |
145 |
|
146 |
|
147 | private renderViews() {
|
148 | this.createFacetViews();
|
149 | }
|
150 |
|
151 | |
152 |
|
153 |
|
154 | private createFacetViews(): View[] {
|
155 |
|
156 | return this.facets.map((facet): View => {
|
157 | return this.facetToView(facet);
|
158 | });
|
159 | }
|
160 |
|
161 | |
162 |
|
163 |
|
164 | private clearFacetViews() {
|
165 |
|
166 | each(this.facets, (facet) => {
|
167 | if (facet.view) {
|
168 | this.view.removeView(facet.view);
|
169 | facet.view = undefined;
|
170 | }
|
171 | });
|
172 | }
|
173 |
|
174 | |
175 |
|
176 |
|
177 | private parseSpacing() {
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | const { width, height } = this.view.viewBBox;
|
192 | const { spacing } = this.cfg;
|
193 | return spacing.map((s: number, idx: number) => {
|
194 | if (isNumber(s)) return s / (idx === 0 ? width : height);
|
195 | else return parseFloat(s) / 100;
|
196 | });
|
197 | }
|
198 |
|
199 |
|
200 |
|
201 | |
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | protected getFieldValues(data: Datum[], field: string): string[] {
|
209 | const rst = [];
|
210 | const cache: Record<string, boolean> = {};
|
211 |
|
212 |
|
213 | each(data, (d: Datum) => {
|
214 | const value = d[field];
|
215 | if (!isNil(value) && !cache[value]) {
|
216 | rst.push(value);
|
217 | cache[value] = true;
|
218 | }
|
219 | });
|
220 |
|
221 | return rst;
|
222 | }
|
223 |
|
224 | |
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | protected getRegion(rows: number, cols: number, xIndex: number, yIndex: number): Region {
|
232 | const [xSpacing, ySpacing] = this.parseSpacing();
|
233 |
|
234 |
|
235 | |
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | const xRatio = (1 + xSpacing) / (cols === 0 ? 1 : cols) - xSpacing;
|
244 | const yRatio = (1 + ySpacing) / (rows === 0 ? 1 : rows) - ySpacing;
|
245 |
|
246 |
|
247 | const start = {
|
248 | x: (xRatio + xSpacing) * xIndex,
|
249 | y: (yRatio + ySpacing) * yIndex,
|
250 | };
|
251 | const end = {
|
252 | x: start.x + xRatio,
|
253 | y: start.y + yRatio,
|
254 | };
|
255 | return { start, end };
|
256 | }
|
257 |
|
258 | protected getDefaultCfg() {
|
259 | return {
|
260 | eachView: undefined,
|
261 | showTitle: true,
|
262 | spacing: [0, 0],
|
263 | padding: 10,
|
264 | fields: [],
|
265 | };
|
266 | }
|
267 |
|
268 | |
269 |
|
270 |
|
271 | protected getDefaultTitleCfg() {
|
272 |
|
273 | const fontFamily = this.view.getTheme().fontFamily;
|
274 | return {
|
275 | style: {
|
276 | fontSize: 14,
|
277 | fill: '#666',
|
278 | fontFamily,
|
279 | },
|
280 | };
|
281 | }
|
282 |
|
283 | |
284 |
|
285 |
|
286 |
|
287 |
|
288 | protected processAxis(view: View, facet: F) {
|
289 | const options = view.getOptions();
|
290 |
|
291 | const coordinateOption = options.coordinate;
|
292 | const geometries = view.geometries;
|
293 |
|
294 | const coordinateType = get(coordinateOption, 'type', 'rect');
|
295 |
|
296 | if (coordinateType === 'rect' && geometries.length) {
|
297 | if (isNil(options.axes)) {
|
298 |
|
299 | options.axes = {};
|
300 | }
|
301 | const axes = options.axes;
|
302 |
|
303 | const [x, y] = geometries[0].getXYFields();
|
304 |
|
305 | const xOption = getAxisOption(axes, x);
|
306 | const yOption = getAxisOption(axes, y);
|
307 |
|
308 | if (xOption !== false) {
|
309 | options.axes[x] = this.getXAxisOption(x, axes, xOption, facet);
|
310 | }
|
311 |
|
312 | if (yOption !== false) {
|
313 | options.axes[y] = this.getYAxisOption(y, axes, yOption, facet);
|
314 | }
|
315 | }
|
316 | }
|
317 |
|
318 | |
319 |
|
320 |
|
321 |
|
322 | protected getFacetDataFilter(conditions: Condition[]): FacetDataFilter {
|
323 | return (datum: Datum) => {
|
324 |
|
325 | return every(conditions, (condition) => {
|
326 | const { field, value } = condition;
|
327 |
|
328 | if (!isNil(value) && field) {
|
329 | return datum[field] === value;
|
330 | }
|
331 | return true;
|
332 | });
|
333 | };
|
334 | }
|
335 |
|
336 | |
337 |
|
338 |
|
339 |
|
340 |
|
341 | protected abstract beforeEachView(view: View, facet: F);
|
342 |
|
343 | |
344 |
|
345 |
|
346 |
|
347 |
|
348 | protected abstract afterEachView(view: View, facet: F);
|
349 |
|
350 | |
351 |
|
352 |
|
353 |
|
354 | protected abstract generateFacets(data: Datum[]): F[];
|
355 |
|
356 | |
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 | protected abstract getXAxisOption(x: string, axes: any, option: AxisCfg, facet: F): object;
|
364 |
|
365 | |
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 | protected abstract getYAxisOption(y: string, axes: any, option: AxisCfg, facet: F): object;
|
373 | }
|