Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/8202_fix.md
Original file line number Diff line number Diff line change
@@ -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!
7 changes: 7 additions & 0 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions test/jasmine/tests/colorbar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down