Skip to content

Commit 14e612a

Browse files
authored
fix incorrect autobin size when data changes
1 parent 1d5a249 commit 14e612a

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/traces/histogram/calc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,11 @@ function calcAllAutoBins(gd, trace, pa, mainData, _overlayEdgeCase) {
308308

309309
// Edge case: single-valued histogram overlaying others
310310
// Use them all together to calculate the bin size for the single-valued one
311-
// Don't re-calculate bin width if user manually specified it (checing in bingroup=='' or xbins is defined)
311+
// Don't re-calculate bin width if user manually specified it
312+
// Check if bingroup or trace[binAttr] (xbins or ybins) is defined
312313
if(isOverlay && !Registry.traceIs(trace, '2dMap') && newBinSpec._dataSpan === 0 &&
313314
pa.type !== 'category' && pa.type !== 'multicategory' &&
314-
trace.bingroup === '' && (typeof trace.xbins === 'undefined')) {
315+
trace.bingroup === '' && trace._input?.[binAttr]?.size === undefined) {
315316
// Several single-valued histograms! Stop infinite recursion,
316317
// just return an extra flag that tells handleSingleValueOverlays
317318
// to sort out this trace too

test/jasmine/tests/histogram_test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,43 @@ describe('Test histogram', function() {
13351335
})
13361336
.then(done, done.fail);
13371337
});
1338+
1339+
it('should correctly recalculate autobin size for single-value overlays on Plotly.react', function(done) {
1340+
var initialData = [
1341+
{x: ['1457'], type: 'histogram'},
1342+
{x: ['820'], type: 'histogram'},
1343+
{x: ['720'], type: 'histogram'}
1344+
];
1345+
1346+
var layout = {
1347+
barmode: 'overlay'
1348+
};
1349+
1350+
Plotly.newPlot(gd, initialData, layout)
1351+
.then(function() {
1352+
// minDiff = 820 - 720 = 100
1353+
console.log("gd._fullData=", gd._fullData);
1354+
expect(gd._fullData[0].xbins.size).toBe(100);
1355+
expect(gd._fullData[1].xbins.size).toBe(100);
1356+
expect(gd._fullData[2].xbins.size).toBe(100);
1357+
1358+
// Use a new trace data object
1359+
var newData = [
1360+
{x: ['1400'], type: 'histogram'},
1361+
{x: ['800'], type: 'histogram'},
1362+
{x: ['600'], type: 'histogram'}
1363+
];
1364+
1365+
return Plotly.react(gd, newData, layout);
1366+
})
1367+
.then(function() {
1368+
// minDiff = 800 - 600 = 200.
1369+
expect(gd._fullData[0].xbins.size).toBe(200);
1370+
expect(gd._fullData[1].xbins.size).toBe(200);
1371+
expect(gd._fullData[2].xbins.size).toBe(200);
1372+
})
1373+
.then(done, done.fail);
1374+
});
13381375
});
13391376
});
13401377

0 commit comments

Comments
 (0)