JSFiddle - React, Tailwind, and code Playground

by ph822720355

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<html>
  <head>
    <link rel="stylesheet" href="https://printjs-4de6.kxcdn.com/print.min.css">
  </head>
  <body>
    <canvas id="chart"></canvas>
    <br>
    <button id="download">Print</button>
  </body>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
  <script src="https://printjs-4de6.kxcdn.com/print.min.js"></script>
</html>

JavaScript

// Chart details
var ctx = document.getElementById('chart').getContext('2d');
var chart = new Chart(ctx, {
    // The type of chart we want to create
    type: 'line',

    // The data for our dataset
    data: {
        labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
        datasets: [{
            label: 'My First dataset',
            backgroundColor: 'rgb(255, 99, 132)',
            borderColor: 'rgb(255, 99, 132)',
            data: [0, 10, 5, 2, 20, 30, 45],
            footer: '<h3>Page {pageNumber} of {totalPages}</h3>',
        }]
    },

    // Configuration options go here
    options: {}
});

document.getElementById('download').onclick = () => {
	printJS({printable:'chart', type:'html'});
}