fix(source & layer): setData update cfg

This commit is contained in:
thinkinggis 2020-06-16 23:33:10 +08:00
parent 5e2fd0f61c
commit c8187c166c
3 changed files with 48 additions and 24 deletions

View File

@ -434,10 +434,10 @@ export default class BaseLayer<ChildLayerStyleOptions = {}> extends EventEmitter
public setData(data: any, options?: ISourceCFG) { public setData(data: any, options?: ISourceCFG) {
if (this.inited) { if (this.inited) {
this.layerSource.setData(data); this.layerSource.setData(data, options);
} else { } else {
this.on('inited', () => { this.on('inited', () => {
this.layerSource.setData(data); this.layerSource.setData(data, options);
}); });
} }

View File

@ -58,22 +58,7 @@ export default class Source extends EventEmitter {
super(); super();
// this.rawData = cloneDeep(data); // this.rawData = cloneDeep(data);
this.originData = data; this.originData = data;
if (cfg) { this.initCfg(cfg);
if (cfg.parser) {
this.parser = cfg.parser;
}
if (cfg.transforms) {
this.transforms = cfg.transforms;
}
this.cluster = cfg.cluster || false;
if (cfg.clusterOptions) {
this.cluster = true;
this.clusterOptions = {
...this.clusterOptions,
...cfg.clusterOptions,
};
}
}
this.hooks.init.tap('parser', () => { this.hooks.init.tap('parser', () => {
this.excuteParser(); this.excuteParser();
@ -87,9 +72,10 @@ export default class Source extends EventEmitter {
this.init(); this.init();
} }
public setData(data: any) { public setData(data: any, options?: ISourceCFG) {
this.rawData = data; this.rawData = data;
this.originData = data; this.originData = data;
this.initCfg(options);
this.init(); this.init();
this.emit('update'); this.emit('update');
} }
@ -161,6 +147,24 @@ export default class Source extends EventEmitter {
return feature?._id; return feature?._id;
} }
private initCfg(cfg?: ISourceCFG) {
if (cfg) {
if (cfg.parser) {
this.parser = cfg.parser;
}
if (cfg.transforms) {
this.transforms = cfg.transforms;
}
this.cluster = cfg.cluster || false;
if (cfg.clusterOptions) {
this.cluster = true;
this.clusterOptions = {
...this.clusterOptions,
...cfg.clusterOptions,
};
}
}
}
private excuteParser(): void { private excuteParser(): void {
const parser = this.parser; const parser = this.parser;
const type: string = parser.type || 'geojson'; const type: string = parser.type || 'geojson';

View File

@ -26,10 +26,10 @@ export default class Country extends React.Component {
scene.on('loaded', () => { scene.on('loaded', () => {
const Layer = new WorldLayer(scene, { const Layer = new WorldLayer(scene, {
data: [], data: [],
bubble: { joinBy: ['SOC', 'SOC'],
enable: false, fill: {
color: { color: {
field: 'NAME_CHN', field: 'value',
values: [ values: [
'#feedde', '#feedde',
'#fdd0a2', '#fdd0a2',
@ -47,12 +47,32 @@ export default class Country extends React.Component {
field: 'NAME_CHN', field: 'NAME_CHN',
}, },
popup: { popup: {
enable: false, enable: true,
Html: (props: any) => { Html: (props: any) => {
return `<span>${props.NAME_CHN}</span>`; return `<span>${props.NAME_CHN + ':' + props.value}</span>`;
}, },
}, },
}); });
Layer.on('loaded', () => {
console.log('完成');
Layer.updateData(
[
{
SOC: 'CHN',
value: 3000,
},
{
SOC: 'CAN',
value: 5000,
},
{
SOC: 'RUS',
value: 4000,
},
],
['SOC', 'SOC'],
);
});
}); });
this.scene = scene; this.scene = scene;
} }