Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 14 additions & 4 deletions src/components/modebar/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,29 @@ var modeBarButtons = module.exports = {};

modeBarButtons.toImage = {
name: 'toImage',
title: function(gd) { return _(gd, 'Download plot as a png'); },
title: function(gd) { return _(gd, 'Download plot'); },
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can't make dynamic strings here, so _(gd, 'Download plot as a ' + format) won't work, but perhaps we can keep 'Download plot as a png' as the default string and only degrade to 'Download plot' if it's not a png? Two reasons:

  • I don't want to unnecessarily break all our existing translations, for folks who are not using this new option.
  • I do find "as a png" helpful, to clarify that it's not the data or json for example, though I guess the icon mitigates that substantially.

icon: Icons.camera,
click: function(gd) {
var format = 'png';
var toImageButtonDefaults = gd._context.toImageButtonDefaults;
var opts = {format: toImageButtonDefaults.format || 'png'};

Lib.notifier(_(gd, 'Taking snapshot - this may take a few seconds'), 'long');

if(Lib.isIE()) {
Lib.notifier(_(gd, 'IE only supports svg. Changing format to svg.'), 'long');
format = 'svg';
opts.format = 'svg';
Copy link
Collaborator

Choose a reason for hiding this comment

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

also we should change the conditional to if(Lib.isIE() && opts.format !== 'svg')

}

Registry.call('downloadImage', gd, {'format': format})
if(toImageButtonDefaults.width) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice. No need for isNumeric() here toImage handles it already:

var format = coerce('format');
var width = coerce('width');
var height = coerce('height');
var scale = coerce('scale');
var setBackground = coerce('setBackground');
var imageDataOnly = coerce('imageDataOnly');

opts.width = toImageButtonDefaults.width;
}
if(toImageButtonDefaults.height) {
opts.height = toImageButtonDefaults.height;
}
if(toImageButtonDefaults.filename) {
opts.filename = toImageButtonDefaults.filename;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@nicolaskruchten we might as well add scale

scale: {
valType: 'number',
min: 0,
dflt: 1,
description: [
'Sets a scaling for the generated image.',
'If set, all features of a graphs (e.g. text, line width)',
'are scaled, unlike simply setting',
'a bigger *width* and *height*.'
].join(' ')
},

Registry.call('downloadImage', gd, opts)
.then(function(filename) {
Lib.notifier(_(gd, 'Snapshot succeeded') + ' - ' + filename, 'long');
})
Expand Down
4 changes: 4 additions & 0 deletions src/plot_api/plot_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ module.exports = {
*/
modeBarButtons: false,

// statically override options for toImage modebar button
// allowed keys are format, filename, width, height
toImageButtonDefaults: {},

// add the plotly logo on the end of the mode bar
displaylogo: true,

Expand Down