-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
add new promise-based toImage and downloadImage to Plotly #446
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
add new promise-based toImage and downloadImage to Plotly #446
Conversation
toImage
and downloadImage
to Plotlyopts.format = opts.format || 'png'; | ||
|
||
if( | ||
(opts.width && isNumeric(opts.width) && opts.width < 1) || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐄 Let's set these as values above so the if
is a bit more clean.
Or define a quick helper function to call on opts.____
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and isNumeric(undefined || null)
returns false
.
So, opts.width && isNumeric(opts.width)
is equivalent to isNumeric(opts.width)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but the opts.width
and opts.height
might be undefined
or null
, and those are valid options. In the case of null
|| undefined
we use layout height and width.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. I see. 👍 My mistake
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/*eslint dot-notation: [2, {"allowPattern": "^catch$"}]*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove the {"allowKeywords": false}
option in the eslintrc
file and remove this line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, this was just copy/paste.
@timelyportfolio looking good. This will make a lot folks happy. You'll need one good linting commit. You can run Let me know if you need any clarifications about #446 (comment) |
0a059ce
to
6124b99
Compare
@etpinard, how should I handle these
|
We should remove the "dot-notation": [2, {"allowKeywords": false}] should become "dot-notation": [2] After some research, this only breaks things in ECMAScript version 3. So, let's not worry about it. |
582332b
to
cea7c5e
Compare
@@ -55,17 +55,15 @@ modeBarButtons.toImage = { | |||
return; | |||
} | |||
|
|||
if(gd._snapshotInProgress) { | |||
Lib.notifier('Snapshotting is still in progress - please hold', 'long'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timelyportfolio why did you review this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind. I see. It's here now: https://github.com/plotly/plotly.js/pull/446/files#diff-40d98cbce238527d9bffcf2ecd4efa9bR33
Seems to work just peachy on Safari. 👍 |
fb6eeea
to
bc96343
Compare
* By Eli Grey, http://eligrey.com | ||
* License: MIT | ||
* See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@etpinard, is this ok for attribution?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Nicely done. You'll need to add this block:
/**
* Copyright 2012-2016, 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.
*/
at the top of the file to make npm run test-syntax
happy.
@timelyportfolio @mdtusz I tested out First, I discovered a bug in After fixing that bug, Maybe IE11 doesn't like |
@etpinard, and the fun continues :). I have had this error before in an unrelated project, but I cannot find any notes. So, before IE was not supported at all, but it seems we should take the opportunity to get this working in all the browsers. Maybe I write a function for both |
It's possible we just need to specify the encoding for it to work in IE 11 as shown here. |
Yes. That sounds good. We have a lib function |
@mdtusz bringing back good memories, but that just goes back to base64 which is what we were doing with |
@timelyportfolio well deserved 🍻 |
Hi. Thanks for all your help on here. I've tried using the latest release to save an SVG. I call the downloadImage function, using the "svg" format option. However, the SVG that is dowloaded can be viewed by chrome, but not illustrator or adobe acrobat. Any thoughts? I looks like you worked on addressing this directly, but I'm still struggling with it. Really want to be able to download my plotly plots as some form of SVG graphic. |
@yniknafs could you share the data and layout from your example if possible, or better yet, the downloaded svg file for me to inspect? Support with Illustrator is mostly there for most plots, but there are still some edge cases of things that Illustrator's svg parser/renderer doesn't like, and unfortunately, there are very sparse error messages and documentation on what exactly their requirements are - it certainly doesn't adhere to the svg spec. |
@mdtusz Thanks so much for the reply. We have a number of plots (bar, box, pie) on the site we are developing, and the svg --> illustrator wasn't working with any of the plots. I have attached the plot. The data is coming from a seql db, can't really paste here, but you can see it in the attached plot. SVG was downloaded using the modebar (changed modebar code to have "format: 'svg';" in /modebar/buttons.js). Here is the code to call the plot. var expression_layout = {
title: 'TPM vs. Transcripts',
xaxis: {
title: 'Transcripts',
showgrid: false,
zeroline: false
},
yaxis: {
title: 'TPM',
showline: false
},
boxmode: 'group'
};
function redraw_expression_plot(json_response) {
var data = [];
for (var key in json_response) {
var trace = {
y: json_response[key][1],
x: json_response[key][0],
type: 'box',
name: key
}
data.push(trace);
}
expression_layout.yaxis.title = "TPM";
Plotly.newPlot('expression_plot', data, expression_layout);
highlight_linear();
highlight_box();
} |
@yniknafs using Adobe Illustrator CC, I'm able to open the svg and it looks ok to me. I don't have Acrobat installed however so I can't verify that either. The main issue we encountered from before was with respect to opacities and quotation marks, but looking through the SVG neither of those issues seem to be present. What sort of warnings does Illustrator spit out at you when you attempt to open it? |
@mdtusz thanks for that. quite interesting. I just download Adobe Illustrator CC and it worked. I have Adobe CS6 installed. And it did not work (see images). Any chance you know why its not working with the CS version? Will be a little limiting to force users to have CC (I'm sure lots of people using our tool will have some illustrator CS5/6 installed). |
@yniknafs @mdtusz FWIW, a quick sanity-check with the W3C validator shows the file is 100% valid. A workaround: I was able to open it in Illustrator CS5 by first running it through Inkscape to convert to PDF. For mac/linux (and probably windows too), Inkscape can be invoked from the command line: $ inkscape /Users/rreusser/Desktop/newplot.svg --export-pdf=/Users/rreusser/Desktop/newplot.pdf which is nice, though plenty weird that you must pass it a full path to the file. It's not a great solution, but if it's the blocker, it's something. |
Please could someone give an example of how to use this with the Python Notebook API. |
Thanks for the pointer. That's very useful. However, it doesn't seem to be working quite as advertised. I am on OS X and have tried both Safari and Firefox. I do get a picture which is downloaded to my 'Downloads' folder after the Dialog, but it is given the name 'Unknown' and no extension. I even tried setting the option |
My apologies, it works in Firefox. ETA. Spoke too soon. The .svg is mangled from Firefox. |
@a59 http://community.plot.ly/ is the best place to look for help. |
Hello folks, Essentially it's the same issue as was originally raised here: I'm not sure if this pull request is supposed to fix this or not, or it's already included in the Plotly CDN? Any help would be appreciated. Thanks, |
@etpinard I apologize yes, it was the #50 and #42 I'm using the plotly CDN ( https://cdn.plot.ly/plotly-latest.js ) This is what the chart looks like: This is what the downloaded png looks like: Please let me know what other info I can provide. Thanks. |
PNG toImage for IE Edge works if img.crossOrigin is set to 'Anonymous'. The tainted canvas is fixed for the Edge browser (and works with this one change), but apparently will not be fixed for IE11. https://connect.microsoft.com/IE/feedback/details/828416/cavas-todataurl-method-doesnt-work-after-draw-svg-file-to-canvas |
ref original pull timelyportfolio#1 for very long discussion and commits
Addresses
Summary of Changes
toImage
directly toPlotly
downloadImage
directly toPlotly
Plotly.Snapshot
for backward compatibility with exception of addingpromise
option insvgToImg
Example Usage
Plotly.toImage
Plotly.downloadImage