]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
fix: aspect ratio calc (#10693)
authorDan Onoshko <danon0404@gmail.com>
Wed, 28 Sep 2022 19:31:19 +0000 (02:31 +0700)
committerGitHub <noreply@github.com>
Wed, 28 Sep 2022 19:31:19 +0000 (15:31 -0400)
src/helpers/helpers.dom.js
test/specs/helpers.dom.tests.js
test/specs/platform.dom.tests.js

index 5ab3778394277d1cbb090ab02a5384ac6b666e26..78b128f7c8550620a46b4681d048e125407d31f5 100644 (file)
@@ -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));
   }
index dfe41f1c3ef3860b259eee8ca72526b54cd6dda6..545e153db3383ee313d3e53749c672bbc64fa6c6 100644 (file)
@@ -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);
+  });
 });
index a2ba3ac62f62e9b4b0319f087a896fd911be5980..13bc3a6109866f643324363eb9e3857c3efa86e7 100644 (file)
@@ -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,
+        });
       });
     });
   });