From cbf5c62a3928352ddefd9f0db3a386c4f6533e1b Mon Sep 17 00:00:00 2001 From: Justin-ZS Date: Fri, 22 Aug 2025 14:25:55 +0800 Subject: [PATCH] fix(bar): wrong data label position when bar width/height is 0 --- src/chart/bar/BarView.ts | 22 +++++++- test/bar-zero-label.html | 119 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 test/bar-zero-label.html diff --git a/src/chart/bar/BarView.ts b/src/chart/bar/BarView.ts index eac83aab02..38fccd44e0 100644 --- a/src/chart/bar/BarView.ts +++ b/src/chart/bar/BarView.ts @@ -1041,8 +1041,8 @@ function updateStyle( ) ) : (isHorizontalOrRadial - ? ((layout as RectLayout).height >= 0 ? 'bottom' : 'top') - : ((layout as RectLayout).width >= 0 ? 'right' : 'left')); + ? getLabelPositionForHorizontal(layout as RectLayout, seriesModel.coordinateSystem) + : getLabelPositionForVertical(layout as RectLayout, seriesModel.coordinateSystem)); const labelStatesModels = getLabelStatesModels(itemModel); @@ -1288,4 +1288,22 @@ function createBackgroundEl( }); } +function getLabelPositionForHorizontal(layout: RectLayout, coordSys: CoordSysOfBar): 'top' | 'bottom' { + if (layout.height === 0) { + // For zero height, determine position based on axis inverse status + const valueAxis = (coordSys as Cartesian2D).getOtherAxis((coordSys as Cartesian2D).getBaseAxis()); + return valueAxis.inverse ? 'bottom' : 'top'; + } + return layout.height > 0 ? 'bottom' : 'top'; +} + +function getLabelPositionForVertical(layout: RectLayout, coordSys: CoordSysOfBar): 'left' | 'right' { + if (layout.width === 0) { + // For zero width, determine position based on axis inverse status + const valueAxis = (coordSys as Cartesian2D).getOtherAxis((coordSys as Cartesian2D).getBaseAxis()); + return valueAxis.inverse ? 'left' : 'right'; + } + return layout.width >= 0 ? 'right' : 'left'; +} + export default BarView; diff --git a/test/bar-zero-label.html b/test/bar-zero-label.html new file mode 100644 index 0000000000..c69de751a3 --- /dev/null +++ b/test/bar-zero-label.html @@ -0,0 +1,119 @@ + + + + + + + + + + + + + + +

Bar chart zero value label position test

+
+
+
+
+
+
+
+
+ + + \ No newline at end of file