Skip to content

Commit 6689f9a

Browse files
committed
fix:MS线、面数据,打开标签,切换一个标签符号,打开数据驱动,对接后图例报错
AI-GEN: 100% gpt-6.1-sol
1 parent 0ebc128 commit 6689f9a

2 files changed

Lines changed: 107 additions & 10 deletions

File tree

‎src/common/mapping/WebMapV3.js‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -858,11 +858,15 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
858858
themeField = sourceInfo.clusterField;
859859
}
860860
const nextLayer = Object.assign({}, layerFromMapInfo, { title: layer.title, themeField });
861-
const styleSettings = this._parseRendererStyleData(renderer);
861+
// 图例附加的标签样式不能写回工程中的 renderer。
862+
const styleSettings = [...this._parseRendererStyleData(renderer)];
862863
// 线面文本标签
863864
if (label) {
864865
styleSettings.push({...label, type: 'text'});
865-
if (label.symbolsContent && label.symbolsContent.value.symbolId) {
866+
// 数据驱动符号使用 values/defaultValue,不存在普通符号的 value。
867+
const { symbolsContent } = label;
868+
if (symbolsContent && symbolsContent.type !== 'custom' &&
869+
(symbolsContent.type !== 'simple' || (symbolsContent.value && symbolsContent.value.symbolId))) {
866870
styleSettings.push({...label, type: 'symbol'});
867871
}
868872
}
@@ -1052,7 +1056,8 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
10521056
}
10531057

