ChartJs to show both of the dataset legends when i hover to one of the points - Javascript Chart.js

Javascript examples for Chart.js:Legend

Description

ChartJs to show both of the dataset legends when i hover to one of the points

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <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  ww.  j a v  a 2 s  .  co m*/
var ctx = document.getElementById("myChart");
  var myChart = new Chart(ctx, {
      type: 'line',
      data: {
          labels: ['A', 'B', 'C', 'D', 'E', 'F'],
          datasets: [{
              label: "Sales",
              data: [2000, 1200, 3000, 5000, 1000, 1300],
              pointHoverBackgroundColor:  'rgb(255, 99, 132)',
              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
          },
          {
              label: "gross sales",
              data: [1500, 2200, 3000, 2800, 1122, 2345],
              pointHoverBackgroundColor:  'rgb(66, 133, 244)',
              backgroundColor: [
                  'rgb(66, 133, 244, 0.2)',
              ],
              borderColor: [
                  'rgb(66, 133, 244)',
              ],
              borderWidth: 1
          }]
      },
      options: {
        title:{
          display: "true",
          text: "Sales Reports",
          fontSize: "22"
        },
        tooltips: {
            mode: 'index',
            intersect: false
        },
        hover: {
            mode: 'nearest',
            intersect: true
        },
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
      }
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="myChart" style="height:800px; width:800px"></canvas>  
   </body>
</html>

Related Tutorials