Skip to content

Commit

Permalink
fix: 修复 Choropleth 下钻 Legend 更新不同步 (#350)
Browse files Browse the repository at this point in the history
* fix: 临时修复 legend 异步数据获取情况

* fix: legend event
  • Loading branch information
lvisei authored Mar 15, 2024
1 parent 3f3e8b2 commit cc6c898
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/l7plot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@
"topojson-client": "^3.1.0"
},
"devDependencies": {
"@antv/l7": "^2.11.5",
"@antv/l7": "^2.21.0",
"@types/amap-js-api": "^1.4.6",
"@types/geojson": "^7946.0.8",
"@types/lodash-es": "^4.17.4",
"@types/topojson-client": "^3.1.0"
},
"peerDependencies": {
"@antv/l7": "^2.11.5"
"@antv/l7": "^2.21.0"
},
"publishConfig": {
"access": "public",
Expand Down
4 changes: 4 additions & 0 deletions packages/l7plot/src/core/map/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ export const LayerEventList: string[] = [
'mousedown',
'uncontextmenu',
'unpick',
// 数据映射更新,图例更新事件
'legend',
'legend:color',
'legend:size',
];
4 changes: 2 additions & 2 deletions packages/l7plot/src/core/plot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export abstract class Plot<O extends PlotOptions> extends Map<O> {
source: Source,
label?: false | LabelOptions,
plotLayerConfig?: PlotLayerOptions,
labelLayer?: TextLayer
labelLayer?: TextLayer,
) {
if (label) {
if (labelLayer) {
Expand Down Expand Up @@ -259,7 +259,7 @@ export abstract class Plot<O extends PlotOptions> extends Map<O> {
if (legend) {
setTimeout(() => {
this.updateLegendControl(legend);
});
}, 500);
}

this.emit('change-data');
Expand Down
10 changes: 7 additions & 3 deletions packages/l7plot/src/layers/area-layer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class AreaLayer extends PlotLayer<AreaLayerOptions> {
this.highlightLayer,
this.selectFillLayer,
this.selectStrokeLayer,
options
options,
);
}

Expand All @@ -146,7 +146,11 @@ export class AreaLayer extends PlotLayer<AreaLayerOptions> {
return;
}
const features = feature ? [feature] : [];
this.highlightLayer.setData({ type: 'FeatureCollection', features }, { parser: { type: 'geojson' } });
if (this.highlightLayer.getSource()) {
this.highlightLayer.setData({ type: 'FeatureCollection', features }, { parser: { type: 'geojson' } });
} else {
this.highlightLayer.source({ type: 'FeatureCollection', features: [] }, { parser: { type: 'geojson' } });
}
this.highlightLayerData = featureId;
}

Expand All @@ -155,7 +159,7 @@ export class AreaLayer extends PlotLayer<AreaLayerOptions> {
this.selectData.length === selectData.length &&
isEqual(
this.selectData.map(({ featureId }) => featureId),
selectData.map(({ featureId }) => featureId)
selectData.map(({ featureId }) => featureId),
)
) {
return;
Expand Down
26 changes: 15 additions & 11 deletions packages/l7plot/src/plots/choropleth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,6 @@ export class Choropleth extends Plot<ChoroplethOptions> {
const { data: geoData, sourceCFG } = this.parserSourceConfig(this.options.source);
this.source.setData(geoData, sourceCFG);

// 更新 legend
// TODO: 数据更新后,图层尚未执行更新,后续加图层 update 事件来解决
const legend = this.options.legend;
if (legend) {
setTimeout(() => {
this.updateLegendControl(legend);
});
}

this.emit('change-data');
}

Expand Down Expand Up @@ -241,7 +232,7 @@ export class Choropleth extends Plot<ChoroplethOptions> {
private createCountryBoundaryLayer(data: FeatureCollection, plotConfig?: ChoroplethOptions) {
const { chinaBoundaryLayer, chinaHkmBoundaryLayer, chinaDisputeBoundaryLayer } = createCountryBoundaryLayer(
data,
plotConfig
plotConfig,
);
this.chinaBoundaryLayer = chinaBoundaryLayer;
this.chinaHkmBoundaryLayer = chinaHkmBoundaryLayer;
Expand All @@ -255,7 +246,7 @@ export class Choropleth extends Plot<ChoroplethOptions> {
protected createLabelLayer(source: Source, label: LabelOptions): TextLayer {
const data = source['originData'].features
.map(({ properties }) =>
Object.assign({}, properties, { centroid: properties['centroid'] || properties['center'] })
Object.assign({}, properties, { centroid: properties['centroid'] || properties['center'] }),
)
.filter(({ centroid }) => centroid);
const { visible, minZoom, maxZoom, zIndex = 0 } = this.options;
Expand Down Expand Up @@ -325,6 +316,7 @@ export class Choropleth extends Plot<ChoroplethOptions> {
*/
protected initLayersEvent() {
this.initDrillEvent();
this.initLegendEvent();
}

/**
Expand Down Expand Up @@ -375,6 +367,18 @@ export class Choropleth extends Plot<ChoroplethOptions> {
this.fillAreaLayer.on(triggerDown, this.onDrillDownHander);
}

/**
* 初始化图例事件
*/
private initLegendEvent() {
const legend = this.options.legend;
if (!legend) return;
const onUpdateLegendData = () => {
this.updateLegendControl(legend);
};
this.fillAreaLayer.on('legend:color', onUpdateLegendData);
}

/**
* 重置钻取缓存数据
*/
Expand Down
2 changes: 1 addition & 1 deletion storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"prettier": "prettier stories -c -w"
},
"dependencies": {
"@antv/l7": "^2.11.5",
"@antv/l7": "^2.21.0",
"@antv/l7plot": "link:../packages/l7plot",
"@antv/l7-composite-layers": "link:../packages/composite-layers",
"antd": "^4.16.13",
Expand Down

0 comments on commit cc6c898

Please sign in to comment.