diff --git a/src/traces/bar/layout_defaults.js b/src/traces/bar/layout_defaults.js index 1f688164dd0..528f5871be1 100644 --- a/src/traces/bar/layout_defaults.js +++ b/src/traces/bar/layout_defaults.js @@ -24,6 +24,8 @@ module.exports = function(layoutIn, layoutOut, fullData) { var gappedAnyway = false; var usedSubplots = {}; + var mode = coerce('barmode'); + for(var i = 0; i < fullData.length; i++) { var trace = fullData[i]; if(Registry.traceIs(trace, 'bar') && trace.visible) hasBars = true; @@ -31,7 +33,7 @@ module.exports = function(layoutIn, layoutOut, fullData) { // if we have at least 2 grouped bar traces on the same subplot, // we should default to a gap anyway, even if the data is histograms - if(layoutIn.barmode !== 'overlay' && layoutIn.barmode !== 'stack') { + if(mode === 'group') { var subploti = trace.xaxis + trace.yaxis; if(usedSubplots[subploti]) gappedAnyway = true; usedSubplots[subploti] = true; @@ -44,9 +46,11 @@ module.exports = function(layoutIn, layoutOut, fullData) { } } - if(!hasBars) return; + if(!hasBars) { + delete layoutOut.barmode; + return; + } - var mode = coerce('barmode'); if(mode !== 'overlay') coerce('barnorm'); coerce('bargap', (shouldBeGapless && !gappedAnyway) ? 0 : 0.2); diff --git a/test/image/baselines/histogram_barmode_relative.png b/test/image/baselines/histogram_barmode_relative.png new file mode 100644 index 00000000000..34c8b61bbaf Binary files /dev/null and b/test/image/baselines/histogram_barmode_relative.png differ diff --git a/test/image/mocks/histogram_barmode_relative.json b/test/image/mocks/histogram_barmode_relative.json new file mode 100644 index 00000000000..7437af5f8e0 --- /dev/null +++ b/test/image/mocks/histogram_barmode_relative.json @@ -0,0 +1,13 @@ +{ + "data": [{ + "type": "histogram", + "x": [9, 9, 3, 2, 5, 1, 3, 0, 6, 8, 5, 7, 2, 9, 9, 8, 5, 4, 1, 9, 2, 8, 0, 7, 2, 3, 5, 0, 3, 8, 2, 1, 7, 7, 7, 3, 9, 7, 8, 1, 7, 9, 4, 6, 2, 4, 2, 9, 3, 1, 5, 1, 6, 7, 6, 1, 6, 8, 6, 7, 8, 3, 7, 3, 1, 0, 2, 6, 1, 2, 7, 2, 9, 6, 2, 8, 0, 0, 9, 8, 5, 5, 8, 3, 5, 7, 7, 8, 3, 9, 3, 1, 5, 3, 5, 0, 8, 9, 4, 3] + }, { + "type": "histogram", + "x": [6, 7, 8, 0, 8, 1, 5, 4, 4, 3, 4, 7, 5, 3, 9, 5, 2, 5, 5, 4, 3, 5, 2, 6, 3, 9, 8, 5, 3, 8, 7, 2, 2, 7, 3, 7, 0, 1, 1, 1, 2, 1, 4, 9, 3, 5, 4, 1, 1, 2, 0, 2, 8, 1, 0, 3, 1, 2, 3, 5, 3, 8, 6, 1, 1, 0, 0, 0, 8, 6, 0, 8, 6, 8, 0, 9, 4, 4, 0, 7, 7, 9, 2, 8, 0, 9, 0, 5, 7, 2, 9, 6, 5, 0, 0, 4, 6, 0, 9, 8] + }], + "layout": { + "width": 600, + "barmode": "relative" + } +} diff --git a/test/jasmine/tests/bar_test.js b/test/jasmine/tests/bar_test.js index 5a0934734be..eef374418f8 100644 --- a/test/jasmine/tests/bar_test.js +++ b/test/jasmine/tests/bar_test.js @@ -226,6 +226,20 @@ describe('Bar.supplyDefaults', function() { expect(gd._fullData[0].alignmentgroup).toBe(undefined, 'alignementgroup'); expect(gd._fullData[0].offsetgroup).toBe(undefined, 'offsetgroup'); }); + + it('should have a barmode only if it contains bars', function() { + var gd = { + data: [{type: 'histogram', y: [1], visible: false}], + layout: {} + }; + + supplyAllDefaults(gd); + expect(gd._fullLayout.barmode).toBe(undefined, '`barmode` should be undefined'); + + gd.data[0].visible = true; + supplyAllDefaults(gd); + expect(gd._fullLayout.barmode).toBe('group', '`barmode` should be set to its default '); + }); }); describe('bar calc / crossTraceCalc (formerly known as setPositions)', function() {