score:1
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.
score:6
since a few versions chart.js has the parameter devicepixelratio. by default, the canvas is rendered in the dpi number by monitor, so 96 or retina - not ideal for a printout, but perfect for the screen.
if you increase this value, more pixels are created. expand the value so that you can export the chart in print quality as a base64 image. the value does not affect the display of the chart on the monitor.
in my case, i set the value to 1.5.
options:{
devicepixelratio: 1.5
}
documentation: https://www.chartjs.org/docs/3.0.2/configuration/device-pixel-ratio.html works wonderfully ...
score:10
the resolution comes from the canvas size. the more you increase your canvas (width and height), the better will be the resolution when downloading your pdf.
however, you probably don't want to increase the canvas size too much, so, one trick you could use is to create a hidden canvas, with a higher width and height, use it to print the chart and create the pdf, getting a better pdf quality.
here is a fiddle demonstrating this, with an option to download a pdf created from the original canvas/chart, and another option to download a new pdf from the hidden canvas/chart. you can see how the quality increase quite a bit when comparing both results.
https://jsfiddle.net/crabbly/kl68ey5z/
i don't think this is the best solution, however, it is the only way i could come up to increase the quality of my pdf chart files. i'm currently playing around both libraries, specially how jspdf treats the w
and h
arguments when creating the docs.
also, chart.js does come with a built in function to extract an image form the chart (.tobase64image()
), however, the quality seems to be worse when i tested.
Source: stackoverflow.com
Related Query
- Chartjs + jsPDF = Blurry image
- How to add ChartJS code in Html2Pdf to view image
- Adding Image inside Linechart points in ChartJs
- ChartJS and jsPDF - why the background is black?
- Show image instead a point in a ChartJS scatter plot
- unable to add background color to the canvas using jspdf and chartjs
- Adding image on the top of bar in chartjs
- why is the background black when with Chartjs export to a image
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- Chartjs not working with d3 from csv source
- Render chartjs and export image to data not in the DOM
- Chartjs - resize an image positioned in a column
- ChartJS have xAxes labels match data source
- Updating Chartjs to 2.5 with custom code
- Calling data from outside of Chartjs code
- VueJS + Chartjs - Chart only renders after code change
- ChartJS 2.0 differences in code help needed
- My Chartjs is not updating after setstate full code attached Reactjs Update
- Exporting dynamic chartjs to jspdf
- chartjs maintain points position, put image alongside with label
- Chartjs Exporting ImageExports Empty Dark Image
- Chartjs doc examples are lightning fast but same code is slow when reproducing
- Add an image as background in ChartJS chart area [Not to canvas]
- How to get the image from graph created using chartjs
- Chartjs bar chart blurry when height is set
- 'require is not defined' error when attempting to use chartjs in javascript code
- How to run Chart.js samples using source code
- Reloading ChartJs Image
- Code for Chartjs Typescript definition file
- Javascript highcharts code similar to image
More Query from same tag
- Populating a chartJS with data from API through axios (VueJS)
- Chart JS: Ignoring x values and putting point data on first available labels
- How to use date in x value for Object[] in chartjs dataset, I am getting an error: 'TS2322: Type 'string' is not assignable to type 'number'.'
- Is there a way to export chartjs-chart-treemap?
- Chart Js - Limit points on graph without limiting data
- Why is my chart not working? What am I missing
- Angularjs - Can't call the value from form to graph
- chartjs create scrolling on the x axis
- Select data based on value of ID
- How to draw lines between minimum and maximum value to average value in chartJS?
- show multiple responsive chart in the same page using chart.js
- Displaying the first value of the array in datasets.label Chartjs
- How To Format Scale Numbers in Chart.js v2
- Label issue in doughnut chart using chartjs
- Problem creating scatter graph using chart.js
- Why chart.js charts are not plotting data in Safari (works in Chrome)
- Chartjs 3.5.0 - Radar Chart - Converting the labels to images
- Cannot find centroid of path
- ChartJS and Radar Chart animation
- how to highlight bar in ChartJS with onkeyup Input
- Highlighting point in all datasets on hover over
- Completely hide empty bars in chart.js
- then function not returning values Angular js
- Trouble with setting background color for Line chart at Chartjs version 3.5.1
- Chart.js Scaling issue when initially loading the page
- How to insert arrays into objects that are inside an array? For ng2-charts / Charts.js
- How to replace the data array of my ChartJS
- ChartJS 2 How to globally remove xAxis gridLines?
- vb.net string to chartjs data field?
- Chart.js displaying time data