Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: multiple lines added to a stack bar chart also stack #3477

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
Merge branch 'main' into main
  • Loading branch information
iameddz authored Jun 26, 2023
commit b009c821c30f34702058d51218e427bcd7a16d85
49 changes: 29 additions & 20 deletions src/modules/Range.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,28 +527,37 @@ class Range {
const gl = this.w.globals
// for stacked charts, we calculate each series's parallel values. i.e, series[0][j] + series[1][j] .... [series[i.length][j]] and get the max out of it

if (gl.series.length) {
for (let j = 0; j < gl.series[gl.maxValsInArrayIndex].length; j++) {
let poss = 0
let negs = 0
for (let i = 0; i < gl.series.length; i++) {
if (!gl.series.length) return
let seriesGroups = gl.seriesGroups

if (!seriesGroups.length) {
seriesGroups = [this.w.config.series.map((serie) => serie.name)]
}
let stackedPoss = {}
let stackedNegs = {}

seriesGroups.forEach((group) => {
stackedPoss[group] = []
stackedNegs[group] = []
const indicesOfSeriesInGroup = this.w.config.series
.map((serie, si) => (group.indexOf(serie.name) > -1 ? si : null))
.filter((f) => f !== null)

indicesOfSeriesInGroup.forEach((i) => {
for (let j = 0; j < gl.series[gl.maxValsInArrayIndex].length; j++) {
if (typeof stackedPoss[group][j] === 'undefined') {
stackedPoss[group][j] = 0
stackedNegs[group][j] = 0
}

let stackSeries =
!this.w.config.chart.stackOnlyBar ||
(gl.series[i] && gl.series[i].type && gl.series[i].type === 'bar')

if (stackSeries) {
if (gl.series[i][j] !== null && Utils.isNumber(gl.series[i][j])) {
// 0.0001 fixes #185 when values are very small
gl.series[i][j] > 0
? (poss = poss + parseFloat(gl.series[i][j]) + 0.0001)
: (negs = negs + parseFloat(gl.series[i][j]))
}

if (i === gl.series.length - 1) {
// push all the totals to the array for future use
stackedPoss.push(poss)
stackedNegs.push(negs)
}
(gl.series[i] && gl.series[i].type && gl.series[i].type === 'bar');

if (stackSeries && gl.series[i][j] !== null && Utils.isNumber(gl.series[i][j])) {
gl.series[i][j] > 0
? (stackedPoss[group][j] += parseFloat(gl.series[i][j]) + 0.0001)
: (stackedNegs[group][j] += parseFloat(gl.series[i][j]))
}
}
})
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.