Skip to content

Commit 3580851

Browse files
authoredOct 15, 2020
Merge pull request #5209 from plotly/nicolaskruchten-patch-3
Force default dims for downloadImage to null
2 parents 8128c88 + 5e98788 commit 3580851

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed
 

‎src/snapshot/download.js

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ function downloadImage(gd, opts) {
3131

3232
opts = opts || {};
3333
opts.format = opts.format || 'png';
34+
opts.width = opts.width || null;
35+
opts.height = opts.height || null;
3436
opts.imageDataOnly = true;
3537

3638
return new Promise(function(resolve, reject) {

‎src/traces/image/calc.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ var constants = require('./constants');
1313
var isNumeric = require('fast-isnumeric');
1414
var Axes = require('../../plots/cartesian/axes');
1515
var maxRowLength = require('../../lib').maxRowLength;
16-
var sizeOf = require('image-size');
17-
var dataUri = require('../../snapshot/helpers').IMAGE_URL_PREFIX;
18-
var Buffer = require('buffer/').Buffer; // note: the trailing slash is important!
16+
var getImageSize = require('./helpers').getImageSize;
1917

2018
module.exports = function calc(gd, trace) {
2119
var h;
@@ -96,10 +94,3 @@ function makeScaler(trace) {
9694
return c;
9795
};
9896
}
99-
100-
// Get image size
101-
function getImageSize(src) {
102-
var data = src.replace(dataUri, '');
103-
var buff = new Buffer(data, 'base64');
104-
return sizeOf(buff);
105-
}

‎src/traces/image/helpers.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2012-2020, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var sizeOf = require('image-size');
12+
var dataUri = require('../../snapshot/helpers').IMAGE_URL_PREFIX;
13+
var Buffer = require('buffer/').Buffer; // note: the trailing slash is important!
14+
15+
exports.getImageSize = function(src) {
16+
var data = src.replace(dataUri, '');
17+
var buff = new Buffer(data, 'base64');
18+
return sizeOf(buff);
19+
};

‎test/jasmine/tests/download_test.js

+28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var Plotly = require('@lib/index');
22
var Lib = require('@src/lib');
33

44
var helpers = require('@src/snapshot/helpers');
5+
var getImageSize = require('@src/traces/image/helpers').getImageSize;
56

67
var createGraphDiv = require('../assets/create_graph_div');
78
var destroyGraphDiv = require('../assets/destroy_graph_div');
@@ -164,6 +165,33 @@ describe('Plotly.downloadImage', function() {
164165
.catch(failTest)
165166
.then(done);
166167
});
168+
169+
it('should default width & height for downloadImage to match with the live graph', function(done) {
170+
spyOn(Lib, 'isSafari').and.callFake(function() { return true; });
171+
spyOn(helpers, 'octetStream');
172+
173+
var fig = {
174+
data: [{y: [0, 1]}]
175+
};
176+
177+
gd.style.width = '500px';
178+
gd.style.height = '300px';
179+
180+
Plotly.plot(gd, fig)
181+
.then(function() { return Plotly.downloadImage(gd, {format: 'png'}); })
182+
.then(function() {
183+
var args = helpers.octetStream.calls.allArgs();
184+
var blob = args[0][0];
185+
expect(blob.slice(0, 8)).toBe(';base64,', 'format:png');
186+
var size = getImageSize('data:image/png' + blob);
187+
expect(size.width).toBe(gd._fullLayout.width, 'fullLayout width');
188+
expect(size.height).toBe(gd._fullLayout.height, 'fullLayout height');
189+
expect(size.width).toBe(500, 'div width');
190+
expect(size.height).toBe(300, 'div height');
191+
})
192+
.catch(failTest)
193+
.then(done);
194+
});
167195
});
168196

169197
function downloadTest(gd, format) {

0 commit comments

Comments
 (0)
Please sign in to comment.