Chartjs chart broke when using Pagedjs

411 Views Asked by At

I am using Chartjs & Paged.js to print pages.

Before paged.preview

Everything is fine but when I call this code, the charts disappear:

  let flow = paged.preview().then((flow) => {
    console.log("Rendered", flow.total, "pages.");
  });

Are these two packages not getting along?

enter image description here

1

There are 1 best solutions below

0
On

I found a solution to this after help by the good people at https://pagedjs.org/

Add this to your <head>-tag:

<head>
    <script src="https://unpkg.com/pagedjs/dist/paged.polyfill.js"></script>
</head>

Now in your body, add the following Paged.js extension:

<body>
<div id="yourchart"></div>
<script>

 function generateChart() {
    // code for chart here.
 }

  class myChartCreator extends Paged.Handler {
            constructor(chunker, polisher, caller) {
                super(chunker, polisher, caller);
            }

            afterRendered(pages) {
              
               // add your chart generating code/calls here:

               generateChart();
               someOtherFunction();

               // some even longer running functions.
               return Promise.resolve(
                    someLongRunningStuff()
               );
               ...
             }
        }
        Paged.registerHandlers(myChartCreator);


</script>
</body>

The key is adding your chart code/calls to generate charts to the afterRendered method.

It works perfectly with both Chart.js and amcharts5.