Skip to content

Commit

Permalink
fix #4230; total stacked label alignment fix
Browse files Browse the repository at this point in the history
  • Loading branch information
junedchhipa committed Jan 8, 2025
1 parent 873e75f commit 4f0be8d
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/charts/common/bar/DataLabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,17 @@ export default class BarDataLabels {
break
}

let lowestPrevY = newY
w.globals.seriesGroups.forEach((sg) => {
this.barCtx[sg.join(',')].prevY.forEach((arr) => {
if (valIsNegative) {
lowestPrevY = Math.max(arr[j], lowestPrevY)
} else {
lowestPrevY = Math.min(arr[j], lowestPrevY)
}
})
})

if (
this.barCtx.lastActiveBarSerieIndex === realIndex &&
barTotalDataLabelsConfig.enabled
Expand All @@ -324,14 +335,14 @@ export default class BarDataLabels {

if (valIsNegative) {
totalDataLabelsY =
newY -
lowestPrevY -
totalLabeltextRects.height / 2 -
offY -
barTotalDataLabelsConfig.offsetY +
ADDITIONAL_OFFY
} else {
totalDataLabelsY =
newY +
lowestPrevY +
totalLabeltextRects.height +
offY +
barTotalDataLabelsConfig.offsetY -
Expand Down Expand Up @@ -439,6 +450,17 @@ export default class BarDataLabels {
break
}

let lowestPrevX = newX
w.globals.seriesGroups.forEach((sg) => {
this.barCtx[sg.join(',')].prevX.forEach((arr) => {
if (valIsNegative) {
lowestPrevX = Math.min(arr[j], lowestPrevX)
} else {
lowestPrevX = Math.max(arr[j], lowestPrevX)
}
})
})

if (
this.barCtx.lastActiveBarSerieIndex === realIndex &&
barTotalDataLabelsConfig.enabled
Expand All @@ -450,12 +472,12 @@ export default class BarDataLabels {
)
if (valIsNegative) {
totalDataLabelsX =
newX - strokeWidth - offX - barTotalDataLabelsConfig.offsetX
lowestPrevX - strokeWidth - offX - barTotalDataLabelsConfig.offsetX

totalDataLabelsAnchor = 'end'
} else {
totalDataLabelsX =
newX +
lowestPrevX +
offX +
barTotalDataLabelsConfig.offsetX +
(this.barCtx.isReversed ? -(barWidth + strokeWidth) : strokeWidth)
Expand All @@ -466,6 +488,11 @@ export default class BarDataLabels {
totalLabeltextRects.height / 2 +
barTotalDataLabelsConfig.offsetY +
strokeWidth

if (w.globals.barGroups.length > 1) {
totalDataLabelsY =
totalDataLabelsY - (w.globals.barGroups.length / 2) * (barHeight / 2)
}
}

if (!w.config.chart.stacked) {
Expand Down

0 comments on commit 4f0be8d

Please sign in to comment.