From 355f9e233fb8babb92682a9e697adca931a76453 Mon Sep 17 00:00:00 2001 From: junedchhipa Date: Sun, 3 Dec 2023 14:36:04 +0530 Subject: [PATCH] improve total datalabels in stacked grouped bars --- src/charts/common/bar/DataLabels.js | 10 +++++++-- src/modules/CoreUtils.js | 32 +++++++++++++++++++++++++++-- src/modules/Data.js | 2 ++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/charts/common/bar/DataLabels.js b/src/charts/common/bar/DataLabels.js index 125c1cd53..5d98e170c 100644 --- a/src/charts/common/bar/DataLabels.js +++ b/src/charts/common/bar/DataLabels.js @@ -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 }), @@ -608,6 +609,7 @@ export default class BarDataLabels { y, val, barWidth, + barHeight, realIndex, textAnchor, barTotalDataLabelsConfig, @@ -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, diff --git a/src/modules/CoreUtils.js b/src/modules/CoreUtils.js index 9ab4e6351..5e876681b 100644 --- a/src/modules/CoreUtils.js +++ b/src/modules/CoreUtils.js @@ -35,7 +35,7 @@ class CoreUtils { return { comboBarCount, - comboCharts + comboCharts, } } @@ -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) { @@ -292,7 +320,7 @@ class CoreUtils { invertedXRatio, baseLineInvertedY, baseLineY, - baseLineX + baseLineX, } } diff --git a/src/modules/Data.js b/src/modules/Data.js index 2053c1942..43f3d9119 100644 --- a/src/modules/Data.js +++ b/src/modules/Data.js @@ -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()