Get the Image of Chart from chartjs - Javascript Chart.js

Javascript examples for Chart.js:Chart Configuration

Description

Get the Image of Chart from chartjs

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>ChartJS Line Chart</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//  w  w  w  .ja  v  a2  s.c o m
Chart.defaults.global.animationSteps = 0;
var ctx = document.getElementById('myChart').getContext('2d');
new Chart(ctx, {
          type: 'line',
              data: {
              labels: ["July","August","September","October","November","December","January","February","March","April"],
              datasets: [
                  {
                      label: 'This year',
                      data: [340,0,450,3240,360,200,0,0,1445,350],
                  }
                  , {
                      label: 'Last year',
                      data: [330,340,100,160,560,3600,320,0,397.5,300],
                  }
              ]
          },
          options: {
              maintainAspectRatio: false,
              responsive: true,
              animation: {
                    duration: 0,
                  onComplete: function(animation){
                      document.querySelector('#myChart').remove();
                      document.querySelector('#myImage').setAttribute('src', this.toBase64Image());
                  }
              }
          }
      });
    }

      </script> 
   </head> 
   <body>  
      <canvas id="myChart" width="600" height="400" style="visibility:hidden;"></canvas>  
      <img id="myImage" src="" alt="">  
   </body>
</html>

Related Tutorials