Skip to content

Charts don't resize appropriately for print view #1275

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

Closed
wallacedf opened this issue Dec 28, 2016 · 15 comments
Closed

Charts don't resize appropriately for print view #1275

wallacedf opened this issue Dec 28, 2016 · 15 comments
Labels
bug something broken community community contribution

Comments

@wallacedf
Copy link

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?

@etpinard
Copy link
Contributor

etpinard commented Jan 4, 2017

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.

@etpinard
Copy link
Contributor

etpinard commented Jan 4, 2017

Actually, (if I understand correctly) this has nothing to do with the fluid layout logic.

<crtl-p> on https://plot.ly/javascript/ gives:

image

@etpinard etpinard added the community community contribution label Jan 9, 2017
@czellweg
Copy link

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:
plotly-browser-screenshot

This is the generated image (snapshot button):
plotly-downloadedpng

Wouldn't it be possible to calculate the width/height of the div.svg-container and set those values?

@etpinard
Copy link
Contributor

@czellweg see #1576

@romanseidl
Copy link

romanseidl commented Aug 21, 2017

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.

@etpinard
Copy link
Contributor

So plotly charts are not really printable?

It should work fine once you set layout.width and layout.height.

@craigdrayton
Copy link

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

@romanseidl
Copy link

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.

@etpinard etpinard added the bug something broken label Dec 1, 2017
@RickAhlf
Copy link

RickAhlf commented Jul 2, 2019

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...

@antoinerg
Copy link
Contributor

antoinerg commented Sep 20, 2019

Here's a Codepen demonstrating the solution proposed in #1275 (comment) . It works well on Chromium.

@etpinard I think we should handle this automatically when config: {responsive: true} to enable printing.

@antoinerg
Copy link
Contributor

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.

@AnastasiyaDemi
Copy link

Is there any progress on this feature?

@jackparmer
Copy link
Contributor

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:

  • Completion of this feature to the Sponsor's satisfaction, in a manner coherent with the rest of the Plotly.js library and API
  • Tests for this feature
  • Long-term support (continued support of this feature in the latest version of Plotly.js)
  • Documentation at plotly.com/javascript
  • Possibility of integrating this feature with Plotly Graphing Libraries (Python, R, F#, Julia, MATLAB, etc)
  • Possibility of integrating this feature with Dash
  • Feature announcement on community.plotly.com with shout out to Sponsor (or can remain anonymous)
  • Gratification of advancing the world's most downloaded, interactive scientific graphing libraries (>50M downloads across supported languages)

Please include the link to this issue when contacting us to discuss.

arthurdejong added a commit to arthurdejong/munin-plot that referenced this issue Dec 12, 2021
Whether this actually works is heavily dependant on the browser and in
some cases the screen size. Support in Plotly is not completely there
and there are also various bugs in browsers, e.g.:

plotly/plotly.js#1275
https://bugs.chromium.org/p/chromium/issues/detail?id=697233
@gvwilson
Copy link
Contributor

gvwilson commented Jun 6, 2024

Hi - this issue has been sitting for a while, so as part of our effort to tidy up our public repositories I'm going to close it. If it's still a concern, we'd be grateful if you could open a new issue (with a short reproducible example if appropriate) so that we can add it to our stack. Cheers - @gvwilson

@gvwilson gvwilson closed this as completed Jun 6, 2024
@hidegh
Copy link

hidegh commented Apr 13, 2025

#7403

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken community community contribution
Projects
None yet
Development

No branches or pull requests