Skip to content

Commit

Permalink
add check on overwriting canvas height/width (chartjs#4874)
Browse files Browse the repository at this point in the history
* add check on overwriting canvas height/width

* unit test for this
  • Loading branch information
andersponders authored and yofreke committed Dec 30, 2017
1 parent 3a0d062 commit 323d8b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/core/core.helpers.js
Expand Up @@ -512,8 +512,10 @@ module.exports = function(Chart) {
// If no style has been set on the canvas, the render size is used as display size,
// making the chart visually bigger, so let's enforce it to the "correct" values.
// See https://github.com/chartjs/Chart.js/issues/3575
canvas.style.height = height + 'px';
canvas.style.width = width + 'px';
if (!canvas.style.height && !canvas.style.width) {
canvas.style.height = height + 'px';
canvas.style.width = width + 'px';
}
};
// -- Canvas methods
helpers.fontString = function(pixelSize, fontStyle, fontFamily) {
Expand Down
17 changes: 17 additions & 0 deletions test/specs/core.helpers.tests.js
Expand Up @@ -725,6 +725,23 @@ describe('Core helper tests', function() {
document.body.removeChild(div);
});

it ('should leave styled height and width on canvas if explicitly set', function() {
var chart = window.acquireChart({}, {
canvas: {
height: 200,
width: 200,
style: 'height: 400px; width: 400px;'
}
});

helpers.retinaScale(chart, true);

var canvas = chart.canvas;

expect(canvas.style.height).toBe('400px');
expect(canvas.style.width).toBe('400px');
});

describe('Color helper', function() {
function isColorInstance(obj) {
return typeof obj === 'object' && obj.hasOwnProperty('values') && obj.values.hasOwnProperty('rgb');
Expand Down

0 comments on commit 323d8b0

Please sign in to comment.