Skip to content

Commit 5972eae

Browse files
[fix]webmap3 图例 面+单值轮廓色,没显示面
AI-GEN: 70% Cursor
1 parent 9a2d778 commit 5972eae

2 files changed

Lines changed: 97 additions & 13 deletions

File tree

‎src/common/mapping/WebMapV3.js‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ const LEGEND_CSS_STATE_KEY = {
3636
backgroundColor: 'color'
3737
};
3838

39+
// 轮廓相关的图例样式属性:这些属性被数据驱动时,面的图例仍然需要单独展示
40+
const LEGEND_OUTLINE_KEYS = ['outlineColor', 'outlineWidth', 'outlineOpacity', 'outlineSymbolsContent'];
41+
3942
const LEGEND_CSS_DEFAULT = {
4043
[LEGEND_RENDER_TYPE.TEXT]: {
4144
textSize: '16px',
@@ -899,16 +902,16 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
899902
if (keys) {
900903
const simpleKeys = keys.filter((k) => styleSetting[k] && styleSetting[k].type === 'simple');
901904
simpleKeys.forEach((k) => {
902-
const isOutlineKey = ['outlineColor', 'outlineWidth', 'outlineOpacity', 'outlineSymbolsContent'].includes(k);
903-
const hasOutline =
904-
styleSetting &&
905-
(
906-
(styleSetting.outline && styleSetting.outline.value === true) ||
907-
(styleSetting.antialias && styleSetting.antialias.value === true)
908-
);
909-
if (isOutlineKey && !hasOutline) {
910-
return;
911-
}
905+
const isOutlineKey = LEGEND_OUTLINE_KEYS.includes(k);
906+
const hasOutline =
907+
styleSetting &&
908+
(
909+
(styleSetting.outline && styleSetting.outline.value === true) ||
910+
(styleSetting.antialias && styleSetting.antialias.value === true)
911+
);
912+
if (isOutlineKey && !hasOutline) {
913+
return;
914+
}
912915
simpleStyle[k] = (styleSetting[k] || {}).value;
913916
});
914917
}
@@ -987,12 +990,18 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
987990

988991
/**
989992
* 1) 无数据驱动时;
990-
* 2) 只有一个颜色数据驱动,且性线数据驱动时
991-
* 以上两种情况图例中需要单独的显示符号项
993+
* 2) 只有一个颜色数据驱动,且性线数据驱动时;
994+
* 3) 只有轮廓相关属性被数据驱动时(面 + 轮廓色/轮廓符号专题图)
995+
* 以上三种情况图例中需要单独的显示符号项,否则「面」的图例会缺失
992996
* 是否显示图例单项
993997
*/
994998
_isShowLegendSingleItem(dataKeys, isLinearColor) {
995-
return dataKeys.length === 0 || (dataKeys.length === 1 && dataKeys[0] === 'color' && isLinearColor);
999+
const isOnlyOutlineDriven = dataKeys.length > 0 && dataKeys.every((key) => LEGEND_OUTLINE_KEYS.includes(key));
1000+
return (
1001+
dataKeys.length === 0 ||
1002+
(dataKeys.length === 1 && dataKeys[0] === 'color' && isLinearColor) ||
1003+
isOnlyOutlineDriven
1004+
);
9961005
}
9971006

9981007
_isWebsymbolById(id){

‎test/common/mapping/WebMapV3Spec.js‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,81 @@ describe('WebMapV3 - legend symbol/style helpers', () => {
524524
});
525525
});
526526

527+
describe('_isShowLegendSingleItem', () => {
528+
it('无数据驱动时显示单项', () => {
529+
expect(instance._isShowLegendSingleItem([], false)).toBe(true);
530+
});
531+
532+
it('color 为线性数据驱动时显示单项', () => {
533+
expect(instance._isShowLegendSingleItem(['color'], true)).toBe(true);
534+
});
535+
536+
it('color 非线性数据驱动时不显示单项', () => {
537+
expect(instance._isShowLegendSingleItem(['color'], false)).toBe(false);
538+
});
539+
540+
it('面与轮廓都数据驱动时不显示单项,避免重复的面图例', () => {
541+
expect(instance._isShowLegendSingleItem(['color', 'outlineColor'], true)).toBe(false);
542+
});
543+
544+
it('只有轮廓色数据驱动时需要显示面的单项(面 + 轮廓色专题图)', () => {
545+
expect(instance._isShowLegendSingleItem(['outlineColor'], false)).toBe(true);
546+
});
547+
548+
it('只有轮廓符号或轮廓宽/透明度数据驱动时需要显示面的单项', () => {
549+
expect(instance._isShowLegendSingleItem(['outlineSymbolsContent'], false)).toBe(true);
550+
expect(instance._isShowLegendSingleItem(['outlineWidth'], false)).toBe(true);
551+
expect(instance._isShowLegendSingleItem(['outlineOpacity'], false)).toBe(true);
552+
});
553+
554+
it('填充符号数据驱动时不显示单项,符号分组本身即为面图例', () => {
555+
expect(instance._isShowLegendSingleItem(['symbolsContent'], false)).toBe(false);
556+
});
557+
558+
it('轮廓属性与非轮廓属性混合驱动时不显示单项', () => {
559+
expect(instance._isShowLegendSingleItem(['symbolsContent', 'outlineColor'], false)).toBe(false);
560+
});
561+
});
562+
563+
describe('_createLayerLegendList 面 + 轮廓色', () => {
564+
it('轮廓色为单值专题图时,图例同时包含面单项与轮廓色分组', () => {
565+
const layer = { id: 'beijing', title: 'beijing', type: 'fill' };
566+
const styleSetting = {
567+
type: 'fill',
568+
color: { type: 'simple', value: '#826DBA' },
569+
opacity: { type: 'simple', value: 0.9 },
570+
outline: { type: 'simple', value: true },
571+
outlineWidth: { type: 'simple', value: 0.5 },
572+
outlineColor: {
573+
type: 'unique',
574+
field: ['smpid'],
575+
defaultValue: '#ffffff',
576+
values: [
577+
{ value: '#d53e4f', key: 1 },
578+
{ value: '#3288bd', key: 18 }
579+
]
580+
},
581+
styleRenderMode: 'mapboxgl'
582+
};
583+
584+
const result = instance._createLayerLegendList(layer, styleSetting);
585+
586+
// 第一项为面:带上填充底色与轮廓
587+
expect(result.length).toBe(2);
588+
expect(result[0].styleField).toBeUndefined();
589+
expect(result[0].styleGroup.length).toBe(1);
590+
expect(result[0].styleGroup[0].style.backgroundColor).toBe('#826DBA');
591+
expect(result[0].styleGroup[0].style.outlineStyle).toBe('solid');
592+
expect(result[0].styleGroup[0].style.shape).toBe('RECTANGLE');
593+
594+
// 第二项为轮廓色专题图分组,且每个分组样式保留面的填充底色
595+
expect(result[1].styleField).toBe('outlineColor');
596+
expect(result[1].styleGroup.length).toBeGreaterThan(1);
597+
expect(result[1].styleGroup[0].style.outlineColor).toBe('#d53e4f');
598+
expect(result[1].styleGroup[0].style.backgroundColor).toBe('#826DBA');
599+
});
600+
});
601+
527602
describe('_getLegendStyleType', () => {
528603
it('should return CUSTOM when currentType is custom', () => {
529604
const result = instance._getLegendStyleType({

0 commit comments

Comments
 (0)