Skip to content

Commit 4ef04a8

Browse files
authored
Allow changing the aspect ratio (#8659)
* Allow changing the aspect ratio * Add test and require `resize()` call * Update to respect maintainAspectRatio
1 parent 851861e commit 4ef04a8

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/core/core.controller.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import PluginService from './core.plugins';
77
import registry from './core.registry';
88
import Config, {determineAxis, getIndexAxis} from './core.config';
99
import {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';
1111
import {clearCanvas, clipArea, unclipArea, _isPointInArea} from '../helpers/helpers.canvas';
1212
// @ts-ignore
1313
import {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});

test/specs/core.controller.tests.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,38 @@ describe('Chart', function() {
12131213
});
12141214
});
12151215

1216+
describe('config.options.aspectRatio', function() {
1217+
it('should resize the canvas when the aspectRatio option changes', function(done) {
1218+
var chart = acquireChart({
1219+
options: {
1220+
responsive: true,
1221+
aspectRatio: 1,
1222+
}
1223+
}, {
1224+
canvas: {
1225+
style: '',
1226+
width: 400,
1227+
},
1228+
});
1229+
1230+
expect(chart).toBeChartOfSize({
1231+
dw: 400, dh: 400,
1232+
rw: 400, rh: 400,
1233+
});
1234+
1235+
waitForResize(chart, function() {
1236+
expect(chart).toBeChartOfSize({
1237+
dw: 400, dh: 200,
1238+
rw: 400, rh: 200,
1239+
});
1240+
1241+
done();
1242+
});
1243+
chart.options.aspectRatio = 2;
1244+
chart.resize();
1245+
});
1246+
});
1247+
12161248
describe('controller.reset', function() {
12171249
it('should reset the chart elements', function() {
12181250
var chart = acquireChart({

0 commit comments

Comments
 (0)