ChartJS to draw multiple vertical lines using an array - Javascript Chart.js

Javascript examples for Chart.js:Line Chart

Description

ChartJS to draw multiple vertical lines using an array

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.0/Chart.min.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.5/chartjs-plugin-annotation.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){/*from  w  ww.j av  a2  s.c o m*/
var marketing = ['2017-08-05', '2017-08-12'];
var amount = [50, 70];
var annotations = marketing.map(function(date, index) {
   return {
      type: 'line',
      id: 'vline' + index,
      mode: 'vertical',
      scaleID: 'x-axis-0',
      value: date,
      borderColor: 'green',
      borderWidth: 1,
      label: {
         enabled: true,
         position: "center",
         content: amount[index]
      }
   }
});
var chart = new Chart(ctx, {
   type: 'line',
   data: {
      labels: ['2017-08-02', '2017-08-05', '2017-08-09', '2017-08-12', '2017-08-14'],
      datasets: [{
         label: 'LINE',
         data: [3, 1, 4, 2, 5],
         backgroundColor: 'rgba(0, 119, 290, 0.2)',
         borderColor: 'rgba(0, 119, 290, 0.6)'
      }]
   },
   options: {
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true
            }
         }]
      },
      annotation: {
         drawTime: 'afterDatasetsDraw',
         annotations: annotations
      }
   }
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="ctx"></canvas>  
   </body>
</html>

Related Tutorials