Get date label of line Chart - Javascript Chart.js

Javascript examples for Chart.js:Line Chart

Description

Get date label of line Chart

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Line Chart ChartJS </title> 
      <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   www  . j a  va2  s . c  om
var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [
       {
         label: '# of Votes',
         data: [12, 19, 3, 5, 2, 3],
         borderWidth: 1
       },
         {
            label: '# of Points',
            data: [7, 11, 5, 8, 3, 7],
            borderWidth: 1
         }
      ]
  },
  options: {
     scales: {
       yAxes: [{
        ticks: {
               reverse: false
        }
      }]
    }
  }
};
var lineCanvas = document.getElementById("chartJSContainer");
var ctx = lineCanvas.getContext('2d');
var lineChart = new Chart(ctx, options);
lineCanvas.onclick = function (e) {
   var firstPoint  = lineChart.getElementAtEvent(e)[0];
   if (firstPoint) {
     var first_point_index= firstPoint._index
     var firstPoint_dataset_index= firstPoint._datasetIndex
     var label = lineChart.data.labels[firstPoint._index];
     var value = lineChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
   }
}
    }

      </script> 
   </head> 
   <body>  
      <canvas id="chartJSContainer" width="600" height="400"></canvas>   
   </body>
</html>

Related Tutorials