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) {
if (this.inited) {
this.layerSource.setData(data);
this.layerSource.setData(data, options);
} else {
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();
// this.rawData = cloneDeep(data);
this.originData = data;
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,
};
}
}
this.initCfg(cfg);
this.hooks.init.tap('parser', () => {
this.excuteParser();
@ -87,9 +72,10 @@ export default class Source extends EventEmitter {
this.init();
}
public setData(data: any) {
public setData(data: any, options?: ISourceCFG) {
this.rawData = data;
this.originData = data;
this.initCfg(options);
this.init();
this.emit('update');
}
@ -161,6 +147,24 @@ export default class Source extends EventEmitter {
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 {
const parser = this.parser;
const type: string = parser.type || 'geojson';

View File

@ -26,10 +26,10 @@ export default class Country extends React.Component {
scene.on('loaded', () => {
const Layer = new WorldLayer(scene, {
data: [],
bubble: {
enable: false,
joinBy: ['SOC', 'SOC'],
fill: {
color: {
field: 'NAME_CHN',
field: 'value',
values: [
'#feedde',
'#fdd0a2',
@ -47,12 +47,32 @@ export default class Country extends React.Component {
field: 'NAME_CHN',
},
popup: {
enable: false,
enable: true,
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;
}