From ef3542bcff399d221b417a62fdbb713f5e4cd57c Mon Sep 17 00:00:00 2001 From: Ray Wong <135116870+rayw-lab@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:13:32 +0800 Subject: [PATCH 1/2] Fix contour colorbar position after a resize --- src/plot_api/plot_api.js | 7 ++++ test/jasmine/tests/colorbar_test.js | 56 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index 5eea5fc4806..67ec2766a2c 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -205,6 +205,13 @@ function _doPlot(gd, data, layout, config) { * start async-friendly code - now we're actually drawing things */ + // Margin pushers such as colorbars depend on the plot's pixel dimensions. + // Refresh the inner size before drawing them after a resize. + var size = fullLayout._size; + if (size.w !== Math.round(fullLayout.width) - size.l - size.r || + size.h !== Math.round(fullLayout.height) - size.t - size.b) { + Plots.doAutoMargin(gd); + } var oldMargins = Lib.extendFlat({}, fullLayout._size); // draw framework first so that margin-pushing diff --git a/test/jasmine/tests/colorbar_test.js b/test/jasmine/tests/colorbar_test.js index 1785f8cb21c..51b350bb331 100644 --- a/test/jasmine/tests/colorbar_test.js +++ b/test/jasmine/tests/colorbar_test.js @@ -71,6 +71,62 @@ describe('Test colorbar:', function() { }); }); + describe('colorbar position after a height change', function() { + var gd; + + beforeEach(function() { + gd = createGraphDiv(); + gd.style.width = '600px'; + gd.style.height = '500px'; + }); + + afterEach(function() { + Plotly.purge(gd); + destroyGraphDiv(); + }); + + function colorbarLeft() { + var colorbar = gd.querySelector('.colorbar .cbbg'); + return colorbar.getBoundingClientRect().left - gd.getBoundingClientRect().left; + } + + var changes = { + relayout: function() { + return Plotly.relayout(gd, {height: 400}); + }, + react: function(type) { + return Plotly.react(gd, [{type: type, z: [[1, 4], [2, 3]]}], {width: 600, height: 400}); + }, + resize: function() { + gd.style.height = '400px'; + return Plotly.Plots.resize(gd); + } + }; + + ['contour', 'heatmap'].forEach(function(type) { + Object.keys(changes).forEach(function(method) { + it('places the ' + type + ' colorbar after ' + method + ' at its fresh position', function(done) { + var data = [{type: type, z: [[1, 4], [2, 3]]}]; + var initialLayout = method === 'resize' ? {} : {width: 600, height: 500}; + var afterResize; + + Plotly.newPlot(gd, data, initialLayout) + .then(function() { + return changes[method](type); + }) + .then(function() { + afterResize = colorbarLeft(); + return Plotly.newPlot(gd, data, {width: 600, height: 400}); + }) + .then(function() { + expect(afterResize).toBeCloseTo(colorbarLeft(), 0); + }) + .then(done, done.fail); + }); + }); + }); + }); + describe('drawing & editing', function() { var gd; From c060da95caa6ae20c6017d3e656a5d630233daef Mon Sep 17 00:00:00 2001 From: Ray Wong <135116870+rayw-lab@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:22:54 +0800 Subject: [PATCH 2/2] Add a draft changelog entry for #8202 --- draftlogs/8202_fix.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 draftlogs/8202_fix.md diff --git a/draftlogs/8202_fix.md b/draftlogs/8202_fix.md new file mode 100644 index 00000000000..e7297e497bf --- /dev/null +++ b/draftlogs/8202_fix.md @@ -0,0 +1 @@ +- Align the contour colorbar after a plot changes height [[#8202](https://github.com/plotly/plotly.js/pull/8202)], with thanks to @rayw-lab for the contribution!