Skip to content

Commit

Permalink
improve total datalabels in stacked grouped bars
Browse files Browse the repository at this point in the history
  • Loading branch information
junedchhipa committed Dec 3, 2023
1 parent 2c0fb38 commit 355f9e2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/charts/common/bar/DataLabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export default class BarDataLabels {
x: dataLabelsPos.totalDataLabelsX,
y: dataLabelsPos.totalDataLabelsY,
barWidth,
barHeight,
realIndex,
textAnchor: dataLabelsPos.totalDataLabelsAnchor,
val: this.getStackedTotalDataLabel({ realIndex, j }),
Expand Down Expand Up @@ -608,6 +609,7 @@ export default class BarDataLabels {
y,
val,
barWidth,
barHeight,
realIndex,
textAnchor,
barTotalDataLabelsConfig,
Expand All @@ -626,10 +628,14 @@ export default class BarDataLabels {
totalDataLabelText = graphics.drawText({
x:
x -
(w.globals.seriesGroups.length
(!w.globals.isBarHorizontal && w.globals.seriesGroups.length
? barWidth / w.globals.seriesGroups.length
: 0),
y: y,
y:
y -
(w.globals.isBarHorizontal && w.globals.seriesGroups.length
? barHeight / w.globals.seriesGroups.length
: 0),
foreColor: barTotalDataLabelsConfig.style.color,
text: val,
textAnchor,
Expand Down
32 changes: 30 additions & 2 deletions src/modules/CoreUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CoreUtils {

return {
comboBarCount,
comboCharts
comboCharts,
}
}

Expand Down Expand Up @@ -81,6 +81,34 @@ class CoreUtils {
}
}

/**
* @memberof CoreUtils
* returns the sum of values in a multiple stacked grouped charts
* Eg. w.globals.series = [[32,33,43,12], [2,3,5,1], [43, 23, 34, 22]]
* series 1 and 2 are in a group, while series 3 is in another group
* @return [[34, 36, 48, 12], [43, 23, 34, 22]]
**/
getStackedSeriesTotalsByGroups() {
const w = this.w
let total = []

w.globals.seriesGroups.forEach((sg) => {
let includedIndexes = []
w.config.series.forEach((s, si) => {
if (sg.indexOf(s.name) > -1) {
includedIndexes.push(si)
}
})

const excludedIndices = w.globals.series
.map((_, fi) => (includedIndexes.indexOf(fi) === -1 ? fi : -1))
.filter((f) => f !== -1)

total.push(this.getStackedSeriesTotals(excludedIndices))
})
return total
}

isSeriesNull(index = null) {
let r = []
if (index === null) {
Expand Down Expand Up @@ -292,7 +320,7 @@ class CoreUtils {
invertedXRatio,
baseLineInvertedY,
baseLineY,
baseLineX
baseLineX,
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/modules/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,8 @@ export default class Data {
this.coreUtils.getSeriesTotals()
if (gl.axisCharts) {
gl.stackedSeriesTotals = this.coreUtils.getStackedSeriesTotals()
gl.stackedSeriesTotalsByGroups =
this.coreUtils.getStackedSeriesTotalsByGroups()
}

this.coreUtils.getPercentSeries()
Expand Down

0 comments on commit 355f9e2

Please sign in to comment.