Fix chart resizing issue (#7297) (#7298)

Fix chart resizing issue
This commit is contained in:
Yiwen Wang
2020-05-26 04:54:55 +08:00
committed by Evert Timberg
parent 5ab50126be
commit e4f9bec4b3
2 changed files with 62 additions and 1 deletions

View File

@@ -236,7 +236,15 @@ function createResizeObserver(chart, type, listener) {
// @ts-ignore until https://github.com/microsoft/TypeScript/issues/37861 implemented
const observer = new ResizeObserver(entries => {
const entry = entries[0];
resize(entry.contentRect.width, entry.contentRect.height);
const width = entry.contentRect.width;
const height = entry.contentRect.height;
// When its container's display is set to 'none' the callback will be called with a
// size of (0, 0), which will cause the chart to lost its original height, so skip
// resizing in such case.
if (width === 0 && height === 0) {
return;
}
resize(width, height);
});
observer.observe(container);
return observer;

View File

@@ -365,6 +365,59 @@ describe('Chart', function() {
wrapper.style.width = '455px';
});
it('should restore the original size when parent became invisible', function(done) {
var chart = acquireChart({
options: {
responsive: true,
maintainAspectRatio: false
}
}, {
canvas: {
style: ''
},
wrapper: {
style: 'width: 300px; height: 350px; position: relative'
}
});
waitForResize(chart, function() {
expect(chart).toBeChartOfSize({
dw: 300, dh: 350,
rw: 300, rh: 350,
});
var original = chart.resize;
chart.resize = function() {
fail('resize should not have been called');
};
var wrapper = chart.canvas.parentNode;
wrapper.style.display = 'none';
setTimeout(function() {
expect(wrapper.clientWidth).toEqual(0);
expect(wrapper.clientHeight).toEqual(0);
expect(chart).toBeChartOfSize({
dw: 300, dh: 350,
rw: 300, rh: 350,
});
chart.resize = original;
waitForResize(chart, function() {
expect(chart).toBeChartOfSize({
dw: 300, dh: 350,
rw: 300, rh: 350,
});
done();
});
wrapper.style.display = 'block';
}, 200);
});
});
it('should resize the canvas when parent is RTL and width changes', function(done) {
var chart = acquireChart({
options: {