Converting Chart.js canvas chart to image using .toDataUrl() - Javascript Chart.js

Javascript examples for Chart.js:Chart Data

Description

Converting Chart.js canvas chart to image using .toDataUrl()

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <style id="compiled-css" type="text/css">

canvas{/*from  w w w.j a v a 2 s .  co  m*/
}
img {
   border: 1px dashed red;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){
var lineChartData = {
         labels : ["January","February","March","April","May","June","July"],
         datasets : [
            {
               fillColor : "rgba(220,220,220,0.5)",
               strokeColor : "rgba(220,220,220,1)",
               pointColor : "rgba(220,220,220,1)",
               pointStrokeColor : "#fff",
               data : [65,59,90,81,56,55,40],
               bezierCurve : false
            }
         ]
      }
function done(){
  console.log("haha");
  var url=myLine.toBase64Image();
  document.getElementById("url").src=url;
}
var options = {
  bezierCurve : false,
  animation: {
    onComplete: done
  }
};
var myLine = new Chart(document.getElementById("canvas").getContext("2d"),{
   data:lineChartData,
  type:"line",
  options:options
});
    }

      </script> 
   </head> 
   <body> 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>  
      <canvas id="canvas" height="450" width="600"></canvas> 
      <img id="url">   
   </body>
</html>

Related Tutorials