Get ChartJS instance after initialization - Javascript Chart.js

Javascript examples for Chart.js:Chart Configuration

Description

Get ChartJS instance after initialization

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>line chart with ChartJS</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://rawgit.com/nnnick/Chart.js/v1.0.2/Chart.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){/*from  ww  w  .  j  a  v a  2  s  . co  m*/
var data = {
  labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"],
  datasets: [
    {
      label: "My First dataset",
      fillColor: "rgba(220,220,220,0.2)",
      strokeColor: "rgba(220,220,220,1)",
      pointColor: "rgba(220,220,220,1)",
      pointStrokeColor: "#fff",
      pointHighlightFill: "#fff",
      pointHighlightStroke: "rgba(220,220,220,1)",
      data: [5, 5, 9, 81, 56, 55, 40]
    }
  ]
};
var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx).Line(data);
var data2 = {
  labels: ["Eating2", "Drinking2", "Sleeping2", "Designing2", "Coding2", "Cycling2", "Running2"],
  datasets: [
    {
      label: "My First dataset",
      fillColor: "rgba(220,220,220,0.2)",
      strokeColor: "rgba(220,220,220,1)",
      pointColor: "rgba(220,220,220,1)",
      pointStrokeColor: "#fff",
      pointHighlightFill: "#fff",
      pointHighlightStroke: "rgba(220,220,220,1)",
      data: [15, 15, 19, 181, 156, 155, 140]
    }
  ]
};
var ctx = document.getElementById("myChart2").getContext("2d");
var myChart = new Chart(ctx).Line(data2);
Chart.helpers.each(Chart.instances, function(instance){
  console.log(instance.chart.canvas.id)
})
    }

      </script> 
   </head> 
   <body> 
      <canvas id="myChart" height="300" width="800"></canvas> 
      <canvas id="myChart2" height="300" width="800"></canvas>  
   </body>
</html>

Related Tutorials