Skip to content

Commit 23bdc82

Browse files
committed
bar: add function sieveBars
* Function sieveBars sieves all the bars without updating the bar bases. This step is required before calling normalizeBars. * Function stackBars sieves all the bars and updates the bar bases and tops accordingly.
1 parent 54ac1f1 commit 23bdc82

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

src/traces/bar/set_positions.js

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ function setGroupPositionsInOverlayMode(gd, pa, sa, traces) {
8484

8585
// set bar bases and sizes, and update size axis
8686
if(barnorm) {
87-
stackBars(gd, sa, sieve);
87+
sieveBars(gd, sa, sieve);
88+
normalizeBars(gd, sa, sieve);
8889
}
8990
else {
9091
// make sure the size axis includes zero,
@@ -113,7 +114,8 @@ function setGroupPositionsInGroupMode(gd, pa, sa, traces) {
113114

114115
// set bar bases and sizes, and update size axis
115116
if(barnorm) {
116-
stackBars(gd, sa, sieve);
117+
sieveBars(gd, sa, sieve);
118+
normalizeBars(gd, sa, sieve);
117119
}
118120
else {
119121
// make sure the size axis includes zero,
@@ -231,6 +233,9 @@ function setOffsetAndWidthInGroupMode(gd, pa, sieve) {
231233
}
232234
}
233235

236+
// stack bars that only differ by rounding
237+
sieve.binWidth = traces[0][0].t.barwidth / 100;
238+
234239
// update position axes
235240
Axes.minDtick(pa, minDiff, distinctPositions[0], overlap);
236241
Axes.expand(pa, distinctPositions, {vpad: minDiff / 2});
@@ -239,15 +244,12 @@ function setOffsetAndWidthInGroupMode(gd, pa, sieve) {
239244

240245
function stackBars(gd, sa, sieve) {
241246
var fullLayout = gd._fullLayout,
247+
barnorm = fullLayout.barnorm,
242248
sLetter = getAxisLetter(sa),
243249
traces = sieve.traces,
244250
i, trace,
245251
j, bar;
246252

247-
var stack = (fullLayout.barmode === 'stack'),
248-
relative = (fullLayout.barmode === 'relative'),
249-
norm = fullLayout.barnorm;
250-
251253
// bar size range and stacking calculation
252254
// for stacked bars, we need to evaluate every step in every
253255
// stack, because negative bars mean the extremes could be
@@ -257,37 +259,32 @@ function stackBars(gd, sa, sieve) {
257259
var sMax = sa.l2c(sa.c2l(0)),
258260
sMin = sMax;
259261

260-
// stack bars that only differ by rounding
261-
sieve.binWidth = traces[0][0].t.barwidth / 100;
262-
263262
for(i = 0; i < traces.length; i++) {
264263
trace = traces[i];
265264

266265
for(j = 0; j < trace.length; j++) {
267266
bar = trace[j];
268267

269-
// skip over bars with no size,
270-
// so that we don't try to stack them
271268
if(!isNumeric(bar.s)) continue;
272269

273270
// stack current bar and get previous sum
274271
var previousSum = sieve.put(bar.p, bar.s);
275272

276-
if(stack || relative) bar.b = previousSum;
273+
// store the bar base and top in each calcdata item
274+
bar.b = previousSum;
277275

278-
// store the bar top in each calcdata item
279-
if(stack || relative) {
280-
var barEnd = bar.b + bar.s;
281-
bar[sLetter] = barEnd;
282-
if(!norm && isNumeric(sa.c2l(barEnd))) {
283-
sMax = Math.max(sMax, barEnd);
284-
sMin = Math.min(sMin, barEnd);
285-
}
276+
var barEnd = bar.b + bar.s;
277+
bar[sLetter] = barEnd;
278+
279+
if(!barnorm && isNumeric(sa.c2l(barEnd))) {
280+
sMax = Math.max(sMax, barEnd);
281+
sMin = Math.min(sMin, barEnd);
286282
}
287283
}
288284
}
289285

290-
if(norm) {
286+
// if barnorm is set, let normalizeBars update the axis range
287+
if(barnorm) {
291288
normalizeBars(gd, sa, sieve);
292289
}
293290
else {
@@ -296,7 +293,27 @@ function stackBars(gd, sa, sieve) {
296293
}
297294

298295

296+
function sieveBars(gd, sa, sieve) {
297+
var traces = sieve.traces;
298+
299+
for(var i = 0; i < traces.length; i++) {
300+
var trace = traces[i];
301+
302+
for(var j = 0; j < trace.length; j++) {
303+
var bar = trace[j];
304+
305+
if(isNumeric(bar.s)) sieve.put(bar.p, bar.s);
306+
}
307+
}
308+
}
309+
310+
299311
function normalizeBars(gd, sa, sieve) {
312+
// Note:
313+
//
314+
// normalizeBars requires that either sieveBars or stackBars has been
315+
// previously invoked.
316+
300317
var traces = sieve.traces,
301318
sLetter = getAxisLetter(sa),
302319
sTop = (gd._fullLayout.barnorm === 'fraction') ? 1 : 100,
@@ -332,6 +349,7 @@ function normalizeBars(gd, sa, sieve) {
332349
}
333350
}
334351

352+
// update range of size axis
335353
Axes.expand(sa, [sMin, sMax], {tozero: true, padded: padded});
336354
}
337355

0 commit comments

Comments
 (0)