doughnut hover event handler - Javascript Chart.js

Javascript examples for Chart.js:Doughnut Chart

Description

doughnut hover event handler

Demo Code

ResultView the demo in separate window

<html lang="en">
   <head> 
      <title>5. Pie Chart</title> 
      <style>

.container {/* ww w.j  ava2 s  .co m*/
   width: 80%;
   margin: 15px auto;
}


      </style> 
   </head> 
   <body translate="no"> 
      <div class="container"> 
         <h2>Chart.js ? Pie Chart Demo (apples)</h2> 
         <div> 
            <canvas id="myChart"></canvas> 
         </div> 
      </div> 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.min.js"></script> 
      <script>
      var currentIndex = -1;
Chart.defaults.global.hover.onHover = function(x) {
  if(x[0]) {
    var index = x[0]._index;
    if (index !== currentIndex) {
        currentIndex = index;
        console.log(x[0]._model.label + ': ' + x[0]._chart.config.data.datasets[0].data[index]);
    }
  }
};
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
  type: 'pie',
  data: {
    labels: ["M", "T", "W", "T", "F", "S", "S"],
    datasets: [{
      backgroundColor: [
        "#2ecc71",
        "#3498db",
        "#95a5a6",
        "#9b59b6",
        "#f1c40f",
        "#e74c3c",
        "#34495e"
      ],
      data: [12, 19, 3, 17, 28, 24, 7]
    }]
  }
});
      //# sourceURL=pen.js
    
      </script>  
   </body>
</html>

Related Tutorials