Skip to content

Commit 77dc2c3

Browse files
committed
bar: fix normalised group bar plots
* Fixed bug computing the normalisation of bar plots in group mode. * The bug was caused by calling `sieve.put` with bar sizes that didn't take into account the bar base. * Bar plots in stack and relative mode weren't affected by this bug, because traces that set a bar base are excluded from the stack/relative mode.
1 parent a0504d1 commit 77dc2c3

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/traces/bar/set_positions.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,8 @@ function stackBars(gd, sa, sieve) {
503503
if(!isNumeric(bar.s)) continue;
504504

505505
// stack current bar and get previous sum
506-
var barBase = sieve.put(bar.p, bar.s),
507-
barTop = barBase + bar.s;
506+
var barBase = sieve.put(bar.p, bar.b + bar.s),
507+
barTop = barBase + bar.b + bar.s;
508508

509509
// store the bar base and top in each calcdata item
510510
bar.b = barBase;
@@ -537,7 +537,7 @@ function sieveBars(gd, sa, sieve) {
537537
for(var j = 0; j < trace.length; j++) {
538538
var bar = trace[j];
539539

540-
if(isNumeric(bar.s)) sieve.put(bar.p, bar.s);
540+
if(isNumeric(bar.s)) sieve.put(bar.p, bar.b + bar.s);
541541
}
542542
}
543543
}

test/jasmine/tests/bar_test.js

+21
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,27 @@ describe('Bar.setPositions', function() {
543543
assertPointField(cd, 'y', [[0.75, 0.50, 0.25], [0.25, 0.50, 0.75]]);
544544
});
545545

546+
it('should honor barnorm (group+base case)', function() {
547+
var gd = mockBarPlot([{
548+
base: [3, 2, 1],
549+
y: [0, 0, 0]
550+
}, {
551+
y: [1, 2, 3]
552+
}], {
553+
bargap: 0,
554+
barmode: 'group',
555+
barnorm: 'fraction'
556+
});
557+
558+
expect(gd._fullLayout.barnorm).toBe('fraction');
559+
560+
var cd = gd.calcdata;
561+
assertPointField(cd, 'b', [[0.75, 0.50, 0.25], [0, 0, 0]]);
562+
assertPointField(cd, 's', [[0, 0, 0], [0.25, 0.50, 0.75]]);
563+
assertPointField(cd, 'x', [[-0.25, 0.75, 1.75], [0.25, 1.25, 2.25]]);
564+
assertPointField(cd, 'y', [[0.75, 0.50, 0.25], [0.25, 0.50, 0.75]]);
565+
});
566+
546567
it('should honor barnorm (stack case)', function() {
547568
var gd = mockBarPlot([{
548569
y: [3, 2, 1]

0 commit comments

Comments
 (0)