From 112c81c73136c4da09fd93905fbd286cf0cc2382 Mon Sep 17 00:00:00 2001 From: biubiukam Date: Tue, 4 Aug 2026 23:36:52 +0800 Subject: [PATCH] fix: respect disabled label smart invert --- ...ue-4492-label-smart-invert_2026-08-04.json | 11 +++++ .../unit/component/label/label-util.test.ts | 45 +++++++++++++++++++ packages/vchart/src/component/label/util.ts | 12 +---- 3 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 common/changes/@visactor/vchart/fix-issue-4492-label-smart-invert_2026-08-04.json create mode 100644 packages/vchart/__tests__/unit/component/label/label-util.test.ts diff --git a/common/changes/@visactor/vchart/fix-issue-4492-label-smart-invert_2026-08-04.json b/common/changes/@visactor/vchart/fix-issue-4492-label-smart-invert_2026-08-04.json new file mode 100644 index 0000000000..d217bf903e --- /dev/null +++ b/common/changes/@visactor/vchart/fix-issue-4492-label-smart-invert_2026-08-04.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "comment": "fix: respect explicit label smart invert settings", + "type": "patch", + "packageName": "@visactor/vchart" + } + ], + "packageName": "@visactor/vchart", + "email": "biukam.w@gmail.com" +} diff --git a/packages/vchart/__tests__/unit/component/label/label-util.test.ts b/packages/vchart/__tests__/unit/component/label/label-util.test.ts new file mode 100644 index 0000000000..9e431cbf82 --- /dev/null +++ b/packages/vchart/__tests__/unit/component/label/label-util.test.ts @@ -0,0 +1,45 @@ +import { barLabel, pieLabel } from '../../../../src/component/label/util'; + +const createLabelInfo = (labelSpec: Record) => + ({ + labelSpec, + series: { + direction: 'vertical', + getMeasureField: () => ['value'], + getYAxisHelper: (): undefined => undefined + } + } as any); + +describe('label util', () => { + describe('barLabel', () => { + it('preserves disabled smart invert for inside labels', () => { + const result = barLabel(createLabelInfo({ position: 'inside', smartInvert: false })); + + expect(result.smartInvert).toBe(false); + }); + + it('preserves explicit smart invert options for inside labels', () => { + const smartInvert = { + fillStrategy: 'invertBase', + strokeStrategy: 'similarBase' + }; + const result = barLabel(createLabelInfo({ position: 'inside', smartInvert })); + + expect(result.smartInvert).toBe(smartInvert); + }); + + it('enables smart invert by default for inside labels', () => { + const result = barLabel(createLabelInfo({ position: 'inside' })); + + expect(result.smartInvert).toBe(true); + }); + }); + + describe('pieLabel', () => { + it('preserves disabled smart invert for inside labels', () => { + const result = pieLabel(createLabelInfo({ position: 'inside', smartInvert: false })); + + expect(result.smartInvert).toBe(false); + }); + }); +}); diff --git a/packages/vchart/src/component/label/util.ts b/packages/vchart/src/component/label/util.ts index 2ddb744dbf..d76155d25a 100644 --- a/packages/vchart/src/component/label/util.ts +++ b/packages/vchart/src/component/label/util.ts @@ -173,10 +173,7 @@ export function barLabel(labelInfo: ILabelInfo) { } // encode smartInvert - let smartInvert = false; - if (isString(originPosition) && originPosition.includes('inside')) { - smartInvert = true; - } + const smartInvert = labelSpec.smartInvert ?? (isString(originPosition) && originPosition.includes('inside')); return { position, overlap, smartInvert }; } @@ -240,12 +237,7 @@ export function pieLabel(labelInfo: ILabelInfo) { const position = labelPosition as BaseLabelAttrs['position']; // encode smartInvert - let smartInvert; - if (labelSpec.smartInvert) { - smartInvert = labelSpec.smartInvert; - } else { - smartInvert = isString(labelPosition) && labelPosition.includes('inside'); - } + const smartInvert = labelSpec.smartInvert ?? (isString(labelPosition) && labelPosition.includes('inside')); return { position, smartInvert }; }