/
index.ts
544 lines (478 loc) · 14.4 KB
/
index.ts
1
2
3
4
5
6
7
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import { clone, isEqual, isUndefined } from '@antv/util';
import { CompositeLayer } from '../../core/composite-layer';
import { LineLayer } from '../../core-layers/line-layer';
import { PolygonLayer } from '../../core-layers/polygon-layer';
import { TextLayer } from '../../core-layers/text-layer';
import { getDefaultState } from './adaptor';
import { ChoroplethLayerOptions, ChoroplethLayerSourceOptions } from './types';
import { ICoreLayer, ISource, MouseEvent } from '../../types';
import { DEFAULT_OPTIONS, DEFAULT_STATE, EMPTY_SOURCE } from './constants';
export type { ChoroplethLayerOptions };
export class ChoroplethLayer extends CompositeLayer<ChoroplethLayerOptions> {
/**
* 默认配置项
*/
static DefaultOptions = DEFAULT_OPTIONS;
/**
* 复合图层类型
*/
public type = CompositeLayer.LayerType.ChoroplethLayer;
/**
* 主图层
*/
protected get layer() {
return this.fillLayer;
}
/**
* 图层间共享 source 实例
*/
public source!: ISource;
/**
* 填充面图层
*/
public get fillLayer() {
return this.subLayers.getLayer('fillLayer') as ICoreLayer;
}
/**
* 描边图层
*/
public get strokeLayer() {
return this.subLayers.getLayer('strokeLayer') as ICoreLayer;
}
/**
* 高亮描边图层
*/
public get highlightStrokeLayer() {
return this.subLayers.getLayer('highlightStrokeLayer') as ICoreLayer;
}
/**
* 高亮数据
*/
private highlightData: any;
/**
* 选中填充面图层
*/
public get selectFillLayer() {
return this.subLayers.getLayer('selectFillLayer') as ICoreLayer;
}
/**
* 选中描边图层
*/
public get selectStrokeLayer() {
return this.subLayers.getLayer('selectStrokeLayer') as ICoreLayer;
}
/**
* 选中数据
*/
private selectData: { feature: any; featureId: number }[] = [];
/**
* 标注文本图层
*/
public get labelLayer() {
return this.subLayers.getLayer('labelLayer') as TextLayer;
}
/**
* 图层交互状态配置
*/
private layerState = DEFAULT_STATE;
/**
* 图层是否具有交互属性
*/
public interaction = true;
constructor(options: ChoroplethLayerOptions) {
super(options);
this.initSubLayersEvent();
}
/**
* 获取默认配置
*/
public getDefaultOptions(): Partial<ChoroplethLayerOptions> {
return ChoroplethLayer.DefaultOptions;
}
/**
* 创建子图层
*/
protected createSubLayers() {
const sourceOptions = this.options.source;
const source = this.isSourceInstance(sourceOptions) ? sourceOptions : this.createSource(sourceOptions);
this.source = source;
this.layerState = getDefaultState(this.options.state);
// 映射填充面图层
const fillLayer = new PolygonLayer({
id: 'fillLayer',
shape: 'fill',
...this.getFillLayerOptions(),
source,
});
const fillBottomColor = this.options.fillBottomColor;
fillBottomColor && fillLayer.layer.setBottomColor(fillBottomColor);
// 描边图层
const strokeLayer = new LineLayer({
id: 'strokeLayer',
shape: 'line',
...this.getStrokeLayerOptions(),
source,
});
// 高亮描边图层
const highlightStrokeLayer = new LineLayer({
id: 'highlightStrokeLayer',
...this.gethigHlightStrokeLayerOptions(),
});
// 选中填充图层
const selectFillLayer = new PolygonLayer({
id: 'selectFillLayer',
...this.getSelectFillLayerOptions(),
});
// 选中描边图层
const selectStrokeLayer = new LineLayer({
id: 'selectStrokeLayer',
...this.getSelectStrokeLayerOptions(),
});
// 标注图层
const labelLayer = new TextLayer({
id: 'labelLayer',
...this.getTextLayerOptions(),
source,
});
const subLayers = [fillLayer, strokeLayer, highlightStrokeLayer, selectFillLayer, selectStrokeLayer, labelLayer];
return subLayers;
}
/**
* 获取填充图层配置项
*/
private getFillLayerOptions() {
const { visible, minZoom, maxZoom, zIndex = 0, fillColor, opacity, ...baseConfig } = this.options;
const defaultState = this.layerState;
const fillState = {
active: defaultState.active.fillColor === false ? false : { color: defaultState.active.fillColor },
select: false,
};
const fillStyle = { opacity: opacity };
const options = {
...baseConfig,
visible,
minZoom,
maxZoom,
zIndex,
color: fillColor,
state: fillState,
style: fillStyle,
};
return options;
}
private getStrokeLayerOptions() {
const {
visible,
minZoom,
maxZoom,
zIndex = 0,
opacity,
strokeColor,
lineWidth,
lineOpacity,
lineDash,
lineType,
} = this.options;
const strokeStyle = {
opacity: isUndefined(lineOpacity) ? opacity : lineOpacity,
dashArray: lineDash,
lineType: lineType,
};
const options = {
visible,
zIndex,
minZoom,
maxZoom,
size: lineWidth,
color: strokeColor,
style: strokeStyle,
};
return options;
}
private gethigHlightStrokeLayerOptions() {
const { visible, minZoom, maxZoom, zIndex = 0, lineWidth } = this.options;
const defaultState = this.layerState;
const color = defaultState.active.strokeColor || undefined;
const size = defaultState.active.lineWidth || lineWidth;
const options = {
visible: visible && Boolean(color),
zIndex: zIndex + 0.1,
minZoom,
maxZoom,
source: EMPTY_SOURCE,
size: size,
color: color,
style: { opacity: defaultState.active.lineOpacity },
};
return options;
}
private getSelectFillLayerOptions() {
const { visible, minZoom, maxZoom, zIndex = 0, opacity } = this.options;
const defaultState = this.layerState;
const color = defaultState.select.fillColor || undefined;
const fillStyle = { opacity: opacity };
const option = {
visible: visible && Boolean(color),
zIndex: zIndex + 0.1,
minZoom,
maxZoom,
source: EMPTY_SOURCE,
color,
style: fillStyle,
state: { select: false, active: false },
};
return option;
}
private getSelectStrokeLayerOptions() {
const { visible, minZoom, maxZoom, zIndex = 0, lineWidth } = this.options;
const defaultState = this.layerState;
const color = defaultState.select.strokeColor || undefined;
const size = defaultState.select.lineWidth || lineWidth;
const option = {
visible: visible && Boolean(color),
zIndex: zIndex + 0.1,
minZoom,
maxZoom,
source: EMPTY_SOURCE,
size,
color,
style: { opacity: defaultState.select.lineOpacity },
};
return option;
}
private getTextLayerOptions() {
const { visible, minZoom, maxZoom, zIndex = 0, label } = this.options;
const options = {
zIndex: zIndex + 0.1,
minZoom,
maxZoom,
...label,
visible: visible && (label?.visible || Boolean(label)),
};
return options;
}
/**
* 设置子图层数据
*/
protected setSubLayersSource(source: ChoroplethLayerSourceOptions | ISource) {
if (this.isSourceInstance(source)) {
this.source = source;
this.fillLayer.setSource(source);
this.strokeLayer.setSource(source);
this.labelLayer.setSource(source);
} else {
const { data, ...option } = source;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.source.setData(data, option);
}
this.highlightStrokeLayer.changeData(EMPTY_SOURCE);
this.selectFillLayer.changeData(EMPTY_SOURCE);
this.selectStrokeLayer.changeData(EMPTY_SOURCE);
}
/**
* 设置高亮描边子图层数据
*/
protected setHighlightLayerSource(feature?: any, featureId = -999) {
if (this.highlightData === featureId) {
return;
}
const features = feature ? [feature] : [];
this.highlightStrokeLayer.changeData({
data: { type: 'FeatureCollection', features },
parser: this.source['parser'],
});
this.highlightData = featureId;
}
/**
* 设置选中描边与填充子图层数据
*/
protected setSelectLayerSource(selectData: any[] = []) {
if (
this.selectData.length === selectData.length &&
isEqual(
this.selectData.map(({ featureId }) => featureId),
selectData.map(({ featureId }) => featureId)
)
) {
return;
}
const features = selectData.map(({ feature }) => feature);
this.selectFillLayer.changeData({ data: { type: 'FeatureCollection', features }, parser: this.source['parser'] });
this.selectStrokeLayer.changeData({ data: { type: 'FeatureCollection', features }, parser: this.source['parser'] });
this.selectData = selectData;
}
/**
* 初始化子图层事件
*/
protected initSubLayersEvent() {
// 初始化主图层交互事件
this.fillLayer.off('mousemove', this.onHighlighHandle);
this.fillLayer.off('unmousemove', this.onHighlighHandle);
this.fillLayer.off('click', this.onSelectHandle);
this.selectData = [];
this.highlightData = null;
if (!this.options.state) return;
// active
if (this.options.state.active) {
this.fillLayer.on('mousemove', this.onHighlighHandle);
this.fillLayer.on('unmousemove', this.onUnhighlighHandle);
}
// select
if (this.options.state.select) {
this.fillLayer.on('click', this.onSelectHandle);
}
}
/**
* 图层高亮回调
*/
private onHighlighHandle = (event: MouseEvent) => {
const { feature, featureId } = event;
this.setHighlightLayerSource(feature, featureId);
};
/**
* 图层取消高亮回调
*/
private onUnhighlighHandle = () => {
this.setHighlightLayerSource();
};
/**
* 图层选中回调
*/
private onSelectHandle = (event: MouseEvent) => {
const { feature, featureId } = event;
this.handleSelectData(featureId, feature);
};
/**
* 处理选择数据私有方法
*/
private handleSelectData(featureId: number, feature: any) {
const enabledMultiSelect = this.options.enabledMultiSelect;
let selectData = clone(this.selectData);
const index = selectData.findIndex((item) => item.featureId === featureId);
if (index === -1) {
if (enabledMultiSelect) {
selectData.push({ feature, featureId });
} else {
selectData = [{ feature, featureId }];
}
this.emit('select', feature, clone(selectData));
} else {
const unselectFeature = selectData[index];
if (enabledMultiSelect) {
selectData.splice(index, 1);
} else {
selectData = [];
}
this.emit('unselect', unselectFeature, clone(selectData));
}
this.setSelectLayerSource(selectData);
}
/**
* 更新
*/
public update(options: Partial<ChoroplethLayerOptions>) {
super.update(options);
this.initSubLayersEvent();
}
/**
* 更新: 更新配置
*/
public updateOption(options: Partial<ChoroplethLayerOptions>) {
super.update(options);
this.layerState = getDefaultState(this.options.state);
}
/**
* 更新子图层
*/
protected updateSubLayers(options: Partial<ChoroplethLayerOptions>) {
// 映射填充面图层
this.fillLayer.update(this.getFillLayerOptions());
// 描边图层
this.strokeLayer.update(this.getStrokeLayerOptions());
// 高亮图层
this.highlightStrokeLayer.update(this.gethigHlightStrokeLayerOptions());
// 选中填充图层
this.selectFillLayer.update(this.getSelectFillLayerOptions());
// 选中描边图层
this.selectStrokeLayer.update(this.getSelectStrokeLayerOptions());
// 重置高亮/选中状态
if (this.options.visible) {
if (!isUndefined(options.state) && !isEqual(this.lastOptions.state, this.options.state)) {
this.updateHighlightSubLayers();
}
if (this.layerState.active.strokeColor) {
this.setHighlightLayerSource();
}
if (this.layerState.select.fillColor || this.layerState.select.strokeColor) {
this.setSelectLayerSource();
}
}
}
/**
* 更新高亮及选中子图层
*/
private updateHighlightSubLayers() {
const defaultState = this.layerState;
const lasetDefaultState = getDefaultState(this.lastOptions.state);
if (lasetDefaultState.active.strokeColor !== defaultState.active.strokeColor) {
defaultState.active.strokeColor ? this.highlightStrokeLayer.show() : this.highlightStrokeLayer.hide();
}
if (lasetDefaultState.select.fillColor !== defaultState.select.fillColor) {
defaultState.select.fillColor ? this.selectFillLayer.show() : this.selectFillLayer.hide();
}
if (lasetDefaultState.select.strokeColor !== defaultState.select.strokeColor) {
defaultState.select.strokeColor ? this.selectStrokeLayer.show() : this.selectStrokeLayer.hide();
}
}
/**
* 设置图层 zIndex
*/
public setIndex(zIndex: number) {
this.fillLayer.setIndex(zIndex);
this.strokeLayer.setIndex(zIndex);
this.highlightStrokeLayer.setIndex(zIndex + 0.1);
this.selectFillLayer.setIndex(zIndex + 0.1);
this.selectStrokeLayer.setIndex(zIndex + 0.1);
this.labelLayer.setIndex(zIndex + 0.1);
}
/**
* 设置图层高亮状态
*/
public setActive(field: string, value: number | string) {
const source = this.fillLayer.source;
const featureId = source.getFeatureId(field, value);
if (isUndefined(featureId)) {
throw new Error('Feature non-existent' + field + value);
}
if (this.layerState.active.fillColor) {
this.fillLayer.layer.setActive(featureId);
}
if (this.layerState.active.strokeColor) {
const feature = source.getFeatureById(featureId);
this.setHighlightLayerSource(feature, featureId);
}
}
/**
* 设置图层选中状态
*/
public setSelect(field: string, value: number | string) {
const source = this.fillLayer.source;
const featureId = source.getFeatureId(field, value);
if (isUndefined(featureId)) {
throw new Error('Feature non-existent' + field + value);
}
if (this.layerState.select.strokeColor === false || this.layerState.select.fillColor === false) {
return;
}
const feature = source.getFeatureById(featureId);
this.handleSelectData(featureId, feature);
// TODO: L7 method pickFeature(id|{x,y})
}
/**
* 图层框选数据
*/
public boxSelect(bounds: [number, number, number, number], callback: (...args: any[]) => void) {
this.fillLayer.boxSelect(bounds, callback);
}
}