Skip to content

Commit 6eb9b1d

Browse files
committed
add alignmentgroup and offsetgroup to bar and histogram traces
1 parent 02e51d8 commit 6eb9b1d

15 files changed

+883
-15
lines changed

src/plots/plots.js

+2
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ plots.supplyDefaults = function(gd, opts) {
394394
newFullLayout._scatterStackOpts = {};
395395
// for the first scatter trace on each subplot (so it knows tonext->tozero)
396396
newFullLayout._firstScatter = {};
397+
// for grouped bar/box/violin trace to share config across traces
398+
newFullLayout._alignmentOpts = {};
397399

398400
// for traces to request a default rangeslider on their x axes
399401
// eg set `_requestRangeslider.x2 = true` for xaxis2

src/traces/bar/attributes.js

+22
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,28 @@ module.exports = {
174174

175175
marker: marker,
176176

177+
offsetgroup: {
178+
valType: 'string',
179+
role: 'info',
180+
dflt: '',
181+
editType: 'calc',
182+
description: [
183+
'Set several traces linked to the same position axis to the same',
184+
'offsetgroup where bars of the same position coordinate will line up.'
185+
].join(' ')
186+
},
187+
alignmentgroup: {
188+
valType: 'string',
189+
role: 'info',
190+
dflt: '',
191+
editType: 'calc',
192+
description: [
193+
'Set several traces linked to the same position axis to the same',
194+
'alignmentgroup. This controls whether bars compute their positional',
195+
'range dependently or independently.'
196+
].join(' ')
197+
},
198+
177199
selected: {
178200
marker: {
179201
opacity: scatterAttrs.selected.marker.opacity,

src/traces/bar/cross_trace_calc.js

+26-10
Original file line numberDiff line numberDiff line change
@@ -279,26 +279,42 @@ function setOffsetAndWidthInGroupMode(gd, pa, sieve) {
279279
var distinctPositions = sieve.distinctPositions;
280280
var minDiff = sieve.minDiff;
281281
var calcTraces = sieve.traces;
282+
var nTraces = calcTraces.length;
282283

283284
// if there aren't any overlapping positions,
284285
// let them have full width even if mode is group
285286
var overlap = (positions.length !== distinctPositions.length);
286-
287-
var nTraces = calcTraces.length;
288287
var barGroupWidth = minDiff * (1 - bargap);
289-
var barWidthPlusGap = (overlap) ? barGroupWidth / nTraces : barGroupWidth;
290-
var barWidth = barWidthPlusGap * (1 - bargroupgap);
288+
289+
var groupId = pa._id + calcTraces[0][0].trace.orientation;
290+
var alignmentGroups = fullLayout._alignmentOpts[groupId] || {};
291291

292292
for(var i = 0; i < nTraces; i++) {
293293
var calcTrace = calcTraces[i];
294-
var t = calcTrace[0].t;
294+
var trace = calcTrace[0].trace;
295295

296-
// computer bar group center and bar offset
297-
var offsetFromCenter = overlap ?
298-
((2 * i + 1 - nTraces) * barWidthPlusGap - barWidth) / 2 :
299-
-barWidth / 2;
296+
var alignmentGroupOpts = alignmentGroups[trace.alignmentgroup] || {};
297+
var nOffsetGroups = Object.keys(alignmentGroupOpts.offsetGroups || {}).length;
300298

301-
// store bar width and offset for this trace
299+
var barWidthPlusGap;
300+
if(nOffsetGroups) {
301+
barWidthPlusGap = barGroupWidth / nOffsetGroups;
302+
} else {
303+
barWidthPlusGap = overlap ? barGroupWidth / nTraces : barGroupWidth;
304+
}
305+
306+
var barWidth = barWidthPlusGap * (1 - bargroupgap);
307+
308+
var offsetFromCenter;
309+
if(nOffsetGroups) {
310+
offsetFromCenter = ((2 * trace._offsetIndex + 1 - nOffsetGroups) * barWidthPlusGap - barWidth) / 2;
311+
} else {
312+
offsetFromCenter = overlap ?
313+
((2 * i + 1 - nTraces) * barWidthPlusGap - barWidth) / 2 :
314+
-barWidth / 2;
315+
}
316+
317+
var t = calcTrace[0].t;
302318
t.barwidth = barWidth;
303319
t.poffset = offsetFromCenter;
304320
t.bargroupwidth = barGroupWidth;

src/traces/bar/defaults.js

+50-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var Lib = require('../../lib');
@@ -17,7 +16,7 @@ var handleXYDefaults = require('../scatter/xy_defaults');
1716
var handleStyleDefaults = require('../bar/style_defaults');
1817
var attributes = require('./attributes');
1918

20-
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
19+
function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
2120
function coerce(attr, dflt) {
2221
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
2322
}
@@ -77,5 +76,54 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
7776
errorBarsSupplyDefaults(traceIn, traceOut, lineColor || Color.defaultLine, {axis: 'y'});
7877
errorBarsSupplyDefaults(traceIn, traceOut, lineColor || Color.defaultLine, {axis: 'x', inherit: 'y'});
7978

79+
handleGroupingDefaults(traceIn, traceOut, layout, coerce);
80+
8081
Lib.coerceSelectionMarkerOpacity(traceOut, coerce);
82+
}
83+
84+
function handleGroupingDefaults(traceIn, traceOut, layout, coerce) {
85+
var orientation = traceOut.orientation;
86+
// TODO make this work across matching axes too?!?
87+
// TODO should this work per trace-type?
88+
// one set for bar/histogram another for box/violin?
89+
// or just one set for all trace trace types?
90+
var posAxId = traceOut[{v: 'x', h: 'y'}[orientation] + 'axis'];
91+
var groupId = posAxId + orientation;
92+
93+
var alignmentOpts = layout._alignmentOpts || {};
94+
var alignmentgroup = coerce('alignmentgroup');
95+
96+
var alignmentGroups = alignmentOpts[groupId];
97+
if(!alignmentGroups) alignmentGroups = alignmentOpts[groupId] = {};
98+
99+
var alignmentGroupOpts = alignmentGroups[alignmentgroup];
100+
101+
if(alignmentGroupOpts) {
102+
alignmentGroupOpts.traces.push(traceOut);
103+
} else {
104+
alignmentGroupOpts = alignmentGroups[alignmentgroup] = {
105+
traces: [traceOut],
106+
alignmentIndex: Object.keys(alignmentGroups).length,
107+
offsetGroups: {}
108+
};
109+
}
110+
111+
var offsetgroup = coerce('offsetgroup');
112+
var offsetGroups = alignmentGroupOpts.offsetGroups;
113+
var offsetGroupOpts = offsetGroups[offsetgroup];
114+
115+
if(offsetgroup) {
116+
if(!offsetGroupOpts) {
117+
offsetGroupOpts = offsetGroups[offsetgroup] = {
118+
offsetIndex: Object.keys(offsetGroups).length
119+
};
120+
}
121+
122+
traceOut._offsetIndex = offsetGroupOpts.offsetIndex;
123+
}
124+
}
125+
126+
module.exports = {
127+
supplyDefaults: supplyDefaults,
128+
handleGroupingDefaults: handleGroupingDefaults
81129
};

src/traces/bar/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var Bar = {};
1312

1413
Bar.attributes = require('./attributes');
1514
Bar.layoutAttributes = require('./layout_attributes');
16-
Bar.supplyDefaults = require('./defaults');
15+
Bar.supplyDefaults = require('./defaults').supplyDefaults;
1716
Bar.supplyLayoutDefaults = require('./layout_defaults');
1817
Bar.calc = require('./calc');
1918
Bar.crossTraceCalc = require('./cross_trace_calc').crossTraceCalc;

src/traces/histogram/attributes.js

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ module.exports = {
188188

189189
marker: barAttrs.marker,
190190

191+
offsetgroup: barAttrs.offsetgroup,
192+
alignmentgroup: barAttrs.alignmentgroup,
193+
191194
selected: barAttrs.selected,
192195
unselected: barAttrs.unselected,
193196

src/traces/histogram/defaults.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var Registry = require('../../registry');
1312
var Lib = require('../../lib');
1413
var Color = require('../../components/color');
1514

1615
var handleStyleDefaults = require('../bar/style_defaults');
16+
var handleGroupingDefaults = require('../bar/defaults').handleGroupingDefaults;
1717
var attributes = require('./attributes');
1818

1919
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
@@ -70,4 +70,6 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
7070
var errorBarsSupplyDefaults = Registry.getComponentMethod('errorbars', 'supplyDefaults');
7171
errorBarsSupplyDefaults(traceIn, traceOut, lineColor || Color.defaultLine, {axis: 'y'});
7272
errorBarsSupplyDefaults(traceIn, traceOut, lineColor || Color.defaultLine, {axis: 'x', inherit: 'y'});
73+
74+
handleGroupingDefaults(traceIn, traceOut, layout, coerce);
7375
};
43 KB
Loading
Loading
23.5 KB
Loading
29.5 KB
Loading

0 commit comments

Comments
 (0)