From: Dan Onoshko Date: Wed, 28 Sep 2022 19:31:19 +0000 (+0700) Subject: fix: aspect ratio calc (#10693) X-Git-Tag: v4.0.0~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=52cf8e8a94130d63b7618751fe0df871ca1af222;p=thirdparty%2FChart.js.git fix: aspect ratio calc (#10693) --- diff --git a/src/helpers/helpers.dom.js b/src/helpers/helpers.dom.js index 5ab377839..78b128f7c 100644 --- a/src/helpers/helpers.dom.js +++ b/src/helpers/helpers.dom.js @@ -175,7 +175,9 @@ export function getMaximumSize(canvas, bbWidth, bbHeight, aspectRatio) { height = round1(width / 2); } - if (aspectRatio && height > containerSize.height) { + const maintainHeight = bbWidth !== undefined || bbHeight !== undefined; + + if (maintainHeight && aspectRatio && containerSize.height && height > containerSize.height) { height = containerSize.height; width = round1(Math.floor(height * aspectRatio)); } diff --git a/test/specs/helpers.dom.tests.js b/test/specs/helpers.dom.tests.js index dfe41f1c3..545e153db 100644 --- a/test/specs/helpers.dom.tests.js +++ b/test/specs/helpers.dom.tests.js @@ -487,4 +487,21 @@ describe('DOM helpers tests', function() { document.body.removeChild(container); }); + + it('should respect aspect ratio and skip container height', () => { + const container = document.createElement('div'); + container.style.width = '500px'; + container.style.height = '200px'; + + document.body.appendChild(container); + + const target = document.createElement('div'); + target.style.width = '500px'; + target.style.height = '500px'; + container.appendChild(target); + + expect(helpers.getMaximumSize(target, undefined, undefined, 1)).toEqual(jasmine.objectContaining({width: 500, height: 500})); + + document.body.removeChild(container); + }); }); diff --git a/test/specs/platform.dom.tests.js b/test/specs/platform.dom.tests.js index a2ba3ac62..13bc3a610 100644 --- a/test/specs/platform.dom.tests.js +++ b/test/specs/platform.dom.tests.js @@ -286,9 +286,11 @@ describe('Platform.dom', function() { } }); - expect(chart).toBeChartOfSize({ - dw: 214, dh: 350, - rw: 214, rh: 350, + waitForResize(chart, () => { + expect(chart).toBeChartOfSize({ + dw: 214, dh: 350, + rw: 214, rh: 350, + }); }); }); });