Skip to content

Commit 3b87e85

Browse files
committed
bar: use Sieve to stack bars in setBaseAndSize
1 parent 80fdfbd commit 3b87e85

File tree

1 file changed

+17
-34
lines changed

1 file changed

+17
-34
lines changed

src/traces/bar/set_positions.js

+17-34
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,12 @@ function setBaseAndSize(gd, sa, sieve) {
249249
// so we don't have to redo this later
250250
var sMax = sa.l2c(sa.c2l(0)),
251251
sMin = sMax,
252-
sums = {},
252+
barEnd;
253253

254-
// make sure if p is different only by rounding,
255-
// we still stack
256-
sumround = traces[0][0].t.barwidth / 100,
257-
sv = 0,
258-
padded = true,
259-
barEnd,
260-
scale;
254+
// stack bars that only differ by rounding
255+
sieve.binWidth = traces[0][0].t.barwidth / 100;
261256

262-
for(i = 0; i < traces.length; i++) { // trace index
257+
for(i = 0; i < traces.length; i++) {
263258
trace = traces[i];
264259

265260
for(j = 0; j < trace.length; j++) {
@@ -269,16 +264,11 @@ function setBaseAndSize(gd, sa, sieve) {
269264
// so that we don't try to stack them
270265
if(!isNumeric(bar.s)) continue;
271266

272-
sv = Math.round(bar.p / sumround);
273-
274-
// store the negative sum value for p at the same key,
275-
// with sign flipped using string to ensure -0 !== 0.
276-
if(relative && bar.s < 0) sv = '-' + sv;
267+
// stack current bar and get previous sum
268+
var previousSum = sieve.put(bar.p, bar.s);
277269

278-
var previousSum = sums[sv] || 0;
279270
if(stack || relative) bar.b = previousSum;
280271
barEnd = bar.b + bar.s;
281-
sums[sv] = previousSum + bar.s;
282272

283273
// store the bar top in each calcdata item
284274
if(stack || relative) {
@@ -291,43 +281,36 @@ function setBaseAndSize(gd, sa, sieve) {
291281
}
292282
}
293283

294-
if(norm) {
295-
var top = norm === 'fraction' ? 1 : 100,
296-
relAndNegative = false,
297-
tiny = top / 1e9; // in case of rounding error in sum
284+
var padded = true;
298285

286+
if(norm) {
299287
padded = false;
288+
289+
var sTop = (norm === 'fraction') ? 1 : 100,
290+
sTiny = sTop / 1e9; // in case of rounding error in sum
291+
300292
sMin = 0;
301-
sMax = stack ? top : 0;
293+
sMax = (stack) ? sTop : 0;
302294

303-
for(i = 0; i < traces.length; i++) { // trace index
295+
for(i = 0; i < traces.length; i++) {
304296
trace = traces[i];
305297

306298
for(j = 0; j < trace.length; j++) {
307299
bar = trace[j];
308300

309-
relAndNegative = (relative && bar.s < 0);
310-
311-
sv = Math.round(bar.p / sumround);
312-
313-
// locate negative sum amount for this p val
314-
if(relAndNegative) sv = '-' + sv;
315-
316-
scale = top / sums[sv];
301+
var scale = Math.abs(sTop / sieve.get(bar.p, bar.s));
317302

318-
// preserve sign if negative
319-
if(relAndNegative) scale *= -1;
320303
bar.b *= scale;
321304
bar.s *= scale;
322305
barEnd = bar.b + bar.s;
323306
bar[sLetter] = barEnd;
324307

325308
if(isNumeric(sa.c2l(barEnd))) {
326-
if(barEnd < sMin - tiny) {
309+
if(barEnd < sMin - sTiny) {
327310
padded = true;
328311
sMin = barEnd;
329312
}
330-
if(barEnd > sMax + tiny) {
313+
if(barEnd > sMax + sTiny) {
331314
padded = true;
332315
sMax = barEnd;
333316
}

0 commit comments

Comments
 (0)