Skip to content

Commit ab5de8e

Browse files
authored
Merge pull request #20564 from apache/feat/geo/styling-in-geojson
feat(geo): support styling region in original GeoJSON data
2 parents 5d1d34e + 3ec7b26 commit ab5de8e

File tree

8 files changed

+86
-26
lines changed

8 files changed

+86
-26
lines changed

src/chart/map/MapSeries.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { createTooltipMarkup } from '../../component/tooltip/tooltipMarkup';
4545
import {createSymbol, ECSymbol} from '../../util/symbol';
4646
import {LegendIconParams} from '../../component/legend/LegendModel';
4747
import {Group} from '../../util/graphic';
48+
import { GeoJSONRegion } from '../../coord/geo/Region';
4849

4950
export interface MapStateOption<TCbParams = never> {
5051
itemStyle?: GeoItemStyleOption<TCbParams>
@@ -117,26 +118,36 @@ class MapSeries extends SeriesModel<MapSeriesOption> {
117118
coordDimensions: ['value'],
118119
encodeDefaulter: zrUtil.curry(makeSeriesEncodeForNameBased, this)
119120
});
120-
const dataNameMap = zrUtil.createHashMap();
121-
const toAppendNames = [] as string[];
121+
const dataNameIndexMap = zrUtil.createHashMap<number>();
122+
const toAppendItems: MapDataItemOption[] = [];
122123

123124
for (let i = 0, len = data.count(); i < len; i++) {
124125
const name = data.getName(i);
125-
dataNameMap.set(name, true);
126+
dataNameIndexMap.set(name, i);
126127
}
127128

128129
const geoSource = geoSourceManager.load(this.getMapType(), this.option.nameMap, this.option.nameProperty);
129130
zrUtil.each(geoSource.regions, function (region) {
130131
const name = region.name;
131-
if (!dataNameMap.get(name)) {
132-
toAppendNames.push(name);
132+
const dataNameIdx = dataNameIndexMap.get(name);
133+
// apply specified echarts style in GeoJSON data
134+
const specifiedGeoJSONRegionStyle = (region as GeoJSONRegion).properties
135+
&& (region as GeoJSONRegion).properties.echartsStyle;
136+
let dataItem: MapDataItemOption;
137+
if (dataNameIdx == null) {
138+
dataItem = { name: name };
139+
toAppendItems.push(dataItem);
133140
}
141+
else {
142+
dataItem = data.getRawDataItem(dataNameIdx) as MapDataItemOption;
143+
}
144+
specifiedGeoJSONRegionStyle && zrUtil.merge(dataItem, specifiedGeoJSONRegionStyle);
134145
});
135146

136147
// Complete data with missing regions. The consequent processes (like visual
137148
// map and render) can not be performed without a "full data". For example,
138149
// find `dataIndex` by name.
139-
data.appendValues([], toAppendNames);
150+
data.appendData(toAppendItems);
140151

141152
return data;
142153
}

