Skip to content

Commit c150b3d

Browse files
committed
bar: Move code for overlay mode into a function
* Refactored the code to set bar positions in overlay mode into function `setGroupPositionsInOverlayMode`.
1 parent 451ee82 commit c150b3d

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/traces/bar/set_positions.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,32 @@ module.exports = function setPositions(gd, plotinfo) {
5252
function setGroupPositions(gd, pa, sa, traces) {
5353
if(!traces.length) return;
5454

55-
var fullLayout = gd._fullLayout,
56-
pLetter = pa._id.charAt(0),
57-
sLetter = sa._id.charAt(0);
58-
59-
if(fullLayout.barmode === 'overlay') {
60-
traces.forEach(function(trace) {
61-
setOffsetAndWidth(gd, pa, pLetter, [trace]);
62-
});
55+
if(gd._fullLayout.barmode === 'overlay') {
56+
setGroupPositionsInOverlayMode(gd, pa, sa, traces);
57+
}
58+
else {
59+
setOffsetAndWidth(gd, pa, traces);
60+
setBaseAndSize(gd, sa, traces);
6361
}
64-
else setOffsetAndWidth(gd, pa, pLetter, traces);
62+
}
63+
6564

66-
setBaseAndSize(gd, sa, sLetter, traces);
65+
function setGroupPositionsInOverlayMode(gd, pa, sa, traces) {
66+
// set bar offsets and widths
67+
traces.forEach(function(trace) {
68+
setOffsetAndWidth(gd, pa, [trace]);
69+
});
70+
71+
// for overlaid bars,
72+
// just make sure the size axis includes zero,
73+
// along with the tops of each bar,
74+
// and store these bar tops in calcdata
75+
var sLetter = getAxisLetter(sa),
76+
fs = function(v) { v[sLetter] = v.s; return v.s; };
77+
78+
for(var i = 0; i < traces.length; i++) {
79+
Axes.expand(sa, traces[i].map(fs), {tozero: true, padded: true});
80+
}
6781
}
6882

6983

@@ -72,8 +86,9 @@ function setGroupPositions(gd, pa, sa, traces) {
7286
// to find the maximum width bars that won't overlap
7387
// for stacked or grouped bars, this is all vertical or horizontal
7488
// bars for overlaid bars, call this individually on each trace.
75-
function setOffsetAndWidth(gd, pa, pLetter, traces) {
89+
function setOffsetAndWidth(gd, pa, traces) {
7690
var fullLayout = gd._fullLayout,
91+
pLetter = getAxisLetter(pa),
7792
i, trace,
7893
j, bar;
7994

@@ -135,8 +150,9 @@ function setOffsetAndWidth(gd, pa, pLetter, traces) {
135150
}
136151

137152

138-
function setBaseAndSize(gd, sa, sLetter, traces) {
153+
function setBaseAndSize(gd, sa, traces) {
139154
var fullLayout = gd._fullLayout,
155+
sLetter = getAxisLetter(sa),
140156
i, trace,
141157
j, bar;
142158

@@ -253,3 +269,8 @@ function setBaseAndSize(gd, sa, sLetter, traces) {
253269
}
254270
}
255271
}
272+
273+
274+
function getAxisLetter(ax) {
275+
return ax._id.charAt(0);
276+
}

0 commit comments

Comments
 (0)