Skip to content

Commit 451ee82

Browse files
committed
bar: replace t.dbar with t.bargroupwidth
* Don't assume the position axis is `xa` in the calculation of `barDelta` in `bar/hover.js`. * Updated `tests/bar_test.js` to account for the replacement of `t.bar` with `t.bargroupwidth`. * Updated the group case in `tests/bar_test.js` to test the use of `layout.bargroupgap`.
1 parent f63a7b7 commit 451ee82

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

src/traces/bar/hover.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
2121
xa = pointData.xa,
2222
ya = pointData.ya,
2323
barDelta = (hovermode === 'closest') ?
24-
t.barwidth / 2 : t.dbar * (1 - xa._gd._fullLayout.bargap) / 2,
24+
t.barwidth / 2 :
25+
t.bargroupwidth,
2526
barPos;
2627

2728
if(hovermode !== 'closest') barPos = function(di) { return di.p; };

src/traces/bar/set_positions.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,26 @@ function setOffsetAndWidth(gd, pa, pLetter, traces) {
105105
Axes.expand(pa, distinctPositions, {vpad: minDiff / 2});
106106

107107
// computer bar widths and position offsets
108-
var barWidth = minDiff * (1 - fullLayout.bargap);
109-
if(overlap) barWidth /= traces.length;
110-
111-
var barWidthMinusGroupGap = barWidth * (1 - fullLayout.bargroupgap);
108+
var barGroupWidth = minDiff * (1 - fullLayout.bargap),
109+
barWidthPlusGap = (overlap) ?
110+
barGroupWidth / traces.length :
111+
barGroupWidth,
112+
barWidth = barWidthPlusGap * (1 - fullLayout.bargroupgap);
112113

113114
for(i = 0; i < traces.length; i++) {
114115
trace = traces[i];
115116

116117
// computer bar group center and bar offset
117-
var offsetFromCenter = (
118-
(overlap ? (2 * i + 1 - traces.length) * barWidth : 0) -
119-
barWidthMinusGroupGap
120-
) / 2,
121-
barCenter = offsetFromCenter + barWidthMinusGroupGap / 2;
118+
var offsetFromCenter = (overlap) ?
119+
((2 * i + 1 - traces.length) * barWidthPlusGap - barWidth) / 2 :
120+
-barWidth / 2,
121+
barCenter = offsetFromCenter + barWidth / 2;
122122

123123
// store bar width and offset for this trace
124124
var t = trace[0].t;
125-
t.barwidth = barWidthMinusGroupGap;
125+
t.barwidth = barWidth;
126126
t.poffset = offsetFromCenter;
127-
t.dbar = minDiff;
127+
t.bargroupwidth = barGroupWidth;
128128

129129
// store the bar center in each calcdata item
130130
for(j = 0; j < trace.length; j++) {

test/jasmine/tests/bar_test.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ describe('heatmap calc / setPositions', function() {
142142
assertPtField(out, 'p', [[0, 1, 2], [0, 1, 2], [0, 1, 2]]);
143143
assertTraceField(out, 't.barwidth', [0.8, 0.8, 0.8]);
144144
assertTraceField(out, 't.poffset', [-0.4, -0.4, -0.4]);
145-
assertTraceField(out, 't.dbar', [1, 1, 1]);
145+
assertTraceField(out, 't.bargroupwidth', [0.8, 0.8, 0.8]);
146146
});
147147

148148
it('should fill in calc pt fields (overlay case)', function() {
@@ -161,7 +161,7 @@ describe('heatmap calc / setPositions', function() {
161161
assertPtField(out, 'p', [[0, 1, 2], [0, 1, 2]]);
162162
assertTraceField(out, 't.barwidth', [0.8, 0.8]);
163163
assertTraceField(out, 't.poffset', [-0.4, -0.4]);
164-
assertTraceField(out, 't.dbar', [1, 1]);
164+
assertTraceField(out, 't.bargroupwidth', [0.8, 0.8]);
165165
});
166166

167167
it('should fill in calc pt fields (group case)', function() {
@@ -170,17 +170,19 @@ describe('heatmap calc / setPositions', function() {
170170
}, {
171171
y: [3, 1, 2]
172172
}], {
173-
barmode: 'group'
173+
barmode: 'group',
174+
// asumming default bargap is 0.2
175+
bargroupgap: 0.1
174176
});
175177

176178
assertPtField(out, 'x', [[-0.2, 0.8, 1.8], [0.2, 1.2, 2.2]]);
177179
assertPtField(out, 'y', [[2, 1, 2], [3, 1, 2]]);
178180
assertPtField(out, 'b', [[0, 0, 0], [0, 0, 0]]);
179181
assertPtField(out, 's', [[2, 1, 2], [3, 1, 2]]);
180182
assertPtField(out, 'p', [[0, 1, 2], [0, 1, 2]]);
181-
assertTraceField(out, 't.barwidth', [0.4, 0.4]);
182-
assertTraceField(out, 't.poffset', [-0.4, 0]);
183-
assertTraceField(out, 't.dbar', [1, 1]);
183+
assertTraceField(out, 't.barwidth', [0.36, 0.36]);
184+
assertTraceField(out, 't.poffset', [-0.38, 0.02]);
185+
assertTraceField(out, 't.bargroupwidth', [0.8, 0.8]);
184186
});
185187

186188
it('should fill in calc pt fields (relative case)', function() {
@@ -199,7 +201,7 @@ describe('heatmap calc / setPositions', function() {
199201
assertPtField(out, 'p', [[0, 1, 2], [0, 1, 2]]);
200202
assertTraceField(out, 't.barwidth', [0.8, 0.8]);
201203
assertTraceField(out, 't.poffset', [-0.4, -0.4]);
202-
assertTraceField(out, 't.dbar', [1, 1]);
204+
assertTraceField(out, 't.bargroupwidth', [0.8, 0.8]);
203205
});
204206

205207
it('should fill in calc pt fields (relative / percent case)', function() {
@@ -221,6 +223,6 @@ describe('heatmap calc / setPositions', function() {
221223
assertPtField(out, 'p', [[0, 1, 2, 3], [0, 1, 2, 3]]);
222224
assertTraceField(out, 't.barwidth', [0.8, 0.8]);
223225
assertTraceField(out, 't.poffset', [-0.4, -0.4]);
224-
assertTraceField(out, 't.dbar', [1, 1]);
226+
assertTraceField(out, 't.bargroupwidth', [0.8, 0.8]);
225227
});
226228
});

0 commit comments

Comments
 (0)