question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resize charts on print.

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report => search github for a similar issue or PR before submitting
[X] feature request
[ ] support request => Please do not submit support request here

Current behavior When printing, the charts does not resize to fit paper.

Expected behavior Charts should be resized to fit paper.

Reproduction of the problem Use: https://swimlane.github.io/ngx-charts/#/ngx-charts/bar-vertical and enable “Fit to container”. Then press print. Depending on the size of the window, the graphs might not fit paper or appear partially behind other elements.

What is the motivation / use case for changing the behavior? Support printing (or export to PDF).

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:14

github_iconTop GitHub Comments

1reaction
arajaycommented, Oct 16, 2020

Here’s a solution that might help someone. I simply update the value of the [view] input with an arbitrary value that looks good on the page when I trigger the print dialog and then reset it after the dialog is closed.

<ngx-chart [view]="view"></ngx-chart>

 @HostListener('window:beforeprint')
  beforePrint() {
    this.view = [800, 350];
  }

  @HostListener('window:afterprint')
  afterPrint() {
    this.view = null;
  }
1reaction
wobkenhcommented, Mar 1, 2020

I think I might be headed in the right direction in terms of a workaround:

In my app.component, I subscribe to beforeprint and afterprint events. Those events are forwarded to a service.

    @HostListener("window:beforeprint", ["$event"])
    async beforePrint($event: any) {
        await this.printResizeService.beforePrint();
    }

    @HostListener("window:afterprint", ["$event"])
    async afterPrint($event: any) {
        await this.printResizeService.afterPrint();
    }

In the service, I resize the divs i wrapped around the charts (they all have the class “roc-chart-box”):

    async beforePrint() {
        console.log("Preparing printing");

        const elements = document.getElementsByClassName("roc-chart-box");
        for (let i = 0; i < elements.length; i++) {
            const element = elements.item(i);
            if (element) {
                const anyElement = element as any;
                const relation = anyElement.clientWidth / window.innerWidth;
                const newWidth = Math.round(800 * relation);
                console.log("New Width: " + newWidth);
                anyElement.style.width = newWidth + "px";
            }
        }
        this.charts.forEach(chart => {
            chart.update();
            chart.animations = false;
        });
        console.log("Waiting for changes to be displayed");
        const promise = new Promise((resolve) => {
            setTimeout(() => resolve(), 1000);
        });
        await promise;
        console.log("Ready to print");
    }

charts is an array of ngx charts (e.g. BarVerticalComponent) which are currently being displayed. Note how I disable animations and call update on each chart so it can resize quickly. To leave the chart a bit of time to resize, I await a promise which waits for 1 second. Also note that when resizing the chart wrapper div, all elements still have their old size - not the size they will have in the printed version.

After the print, I revert the changes:

    afterPrint() {
        console.log("Reverting print changes");
        const elements = document.getElementsByClassName("roc-chart-box");
        for (let i = 0; i < elements.length; i++) {
            const element = elements.item(i);
            if (element) {
                const anyElement = element as any;
                anyElement.style.width = "";
            }
        }
        this.charts.forEach(chart => {
            chart.update();
            chart.animations = true;
        });
    }

Problem with this solution is that charts are resized according to a fixed page width (800px in this case). This is the value that yielded the best results for me in Chrome. However, if I try other browsers or choose landscape mode when printing, the fixed width solution does not work anymore.

I post this in the hope of someone being able to improve this workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Move or resize a chart - Microsoft Support
To resize a chart, do one of the following: To change the size manually, click the chart, and then drag the sizing handles...
Read more >
Resize chart.js canvas for printing - Stack Overflow
The solution is to render your chart to an image using the canvas.toDataURL() method when printing is invoked, then switch it back to...
Read more >
Excel Chart sizes are different when printed and viewed on ...
Solution for where the Excel Chart sizes are different when printed (or even in print preview) as compared to what you see on...
Read more >
Responsive Charts - Chart.js
js is unable to properly resize for the print layout. To work around this, you can pass an explicit size to .resize() then...
Read more >
Resizing the chart for print - amCharts Help & Support - Zendesk
Usually browsers use different resolution when printing web pages. If you have charts that are wider than ~600-700 pixels, they will print as ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found