10541058
_isAllPictureSymbolSaved(symbolType, symbolsContent, spriteJson) {
1055-
if (!symbolsContent) {
1059+
if (!symbolsContent || symbolsContent.type === 'custom') {
1060+
// 自定义表达式无法静态判断是否全部为图片符号。
10561061
return false;
10571062
}
10581063
if (symbolType === 'point') {
@@ -1062,10 +1067,6 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
10621067
if (currentType === 'simple') {
10631068
return !!this._getImageIdFromValue(symbolsContent.value.style, SymbolType[symbolType]).length;
10641069
}
1065-
if(currentType === 'custom') {
1066-
// 表达式暂不支持图例显示
1067-
return false;
1068-
}
10691070
const styles = symbolsContent.values.map((v) => v.value).concat(symbolsContent.defaultValue);
10701071
return styles.every((v) => {
10711072
return !!this._getImageIdFromValue(v.style, SymbolType[symbolType]).length;
@@ -1138,8 +1139,10 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
11381139
} else {
11391140
// 其他
11401141
// isAllPic: 如果符号为图片,图例中将不再显示颜色
1141-
const symbolTypes = MAP_LAYER_TYPE_2_SYMBOL_TYPE[layer.type];
1142-
const isAllPic = this._isAllPictureSymbolSaved(symbolTypes, styleSetting.symbolsContent, this._spriteDatas);
1142+
// 线面标签中的图标按点符号处理;文本不参与图片符号判断。
1143+
const symbolTypes = MAP_LAYER_TYPE_2_SYMBOL_TYPE[renderType];
1144+
const isAllPic = legendRenderType !== LEGEND_RENDER_TYPE.TEXT &&
1145+
this._isAllPictureSymbolSaved(symbolTypes, styleSetting.symbolsContent, this._spriteDatas);
11431146
if(isAllPic) {
11441147
dataKeys = dataKeys.filter((key) => key !== 'color')
11451148
}
@@ -1525,7 +1528,8 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
15251528
}
15261529
return {
15271530
lineWidth: ((newSymbol || {}).paint || {})[LINE_WIDTH_KEY] || paint[LINE_WIDTH_KEY] || defaultStyle.height,
1528-
opacity: customValue.opacity || paint['line-opacity'] || defaultStyle.opacity,
1531+
// 0 是合法的线不透明度,用 ?? 而不是 ||,避免透明线被回退成不透明
1532+
opacity: customValue.opacity ?? paint['line-opacity'] ?? defaultStyle.opacity,
15291533
color: customValue.color || paint['line-color'] || defaultStyle.backgroundColor,
15301534
lineDash: paint['line-dasharray'] || [],
15311535
lineOffset: paint['line-offset'] || 0

‎test/common/mapping/WebMapV3Spec.js‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,75 @@ describe('WebMapV3 - _createLegendInfo', () => {
16691669
expect(instance._legendList[0].themeField).toBe('CLUSTER_COUNT');
16701670
});
16711671

1672+
['line', 'fill'].forEach((layerType) => {
1673+
['unique', 'range', 'simple', 'empty', 'custom'].forEach((symbolMode) => {
1674+
it(`should preserve ${layerType} legends with ${symbolMode} label symbols`, () => {
1675+
const { instance } = createTestContext();
1676+
const symbol = { symbolId: 'shape1', style: { layout: { 'icon-image': 'shape1' } } };
1677+
const symbolsContent = symbolMode === 'unique' || symbolMode === 'range' ? {
1678+
type: symbolMode,
1679+
field: ['smpid'],
1680+
defaultValue: symbol,
1681+
values: symbolMode === 'unique'
1682+
? [{ key: 1, value: symbol }, { key: 10, value: symbol }]
1683+
: [{ start: 0, end: 10, value: symbol }, { start: 10, end: 20, value: symbol }],
1684+
interpolateInfo: { type: 'custom' }
1685+
} : symbolMode === 'custom'
1686+
? { type: 'custom', value: ['get', 'symbol'] }
1687+
: { type: 'simple', value: symbolMode === 'empty' ? { symbolId: '' } : symbol };
1688+
const renderer = [{
1689+
color: { type: 'simple', value: '#4CC8A3' },
1690+
opacity: { type: 'simple', value: 0 },
1691+
width: { type: 'simple', value: 2 }
1692+
}];
1693+
const label = {
1694+
symbolsContent,
1695+
textField: { type: 'simple', value: '标准名称' },
1696+
textColor: { type: 'simple', value: '#FFFFFF' },
1697+
textSize: { type: 'simple', value: 16 },
1698+
color: { type: 'simple', value: '#EE4D5A' },
1699+
size: { type: 'simple', value: 8 },
1700+
opacity: { type: 'simple', value: 0.9 }
1701+
};
1702+
instance._legendList = [];
1703+
instance._mapInfo = {
1704+
layers: [{ id: 'l1', type: layerType, source: 's1' }],
1705+
sources: { s1: {} }
1706+
};
1707+
instance._mapResourceInfo = { catalogs: [legendCatalog(renderer, { visualization: { renderer, label } })] };
1708+
instance._spriteDatas = { shape1: { x: 0, y: 0, width: 20, height: 20, sdf: false } };
1709+
instance._sprite = 'http://localhost/sprite';
1710+
1711+
instance._createLegendInfo();
1712+
1713+
const legends = instance._legendList;
1714+
expect(legends[0].styleGroup[0].style.shape).toBe(layerType === 'line' ? 'LINE' : 'RECTANGLE');
1715+
expect(legends.some((item) => item.styleGroup.some((entry) => entry.style.shape === 'TEXT'))).toBe(true);
1716+
if (symbolMode === 'unique' || symbolMode === 'range') {
1717+
const symbolLegend = legends.find((item) => item.styleField === 'symbolsContent');
1718+
expect(symbolLegend.themeField).toBe('smpid');
1719+
expect(symbolLegend.styleGroup.length).toBe(3);
1720+
expect(symbolLegend.styleGroup.every((entry) => entry.style.shape === 'POINT')).toBe(true);
1721+
if (symbolMode === 'unique') {
1722+
expect(symbolLegend.styleGroup.map((entry) => entry.fieldValue)).toEqual([1, 10, null]);
1723+
} else {
1724+
expect(symbolLegend.styleGroup.map((entry) => [entry.start, entry.end])).toEqual([[0, 10], [10, 20], [null, null]]);
1725+
}
1726+
} else if (symbolMode === 'simple') {
1727+
expect(legends.some((item) => item.styleGroup.some((entry) => entry.style.shape === 'POINT'))).toBe(true);
1728+
} else {
1729+
expect(legends.some((item) => item.styleGroup.some((entry) => entry.style.shape === 'POINT'))).toBe(false);
1730+
expect(legends.some((item) => item.styleField === 'symbolsContent')).toBe(false);
1731+
}
1732+
// Rebuilding legends must not append label settings to the saved renderer.
1733+
expect(renderer.length).toBe(1);
1734+
instance._legendList = [];
1735+
instance._createLegendInfo();
1736+
expect(instance._legendList).toEqual(legends);
1737+
});
1738+
});
1739+
});
1740+
16721741
it('should append text style for point renderer with text field', () => {
16731742
const { instance } = createTestContext();
16741743
instance._legendList = [];
@@ -2132,6 +2201,30 @@ describe('WebMapV3 - setting style helpers', () => {
21322201
});
21332202
});
21342203

2204+
describe('WebMapV3 - _transformLineStyle', () => {
2205+
it('应保留图层自身的 0 不透明度,不回退成符号的不透明度', () => {
2206+
const { instance } = createTestContext();
2207+
const result = instance._transformLineStyle({
2208+
oldWidth: 2,
2209+
symbol: { paint: { 'line-width': 2, 'line-color': '#FF0000', 'line-opacity': 0.8 } },
2210+
defaultStyle: { height: 8, opacity: 1, backgroundColor: '#FFFFFF' },
2211+
customValue: { opacity: 0 }
2212+
});
2213+
expect(result.opacity).toBe(0);
2214+
});
2215+
2216+
it('图层未设置不透明度时回退到符号自身的 line-opacity', () => {
2217+
const { instance } = createTestContext();
2218+
const result = instance._transformLineStyle({
2219+
oldWidth: 2,
2220+
symbol: { paint: { 'line-width': 2, 'line-color': '#FF0000', 'line-opacity': 0.8 } },
2221+
defaultStyle: { height: 8, opacity: 1, backgroundColor: '#FFFFFF' },
2222+
customValue: {}
2223+
});
2224+
expect(result.opacity).toBe(0.8);
2225+
});
2226+
});
2227+
21352228
describe('WebMapV3 - _parseLegendtyle types', () => {
21362229
it('TEXT should return text icon and css style', () => {
21372230
const { instance } = createTestContext();

0 commit comments

Comments
 (0)