Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force default dims for downloadImage to null #5209

Merged
merged 2 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/snapshot/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ function downloadImage(gd, opts) {

opts = opts || {};
opts.format = opts.format || 'png';
opts.width = opts.width || null;
opts.height = opts.height || null;
opts.imageDataOnly = true;

return new Promise(function(resolve, reject) {
Expand Down
11 changes: 1 addition & 10 deletions src/traces/image/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ var constants = require('./constants');
var isNumeric = require('fast-isnumeric');
var Axes = require('../../plots/cartesian/axes');
var maxRowLength = require('../../lib').maxRowLength;
var sizeOf = require('image-size');
var dataUri = require('../../snapshot/helpers').IMAGE_URL_PREFIX;
var Buffer = require('buffer/').Buffer; // note: the trailing slash is important!
var getImageSize = require('./helpers').getImageSize;

module.exports = function calc(gd, trace) {
var h;
Expand Down Expand Up @@ -96,10 +94,3 @@ function makeScaler(trace) {
return c;
};
}

// Get image size
function getImageSize(src) {
var data = src.replace(dataUri, '');
var buff = new Buffer(data, 'base64');
return sizeOf(buff);
}
19 changes: 19 additions & 0 deletions src/traces/image/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2012-2020, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var sizeOf = require('image-size');
var dataUri = require('../../snapshot/helpers').IMAGE_URL_PREFIX;
var Buffer = require('buffer/').Buffer; // note: the trailing slash is important!

exports.getImageSize = function(src) {
var data = src.replace(dataUri, '');
var buff = new Buffer(data, 'base64');
return sizeOf(buff);
};
28 changes: 28 additions & 0 deletions test/jasmine/tests/download_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Plotly = require('@lib/index');
var Lib = require('@src/lib');

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

var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
Expand Down Expand Up @@ -164,6 +165,33 @@ describe('Plotly.downloadImage', function() {
.catch(failTest)
.then(done);
});

it('should default width & height for downloadImage to match with the live graph', function(done) {
spyOn(Lib, 'isSafari').and.callFake(function() { return true; });
spyOn(helpers, 'octetStream');

var fig = {
data: [{y: [0, 1]}]
};

gd.style.width = '500px';
gd.style.height = '300px';

Plotly.plot(gd, fig)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this test would fail without my change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃 if so!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this test would fail without my change?

Yes it used to fail without the changes proposed by you.
Thanks very much @nicolaskruchten for fixing this bug!

.then(function() { return Plotly.downloadImage(gd, {format: 'png'}); })
.then(function() {
var args = helpers.octetStream.calls.allArgs();
var blob = args[0][0];
expect(blob.slice(0, 8)).toBe(';base64,', 'format:png');
var size = getImageSize('data:image/png' + blob);
expect(size.width).toBe(gd._fullLayout.width, 'fullLayout width');
expect(size.height).toBe(gd._fullLayout.height, 'fullLayout height');
expect(size.width).toBe(500, 'div width');
expect(size.height).toBe(300, 'div height');
})
.catch(failTest)
.then(done);
});
});

function downloadTest(gd, format) {
Expand Down