Skip to content Skip to sidebar Skip to footer

Chartjs + Jspdf = Blurry Image

I'm having some issues exporting my Charts to PDF. I have this div

Documentation: https://www.chartjs.org/docs/3.0.2/configuration/device-pixel-ratio.html Works wonderfully ...

Solution 3:

I've been working on a project trying to produce graphs with chartjs and then printing them using Chrome's print to PDF functionality, and I found that the chartjs graphs look poor. After reading various threads both on stackoverflow and github I developed one solution that worked well enough for me.

In my particular case I need the graphs at a fixed size and I can't have them be responsive because I need them to fit within the printed page correctly. I use style tags to set the size:

<canvas style="width: 300px; height: 300px" />

I've found that if you set responsive: false in the chart and then use the style tags like that, Chartjs won't mess with the size of the chart. Using any other method like setting width or height (not the style width or height) or using css classes will not set it properly. Chartjs only seems to work when I set the element's inline style tag for this.

Anyway, the trick that worked for me in getting better PDF output was to have Chartjs render a larger chart and then scaling it down to a smaller size so it fits on my page correctly.

Let's say for some reason we want a 300x300 pixel chart and that it looks poor when we print it to PDF. We need to have Chartjs draw this chart into a larger chart and then resize it down to 300x300. In my own project I am having Chartjs draw it 2x as large. So for this example I would make a canvas element that is 600x600 as follows:

<canvas style="width: 600px; height: 600px;"class="graph" />

At the same time I have a "graph" css class with height and width set to 300px. The chart will not render at 300px because of the inline style however.

You can then make the chart as you normally would, but immediately after the line of code that makes the chart, you remove the inline style tag from the chart. I found that when you do this, chartjs will draw the chart to the larger 600x600 size but then the chart instantly gets resized to 300x300. Here is an example of what the code looks like:

var ctx = canvas.getContext('2d'); var mychart = new Chart( ctx ,{ .... }); canvas.removeAttribute("style");

The canvas.removeAttribute removes the inline style tag, so then the css class takes effect and instantly causes the canvas to re-render at the smaller size. There is no flash or any indication that this has happened, yet I've found that you get a much higher quality looking chart.

There is one other issue with this. You will have to design your chart for the larger 600x600 size for example in order to get it to look right. When you draw the chart at a larger size, the lines and fonts don't get resized so everything looks really tiny. I had to set my chart manually to the larger size to design it and figure out good fonts and line sizes for the graph first, and then do the resize trick here.

I have also found that simply using the smaller sized chart and making thicker lines or larger fonts does not seem to have the same effect as sizing everything up first, and then rendering it as a smaller size.

Post a Comment for "Chartjs + Jspdf = Blurry Image"