diff --git a/packages/layers/src/plugins/FeatureScalePlugin.ts b/packages/layers/src/plugins/FeatureScalePlugin.ts index 531d0f9c65..54c9d7e66b 100644 --- a/packages/layers/src/plugins/FeatureScalePlugin.ts +++ b/packages/layers/src/plugins/FeatureScalePlugin.ts @@ -11,6 +11,7 @@ import { ScaleTypes, StyleScaleType, TYPES, + IParserData, } from '@antv/l7-core'; import { IParseDataItem } from '@antv/l7-source'; import { extent } from 'd3-array'; @@ -50,6 +51,17 @@ export default class FeatureScalePlugin implements ILayerPlugin { private scaleOptions: IScaleOptions = {}; + private getSourceData(layer: ILayer, callback: (data: IParserData) => void) { + const source = layer.getSource(); + if (source.inited) { + callback(source.data); + } else { + source.once('sourceUpdate', () => { + callback(source.data); + }); + } + } + public apply( layer: ILayer, { @@ -59,20 +71,45 @@ export default class FeatureScalePlugin implements ILayerPlugin { layer.hooks.init.tap('FeatureScalePlugin', () => { this.scaleOptions = layer.getScaleOptions(); const attributes = styleAttributeService.getLayerStyleAttributes(); - const { dataArray } = layer.getSource().data; - if (dataArray.length === 0) { - return; - } - this.caculateScalesForAttributes(attributes || [], dataArray); + + this.getSourceData(layer, ({ dataArray }) => { + if (Array.isArray(dataArray) && dataArray.length === 0) { + return; + } else { + this.caculateScalesForAttributes(attributes || [], dataArray); + } + }); + // const { dataArray } = layer.getSource().data; + // if (dataArray.length === 0) { + // return; + // } + // this.caculateScalesForAttributes( + // attributes || [], + // dataArray as IParseDataItem[], + // ); }); // 检测数据是否需要更新 layer.hooks.beforeRenderData.tap('FeatureScalePlugin', () => { this.scaleOptions = layer.getScaleOptions(); const attributes = styleAttributeService.getLayerStyleAttributes(); - const { dataArray } = layer.getSource().data; - this.caculateScalesForAttributes(attributes || [], dataArray); - layer.layerModelNeedUpdate = true; + + this.getSourceData(layer, ({ dataArray }) => { + if (Array.isArray(dataArray) && dataArray.length === 0) { + return; + } + this.caculateScalesForAttributes(attributes || [], dataArray); + layer.layerModelNeedUpdate = true; + }); + // const { dataArray } = layer.getSource().data; + // if (dataArray.length === 0) { + // return; + // } + // this.caculateScalesForAttributes( + // attributes || [], + // dataArray + // ); + // layer.layerModelNeedUpdate = true; return true; }); @@ -83,16 +120,31 @@ export default class FeatureScalePlugin implements ILayerPlugin { this.scaleOptions = layer.getScaleOptions(); const attributes = styleAttributeService.getLayerStyleAttributes(); if (attributes) { - const { dataArray } = layer.getSource().data; - if (dataArray.length === 0) { - return; - } - const attributesToRescale = attributes.filter( - (attribute) => attribute.needRescale, - ); - if (attributesToRescale.length) { - this.caculateScalesForAttributes(attributesToRescale, dataArray); - } + this.getSourceData(layer, ({ dataArray }) => { + if (dataArray.length === 0) { + return; + } + const attributesToRescale = attributes.filter( + (attribute) => attribute.needRescale, + ); + if (attributesToRescale.length) { + this.caculateScalesForAttributes(attributesToRescale, dataArray); + } + }); + + // const { dataArray } = layer.getSource().data; + // if (dataArray.length === 0) { + // return; + // } + // const attributesToRescale = attributes.filter( + // (attribute) => attribute.needRescale, + // ); + // if (attributesToRescale.length) { + // this.caculateScalesForAttributes( + // attributesToRescale, + // dataArray, + // ); + // } } }); } diff --git a/packages/source/src/source.ts b/packages/source/src/source.ts index d10e3958d2..04d1300bfc 100644 --- a/packages/source/src/source.ts +++ b/packages/source/src/source.ts @@ -240,6 +240,14 @@ export default class Source extends EventEmitter implements ISource { * 数据解析 */ private excuteParser(): void { + // 耗时计算测试 + // let t = new Date().getTime(); + // let c = 0 + // while(c < 100000000) { + // c++ + // } + // console.log('t', new Date().getTime() - t) + const parser = this.parser; const type: string = parser.type || 'geojson'; const sourceParser = getParser(type);