Skip to content

Commit 55dfea3

Browse files
authored
Merge pull request #7507 from Lexachoc/fix_autobin_size_overlay
fix bin size becomes 1 (very thin) when data changes with barmode: 'overlay' for histogram
2 parents 227bba9 + e87fee1 commit 55dfea3

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

draftlogs/7507_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `histogram` autobin size for single-point traces in `overlay` mode on data updates via `Plotly.react` [[#7507](https://github.com/plotly/plotly.js/pull/7507)], with thanks to @Lexachoc for the contribution!

src/traces/histogram/calc.js

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

328328
// Edge case: single-valued histogram overlaying others
329329
// Use them all together to calculate the bin size for the single-valued one
330-
// Don't re-calculate bin width if user manually specified it (checing in bingroup=='' or xbins is defined)
330+
// Don't re-calculate bin width if user manually specified it
331331
if(isOverlay && !Registry.traceIs(trace, '2dMap') && newBinSpec._dataSpan === 0 &&
332332
pa.type !== 'category' && pa.type !== 'multicategory' &&
333-
trace.bingroup === '' && (typeof trace.xbins === 'undefined')) {
333+
trace.bingroup === '' && !trace._input[binAttr]?.size) {
334334
// Several single-valued histograms! Stop infinite recursion,
335335
// just return an extra flag that tells handleSingleValueOverlays
336336
// to sort out this trace too

test/jasmine/tests/histogram_test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,75 @@ 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+
expect(gd._fullData[0].xbins.size).toBe(100);
1354+
expect(gd._fullData[1].xbins.size).toBe(100);
1355+
expect(gd._fullData[2].xbins.size).toBe(100);
1356+
1357+
// Use a new trace data object
1358+
var newData = [
1359+
{x: ['1400'], type: 'histogram'},
1360+
{x: ['800'], type: 'histogram'},
1361+
{x: ['600'], type: 'histogram'}
1362+
];
1363+
1364+
return Plotly.react(gd, newData, layout);
1365+
})
1366+
.then(function() {
1367+
// minDiff = 800 - 600 = 200.
1368+
expect(gd._fullData[0].xbins.size).toBe(200);
1369+
expect(gd._fullData[1].xbins.size).toBe(200);
1370+
expect(gd._fullData[2].xbins.size).toBe(200);
1371+
})
1372+
.then(done, done.fail);
1373+
});
1374+
1375+
it('should preserve user-specified xbins.size for single-value overlays on Plotly.react', function(done) {
1376+
var initialData = [
1377+
{x: ['1457'], type: 'histogram', xbins: {size: 50}},
1378+
{x: ['820'], type: 'histogram', xbins: {size: 50}},
1379+
{x: ['720'], type: 'histogram', xbins: {size: 50}}
1380+
];
1381+
1382+
var layout = {
1383+
barmode: 'overlay'
1384+
};
1385+
1386+
Plotly.newPlot(gd, initialData, layout)
1387+
.then(function() {
1388+
expect(gd._fullData[0].xbins.size).toBe(50);
1389+
expect(gd._fullData[1].xbins.size).toBe(50);
1390+
expect(gd._fullData[2].xbins.size).toBe(50);
1391+
1392+
var newData = [
1393+
{x: ['1400'], type: 'histogram', xbins: {size: 50}},
1394+
{x: ['800'], type: 'histogram', xbins: {size: 50}},
1395+
{x: ['600'], type: 'histogram', xbins: {size: 50}}
1396+
];
1397+
1398+
return Plotly.react(gd, newData, layout);
1399+
})
1400+
.then(function() {
1401+
expect(gd._fullData[0].xbins.size).toBe(50);
1402+
expect(gd._fullData[1].xbins.size).toBe(50);
1403+
expect(gd._fullData[2].xbins.size).toBe(50);
1404+
})
1405+
.then(done, done.fail);
1406+
});
13381407
});
13391408
});
13401409

0 commit comments

Comments
 (0)