Skip to content

Commit f872f76

Browse files
committed
add "have to match" bingroup logic for traces w/ alignmentgroup
1 parent a7f49a0 commit f872f76

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

Diff for: src/traces/histogram/cross_trace_defaults.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var traceIs = require('../../registry').traceIs;
1515
var handleGroupingDefaults = require('../bar/defaults').handleGroupingDefaults;
1616

1717
var nestedProperty = Lib.nestedProperty;
18+
var getAxisGroup = axisIds.getAxisGroup;
1819

1920
var BINATTRS = [
2021
{aStr: {x: 'xbins.start', y: 'ybins.start'}, name: 'start'},
@@ -28,8 +29,6 @@ var BINDIRECTIONS = ['x', 'y'];
2829
// handle bin attrs and relink auto-determined values so fullData is complete
2930
module.exports = function crossTraceDefaults(fullData, fullLayout) {
3031
var allBinOpts = fullLayout._histogramBinOpts = {};
31-
var isOverlay = fullLayout.barmode === 'overlay';
32-
3332
var histTraces = [];
3433
var mustMatchTracesLookup = {};
3534
var otherTracesList = [];
@@ -117,19 +116,40 @@ module.exports = function crossTraceDefaults(fullData, fullLayout) {
117116
}
118117
}
119118

119+
var alignmentOpts = fullLayout._alignmentOpts || {};
120+
120121
// Look for traces that "have to match", that is:
121122
// - 1d histogram traces on the same subplot with same orientation under barmode:stack,
122123
// - 1d histogram traces on the same subplot with same orientation under barmode:group
124+
// - 1d histogram traces on the same position axis with the same orientation
125+
// and the same *alignmentgroup* (coerced under barmode:group)
126+
// - Once `stackgroup` gets implemented (see https://github.com/plotly/plotly.js/issues/3614),
127+
// traces within the same stackgroup will also "have to match"
123128
for(i = 0; i < histTraces.length; i++) {
124129
traceOut = histTraces[i];
130+
groupName = '';
131+
132+
if(!traceIs(traceOut, '2dMap')) {
133+
binDir = orientation2binDir(traceOut);
125134

126-
if(!isOverlay && !traceIs(traceOut, '2dMap')) {
127-
groupName = (
128-
axisIds.getAxisGroup(fullLayout, traceOut.xaxis) +
129-
axisIds.getAxisGroup(fullLayout, traceOut.yaxis) +
130-
orientation2binDir(traceOut)
131-
);
135+
if(fullLayout.barmode === 'group' && traceOut.alignmentgroup) {
136+
var pa = traceOut[binDir + 'axis'];
137+
var aGroupId = getAxisGroup(fullLayout, pa) + traceOut.orientation;
138+
if((alignmentOpts[aGroupId] || {})[traceOut.alignmentgroup]) {
139+
groupName = aGroupId;
140+
}
141+
}
142+
143+
if(!groupName && fullLayout.barmode !== 'overlay') {
144+
groupName = (
145+
getAxisGroup(fullLayout, traceOut.xaxis) +
146+
getAxisGroup(fullLayout, traceOut.yaxis) +
147+
orientation2binDir(traceOut)
148+
);
149+
}
150+
}
132151

152+
if(groupName) {
133153
if(!mustMatchTracesLookup[groupName]) {
134154
mustMatchTracesLookup[groupName] = [];
135155
}

Diff for: test/jasmine/tests/histogram_test.js

+44
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,50 @@ describe('Test histogram', function() {
478478
'with bins on a gregorian calendar'
479479
);
480480
});
481+
482+
it('should force traces that "have to match" to have same bingroup (alignmentgroup case)', function() {
483+
var traces;
484+
485+
function initTraces() {
486+
traces = [{}, {}, {yaxis: 'y2'}, {yaxis: 'y2'}];
487+
traces.forEach(function(t) {
488+
t.type = 'histogram';
489+
t.x = [1];
490+
});
491+
}
492+
493+
function _supply() {
494+
gd = {
495+
data: traces,
496+
layout: {
497+
barmode: 'group',
498+
grid: {rows: 2, columns: 1}
499+
}
500+
};
501+
supplyAllDefaults(gd);
502+
}
503+
504+
initTraces();
505+
_supply(gd);
506+
_assert('base (separate subplot w/o alignmentgroup)', [
507+
['xyx', [0, 1]],
508+
['xy2x', [2, 3]]
509+
]);
510+
511+
initTraces();
512+
[
513+
{alignmentgroup: 'a', offsetgroup: '-'},
514+
{alignmentgroup: 'a', offsetgroup: '--'},
515+
{alignmentgroup: 'a', offsetgroup: '-'},
516+
{alignmentgroup: 'a', offsetgroup: '--'}
517+
].forEach(function(patch, i) {
518+
Lib.extendFlat(traces[i], patch);
519+
});
520+
_supply(gd);
521+
_assert('all in same alignmentgroup, must match', [
522+
['xv', [0, 1, 2, 3]]
523+
]);
524+
});
481525
});
482526

483527
describe('calc', function() {

0 commit comments

Comments
 (0)