• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

antvis / L7Plot / 9478568050

12 Jun 2024 07:33AM UTC coverage: 56.968% (-2.6%) from 59.55%
9478568050

push

github

web-flow
chore: type mis (#356)

* chore: type mis

* chore: compiler options target

960 of 2222 branches covered (43.2%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 1 file covered. (100.0%)

916 existing lines in 53 files now uncovered.

2768 of 4322 relevant lines covered (64.04%)

234.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

39.32
/packages/l7plot/src/plot/index.ts
1
import { isUndefined } from '@antv/util';
2
import { Map } from '../core/map';
124✔
3
import { Plot } from '../core/plot';
124✔
4
import { deepAssign } from '../utils';
124✔
5
import { L7PlotOptions, IPlotLayer } from '../types';
124✔
6
import { LayerGroup } from '../core/layer/layer-group';
124✔
7
import { LayerConfigType, LAYERS_MAP, PlotConfigType, PLOTS_MAP } from './types';
124✔
8

124✔
9
const DEFAULT_OPTIONS: Partial<L7PlotOptions> = {};
124✔
10

124✔
11
export class L7Plot extends Map<L7PlotOptions> {
12
  /**
13
   * 默认的 options 配置项
2✔
14
   */
15
  static DefaultOptions = DEFAULT_OPTIONS;
16

17
  /**
2✔
18
   * 图表实例
2✔
19
   */
2✔
20
  public plots: Plot<PlotConfigType>[] = [];
2✔
21

2✔
22
  constructor(container: string | HTMLDivElement, options: L7PlotOptions) {
2✔
23
    super(options);
2✔
24
    this.container = this.createContainer(container);
25

26
    this.theme = this.createTheme();
27
    this.scene = this.createScene();
28

29
    this.registerResources();
2✔
30
    this.render();
31
    this.inited = true;
32
  }
33

34
  /**
35
   * 获取默认配置
2✔
36
   */
2!
37
  protected getDefaultOptions(): Partial<L7PlotOptions> {
2✔
UNCOV
38
    return deepAssign({}, Map.DefaultOptions, L7Plot.DefaultOptions);
×
UNCOV
39
  }
×
40

41
  /**
2✔
42
   * 创建所有图层
43
   */
44
  protected createLayers(): LayerGroup {
45
    const layerGroup = new LayerGroup([]);
46

UNCOV
47
    const layers = this.options.layers || [];
×
UNCOV
48
    for (let index = 0; index < layers.length; index++) {
×
49
      const layer: IPlotLayer = this.createLayer(layers[index]);
×
50
      layerGroup.addLayer(layer);
×
51
    }
52

×
UNCOV
53
    return layerGroup;
×
54
  }
55

56
  /**
57
   * 创建图层实例
58
   */
UNCOV
59
  private createLayer(layerConfig: LayerConfigType) {
×
UNCOV
60
    const { type, ...options } = layerConfig;
×
61
    const LayerClass = LAYERS_MAP[type];
62
    if (isUndefined(LayerClass)) {
×
UNCOV
63
      throw new Error(`Don't exist ${type} layer`);
×
64
    }
65
    const layer: IPlotLayer = new (LayerClass as any)(options);
UNCOV
66
    return layer;
×
UNCOV
67
  }
×
68

69
  /**
70
   * 添加图层
71
   */
72
  public addLayer(layer: LayerConfigType | IPlotLayer) {
73
    const isLayerClass = (layer: LayerConfigType | IPlotLayer): layer is IPlotLayer => {
UNCOV
74
      return typeof layer['render'] === 'function';
×
UNCOV
75
    };
×
76
    if (isLayerClass(layer)) {
×
77
      super.addLayer(layer);
78
    } else {
×
79
      const plotLayer = this.createLayer(layer);
80
      super.addLayer(plotLayer);
81
    }
82
  }
83

84
  /**
85
   * 移除图层
86
   */
87
  public removeLayerByName(name: string) {
88
    const layer = this.layerGroup.getLayerByName(name);
89
    if (layer) {
90
      return this.layerGroup.removeLayer(layer);
91
    }
92
    return false;
93
  }
94

95
  /**
96
   * 更新图层
97
   */
98
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
2✔
99
  protected updateLayers(layers: LayerConfigType[]) {
2!
UNCOV
100
    //
×
UNCOV
101
  }
×
UNCOV
102

×
103
  /**
×
104
   * 更新 Plot
105
   */
106
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
2✔
107
  protected updatePlots(plots: PlotConfigType[]) {
2!
UNCOV
108
    //
×
109
  }
110

111
  /**
2✔
112
   * 渲染
2✔
113
   */
114
  public render() {
115
    const layerGroup = this.createLayers();
116
    if (this.inited) {
117
      this.layerGroup.removeAllLayer();
118
      layerGroup.addTo(this.scene);
119
      this.layerGroup = layerGroup;
120
      this.updateControls();
121
    } else {
2✔
122
      this.layerGroup = layerGroup;
2!
123
      if (this.scene['sceneService'].loaded) {
2✔
124
        this.onSceneLoaded();
125
      } else {
UNCOV
126
        this.scene.once('loaded', () => {
×
UNCOV
127
          this.onSceneLoaded();
×
128
        });
129
      }
130
    }
2✔
131
  }
132

133
  /**
134
   * scene 加载成功回调
135
   */
136
  private onSceneLoaded() {
2✔
137
    this.sceneLoaded = true;
2✔
138

2✔
139
    if (this.layerGroup.isEmpty()) {
2✔
140
      this.onLayersLoaded();
2✔
141
    } else {
142
      this.layerGroup.once('inited-all', () => {
143
        this.onLayersLoaded();
144
      });
145
    }
146

2!
147
    this.layerGroup.addTo(this.scene);
2✔
UNCOV
148
  }
×
UNCOV
149

×
UNCOV
150
  /**
×
151
   * 图层加载成功回调
152
   */
153
  private onLayersLoaded() {
154
    this.layersLoaded = true;
155
    this.renderPlots();
156
    this.initControls();
157
    this.loaded = true;
UNCOV
158
    this.emit('loaded');
×
UNCOV
159
  }
×
UNCOV
160

×
UNCOV
161
  /**
×
162
   * 渲染 plots
163
   */
×
164
  private renderPlots() {
×
165
    const plots = this.options.plots || [];
×
166
    for (let index = 0; index < plots.length; index++) {
167
      const plot = plots[index];
168
      const plotInstance = this.createPlot(plot);
169
      this.plots.push(plotInstance);
170
    }
171
  }
UNCOV
172

×
UNCOV
173
  /**
×
174
   * 创建 plot
175
   */
176
  private createPlot(plot: PlotConfigType) {
177
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
178
    const { type, legend, layerMenu, ...options } = plot;
UNCOV
179
    const PlotClass = PLOTS_MAP[type];
×
180
    if (isUndefined(PlotClass)) {
181
      throw new Error(`Don't exist ${type} plot`);
182
    }
183
    const plotInstance: Plot<PlotConfigType> = new (PlotClass as any)(options);
184
    plotInstance.attachToScene(this.scene, this.theme);
UNCOV
185
    return plotInstance;
×
186
  }
187

188
  /**
189
   * 添加图表
190
   */
UNCOV
191
  public addPlot(plotConfig: PlotConfigType) {
×
UNCOV
192
    // TODO: duplicate plot
×
UNCOV
193
    const plotInstance = this.createPlot(plotConfig);
×
UNCOV
194
    this.plots.push(plotInstance);
×
195
  }
×
196

×
197
  /**
198
   * 获取所有图表
199
   */
200
  public getPlots(): Plot<PlotConfigType>[] {
201
    return this.plots;
UNCOV
202
  }
×
UNCOV
203

×
204
  /**
UNCOV
205
   * 根据图表名称获取图表
×
206
   */
207
  public getPlotByName(name: string): Plot<PlotConfigType> | undefined {
208
    return this.plots.find((plot) => plot.options?.name === name);
124✔
209
  }
210

211
  /**
212
   * 根据图表名称移除图表
124✔
213
   */
214
  public removePlotByName(name: string) {
215
    const layerIndex = this.plots.findIndex((plot) => plot.options?.name === name);
216
    if (layerIndex === -1) return false;
217
    const [plot] = this.plots.splice(layerIndex, 1);
218
    plot.unattachFromScene();
219
    return true;
220
  }
221

222
  /**
223
   * 移除所有的图表
224
   */
225
  public removeAllPlot() {
226
    this.plots.forEach((plot) => {
227
      plot.unattachFromScene();
228
    });
229
    this.plots = [];
230
  }
231
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc