@@ -7,7 +7,7 @@ import PluginService from './core.plugins';
77import registry from './core.registry' ;
88import Config , { determineAxis , getIndexAxis } from './core.config' ;
99import { retinaScale } from '../helpers/helpers.dom' ;
10- import { each , callback as callCallback , uid , valueOrDefault , _elementsEqual } from '../helpers/helpers.core' ;
10+ import { each , callback as callCallback , uid , valueOrDefault , _elementsEqual , isNullOrUndef } from '../helpers/helpers.core' ;
1111import { clearCanvas , clipArea , unclipArea , _isPointInArea } from '../helpers/helpers.canvas' ;
1212// @ts -ignore
1313import { version } from '../../package.json' ;
@@ -103,8 +103,11 @@ class Chart {
103103 this . canvas = canvas ;
104104 this . width = width ;
105105 this . height = height ;
106- this . aspectRatio = height ? width / height : null ;
107106 this . _options = options ;
107+ // Store the previously used aspect ratio to determine if a resize
108+ // is needed during updates. Do this after _options is set since
109+ // aspectRatio uses a getter
110+ this . _aspectRatio = this . aspectRatio ;
108111 this . _layers = [ ] ;
109112 this . _metasets = [ ] ;
110113 this . _stacks = undefined ;
@@ -147,6 +150,22 @@ class Chart {
147150 }
148151 }
149152
153+ get aspectRatio ( ) {
154+ const { options : { aspectRatio, maintainAspectRatio} , width, height, _aspectRatio} = this ;
155+ if ( ! isNullOrUndef ( aspectRatio ) ) {
156+ // If aspectRatio is defined in options, use that.
157+ return aspectRatio ;
158+ }
159+
160+ if ( maintainAspectRatio && _aspectRatio ) {
161+ // If maintainAspectRatio is truthly and we had previously determined _aspectRatio, use that
162+ return _aspectRatio ;
163+ }
164+
165+ // Calculate
166+ return height ? width / height : null ;
167+ }
168+
150169 get data ( ) {
151170 return this . config . data ;
152171 }
@@ -238,6 +257,7 @@ class Chart {
238257
239258 me . width = newSize . width ;
240259 me . height = newSize . height ;
260+ me . _aspectRatio = me . aspectRatio ;
241261 retinaScale ( me , newRatio , true ) ;
242262
243263 me . notifyPlugins ( 'resize' , { size : newSize } ) ;
0 commit comments