Print chart using chart js
Solution 1
This function prints the contents of a Canvas correctly
function PrintImage() {
var canvas = document.getElementById("bidStatus");
var win = window.open();
win.document.write("<br><img src='" + canvas.toDataURL() + "'/>");
win.print();
win.location.reload();
}
Solution 2
I was able to use jsPDF to print a canvas from chart.js with Chrome using the code below. There is already an accepted answer to this question, but as someone else pointed out in the comments, I could not get that solution to work with Chrome.
Here is a link to jsPDF documentation. I am still exploring this so you may find more useful tools to accomplish the same task. I had to use a PNG instead of a JPEG because of the transparent background of the chart. The JPEG would only appear black. If you want a colored background, add a colored rectangle(rect method in docs) that is the same size and position of the chart image before adding the image. Other text and features can also be added to the pdf you are building.
I do not like that the chart image has to be added at a specific location and size and will update this post if I find a better way of doing this.
EDIT: With jsPDF you can use pdf.save('Filename.pdf') to replace FileSaver.js, provided you want a pdf.
HTML
<button id="print-chart-btn">Print Chart</button>
<div class="canvas-container">
<canvas id="random-chart" ></canvas>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jspdf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
JAVASCRIPT
$('#print-chart-btn').on('click', function() {
var canvas = document.querySelector("#random-chart");
var canvas_img = canvas.toDataURL("image/png",1.0); //JPEG will not match background color
var pdf = new jsPDF('landscape','in', 'letter'); //orientation, units, page size
pdf.addImage(canvas_img, 'png', .5, 1.75, 10, 5); //image, type, padding left, padding top, width, height
pdf.autoPrint(); //print window automatically opened with pdf
var blob = pdf.output("bloburl");
window.open(blob);
});
SP1
Updated on June 28, 2022Comments
-
SP1 almost 2 years
I am using Chart JS library to create charts https://www.chartjs.org/
Say I have the below code
<div class="card-body "> <canvas id="bidStatus"></canvas> </div>
Using the FileSaver.js I am able to save the chart using the below code
function DownloadImage() { $("#bidStatus").get(0).toBlob(function (blob) { saveAs(blob, "BidStatus.png"); }); }
But I am not sure how I can print the chart. Don't see any native API call to do that. Can some one please tell me how I can achieve this.
I tried using jquery print libraries which are mentioned in the Print an HTMl element example but they don't seem to load the chart generated using Chart js. I get a blank page for printing.
Thanks
-
Antonios Tsimourtos about 5 yearsNot working in Chrome, just opens a new tab blank page!