src/component/helper/MapDraw.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
import geoSourceManager from '../../coord/geo/geoSourceManager';
3131
import {getUID} from '../../util/component';
3232
import ExtensionAPI from '../../core/ExtensionAPI';
33-
import GeoModel, { GeoCommonOptionMixin, GeoItemStyleOption, RegoinOption } from '../../coord/geo/GeoModel';
33+
import GeoModel, { GeoCommonOptionMixin, GeoItemStyleOption, RegionOption } from '../../coord/geo/GeoModel';
3434
import MapSeries, { MapDataItemOption } from '../../chart/map/MapSeries';
3535
import GlobalModel from '../../model/Global';
3636
import { Payload, ECElement, LineStyleOption, InnerFocus, DisplayState } from '../../util/types';
@@ -240,7 +240,7 @@ class MapDraw {
240240
const regionsGroupByName = this._regionsGroupByName = zrUtil.createHashMap<RegionsGroup, string>();
241241
const regionsInfoByName = zrUtil.createHashMap<{
242242
dataIdx: number;
243-
regionModel: Model<RegoinOption> | Model<MapDataItemOption>;
243+
regionModel: Model<RegionOption> | Model<MapDataItemOption>;
244244
}, string>();
245245
const regionsGroup = this._regionsGroup;
246246
const transformInfoRaw = viewBuildCtx.transformInfoRaw;
@@ -302,10 +302,6 @@ class MapDraw {
302302
? mapOrGeoModel.getRegionModel(regionName)
303303
: (data ? data.getItemModel(dataIdx) as Model<MapDataItemOption> : null);
304304

305-
// allow specified echarts style in GeoJSON data
306-
const specifiedRegionStyle = region.properties && region.properties.echartsStyle;
307-
specifiedRegionStyle && zrUtil.merge(regionModel.option, specifiedRegionStyle);
308-
309305
regionsInfoByName.set(regionName, { dataIdx, regionModel });
310306
}
311307

src/coord/geo/GeoModel.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,17 @@ interface GeoLabelFormatterDataParams {
5959
status: DisplayState;
6060
}
6161

62-
export interface RegoinOption extends GeoStateOption, StatesOptionMixin<GeoStateOption, StatesMixinBase> {
62+
export interface RegionOption extends GeoStateOption, StatesOptionMixin<GeoStateOption, StatesMixinBase> {
6363
name?: string
6464
selected?: boolean
6565
tooltip?: CommonTooltipOption<GeoTooltipFormatterParams>
6666
}
6767

68+
/**
69+
* @deprecated Use `RegionOption` instead.
70+
*/
71+
export interface RegoinOption extends RegionOption {}
72+
6873
export interface GeoTooltipFormatterParams {
6974
componentType: 'geo'
7075
geoIndex: number
@@ -118,7 +123,7 @@ export interface GeoOption extends
118123
show?: boolean;
119124
silent?: boolean;
120125

121-
regions?: RegoinOption[];
126+
regions?: RegionOption[];
122127

123128
stateAnimation?: AnimationOptionMixin
124129

@@ -137,7 +142,7 @@ class GeoModel extends ComponentModel<GeoOption> {
137142

138143
static layoutMode = 'box' as const;
139144

140-
private _optionModelMap: zrUtil.HashMap<Model<RegoinOption>>;
145+
private _optionModelMap: zrUtil.HashMap<Model<RegionOption>>;
141146

142147
static defaultOption: GeoOption = {
143148

@@ -263,7 +268,7 @@ class GeoModel extends ComponentModel<GeoOption> {
263268
/**
264269
* Get model of region.
265270
*/
266-
getRegionModel(name: string): Model<RegoinOption> {
271+
getRegionModel(name: string): Model<RegionOption> {
267272
return this._optionModelMap.get(name) || new Model(null, this, this.ecModel);
268273
}
269274

src/coord/geo/Region.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { GeoJSON, GeoProjection, GeoSVGGraphicRoot } from './geoTypes';
2525
import * as matrix from 'zrender/src/core/matrix';
2626
import type Element from 'zrender/src/Element';
2727
import { each } from 'zrender/src/core/util';
28-
import type { RegoinOption } from './GeoModel';
28+
import type { RegionOption } from './GeoModel';
2929

3030
const TMP_TRANSFORM = [] as number[];
3131

@@ -137,7 +137,7 @@ export class GeoJSONRegion extends Region {
137137

138138
// Injected outside.
139139
properties: GeoJSON['features'][0]['properties'] & {
140-
echartsStyle?: Omit<RegoinOption, 'name' | 'selected'>
140+
echartsStyle?: Omit<RegionOption, 'name'>
141141
};
142142

143143
constructor(

src/coord/geo/geoCreator.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Geo, { geo2DDimensions } from './Geo';
2222
import * as layout from '../../util/layout';
2323
import * as numberUtil from '../../util/number';
2424
import geoSourceManager from './geoSourceManager';
25-
import GeoModel, { GeoCommonOptionMixin, GeoOption, RegoinOption } from './GeoModel';
25+
import GeoModel, { GeoCommonOptionMixin, GeoOption, RegionOption } from './GeoModel';
2626
import MapSeries, { MapSeriesOption } from '../../chart/map/MapSeries';
2727
import ExtensionAPI from '../../core/ExtensionAPI';
2828
import { CoordinateSystemCreator } from '../CoordinateSystem';
@@ -34,6 +34,7 @@ import type GlobalModel from '../../model/Global';
3434
import type SeriesModel from '../../model/Series';
3535
import type ComponentModel from '../../model/Component';
3636
import * as vector from 'zrender/src/core/vector';
37+
import type { GeoJSONRegion } from './Region';
3738

3839
export type resizeGeoType = typeof resizeGeo;
3940

@@ -254,11 +255,11 @@ class GeoCreator implements CoordinateSystemCreator {
254255
* Fill given regions array
255256
*/
256257
getFilledRegions(
257-
originRegionArr: RegoinOption[],
258+
originRegionArr: RegionOption[],
258259
mapName: string,
259260
nameMap: NameMap,
260261
nameProperty: string
261-
): RegoinOption[] {
262+
): RegionOption[] {
262263
// Not use the original
263264
const regionsArr = (originRegionArr || []).slice();
264265

@@ -270,7 +271,16 @@ class GeoCreator implements CoordinateSystemCreator {
270271
const source = geoSourceManager.load(mapName, nameMap, nameProperty);
271272
zrUtil.each(source.regions, function (region) {
272273
const name = region.name;
273-
!dataNameMap.get(name) && regionsArr.push({name: name});
274+
if (!dataNameMap.get(name)) {
275+
const regionOption = {
276+
name: name
277+
};
278+
// apply specified echarts style in GeoJSON data
279+
const specifiedGeoJSONRegionStyle = (region as GeoJSONRegion).properties
280+
&& (region as GeoJSONRegion).properties.echartsStyle;
281+
specifiedGeoJSONRegionStyle && zrUtil.merge(regionOption, specifiedGeoJSONRegionStyle);
282+
regionsArr.push(regionOption);
283+
}
274284
});
275285

276286
return regionsArr;

src/data/SeriesData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ class SeriesData<
583583
* Each item is exactly corresponding to a dimension.
584584
*/
585585
appendValues(values: any[][], names?: string[]): void {
586-
const {start, end} = this._store.appendValues(values, names.length);
586+
const {start, end} = this._store.appendValues(values, names && names.length);
587587
const shouldMakeIdFromName = this._shouldMakeIdFromName();
588588

589589
this._updateOrdinalMeta();

test/geo-map.html

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/map-nested.html

Lines changed: 35 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)