ChartJS to Get value from another another Chart - Javascript Chart.js

Javascript examples for Chart.js:Chart Data

Description

ChartJS to Get value from another another Chart

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>ChartJS Click Events</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//from  w w  w.ja v a 2 s .c  o  m
$(document).ready(function() {
  var myChart1Canvas = document.getElementById("myChart1");
  var myChart1Context = myChart1Canvas.getContext("2d");
  var myChart = new Chart(myChart1Context, {
    type: 'bar',
    data: {
      labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
      datasets: [{
        label: 'Shirts',
        data: [10, 10, 15, 5, 2, 3],
        backgroundColor: [
          'rgba(255, 99, 132, 0.2)',
          'rgba(54, 162, 235, 0.2)',
          'rgba(255, 206, 86, 0.2)',
          'rgba(75, 192, 192, 0.2)',
          'rgba(153, 102, 255, 0.2)',
          'rgba(255, 159, 64, 0.2)'
        ],
        borderColor: [
          'rgba(255,99,132,1)',
          'rgba(54, 162, 235, 1)',
          'rgba(255, 206, 86, 1)',
          'rgba(75, 192, 192, 1)',
          'rgba(153, 102, 255, 1)',
          'rgba(255, 159, 64, 1)'
        ],
        borderWidth: 1
      }]
    },
    options: {
      scales: {
        yAxes: [{
          ticks: {
            beginAtZero: true
          }
        }]
      },
    }
  });
  myChart1Canvas.onclick = function(evt) {
    var activePoints = myChart.getElementsAtEvent(evt);
    if (activePoints[0]) {
      var chartData = activePoints[0]['_chart'].config.data;
      // chartData will be an object in the format {Labels: [], datasets: Array}}
      console.log(chartData);
    }
  };
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="myChart1"></canvas> 
      <canvas id="myChart2"></canvas>  
   </body>
</html>

Related Tutorials