Currently, if you have a fluid chart and resize the browser window, the chart will be resized correctly to fit in the new available width. That is working fine. However, the same does not happen when you try to print that chart.
The easiest way to reproduce the issue is looking at the official response fluid chart example: https://plot.ly/javascript/responsive-fluid-layout/
If you try to print the page, you'll notice that the chart is cut off. The window.resize() function is actually called but, for some reason, it looks like the chart still reflects the width of the normal browser window rather than the width of the print view:
http://codepen.io/anon/pen/vyoBrW
Could you please check this issue?
not happen when you try to print that chart.
Ha. I'm surprised that any plotly graphs are displayed properly when printed.
I'd recommend exporting your plotly graphs as images either using the snapshot mode bar button or Plotly.toImage for better -- officially supported -- results.
Actually, (if I understand correctly) this has nothing to do with the fluid layout logic.
<crtl-p> on https://plot.ly/javascript/ gives:

I'd recommend exporting your plotly graphs as images either using the snapshot mode bar button or Plotly.toImage for better -- officially supported -- results.
When the snapshot mode bar button is used, the generated image seems to have the default dimensions (700x450px) - regardless of how wide the actually displayed graph in the browser (before taking the snapshot) is.
This is what the browser shows:

This is the generated image (snapshot button):

Wouldn't it be possible to calculate the width/height of the div.svg-container and set those values?
@czellweg see https://github.com/plotly/plotly.js/issues/1576
So plotly charts are not really printable? This is a major issue.
And no, an image won't solve this, an image is nothing that should be considered when you have svg. Also a printable image would have to be 300dpi+ in resolution. What you seen on screen is not what is to be seen on paper.
So plotly charts are not really printable?
It should work fine once you set layout.width and layout.height.
I was able to solve this with the following:
/* params divId & chartNode */
// Resize the chart if the window size changes
window.addEventListener('resize', () => {
const chartDiv = $(`#${divId}`);
Plotly.relayout(chartNode, { width: chartDiv.width(), height: chartDiv.height() });
});
// Resize Plotly charts for printing
const resizeChartWidth = () => {
const chartDiv = $(\`#${divId}\`);
Plotly.relayout(chartNode, { width: chartDiv.width() });
};
if (window.matchMedia) { // Webkit
window.matchMedia('print').addListener((print) => {
if (print.matches) {
resizeChartWidth();
}
});
}
window.onbeforeprint = resizeChartWidth; // FF, IE
I had a look at the code for relayout and it seems to depend on the chart and its elements if it is synchronous or asynchronous. And this trick supposedly only works with synchronous calls.
But I'll validate that and report the results.
Has there been any progress on this issue? I am running into a similar problem when attempting to print dcc.Graph components sized by viewport units. The print preview does not seem to respect any custom CSS that attempts to redefine the height and width of the components for @media print queries.
@craigdrayton your solution looks promising. Could you clarify what exactly chartNode is? It looks like divId is a prop of the plotly.js library according to the docs, but I don't see anything referencing chartNode unless it is user-defined in your implemented solution...
Here's a Codepen demonstrating the solution proposed in https://github.com/plotly/plotly.js/issues/1275#issuecomment-334912174 . It works well on Chromium.
@etpinard I think we should handle this automatically when config: {responsive: true} to enable printing.
One challenge would be to test that solution in an automated fashion. For example, we would have to make sure we resize the figures back to their original sizes when closing the print dialog.
Is there any progress on this feature?
This issue has been tagged with NEEDS SPON$OR
A community PR for this feature would certainly be welcome, but our experience is deeper features like this are difficult to complete without the Plotly maintainers leading the effort.
Sponsorship range: $10k-$15k
What Sponsorship includes:
Please include the link to this issue when contacting us to discuss.
Most helpful comment
I was able to solve this with the following: