chart.js annotation plugin

by kenmasu

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.5/chartjs-plugin-annotation.min.js"></script>
<canvas id="ctx"></canvas>

JavaScript

var marketing = ['2017-08-05', '2017-08-12'];
var amount = [50, 70];
// populate 'annotations' array dynamically based on 'marketing'
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]
      },
      click: function(event) {
        console.log("###click", event);
      },
   }
});

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: {
      title: {
        display: true,
        text: "test title",
      },
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true
            }
         }]
      },
      annotation: {
         drawTime: 'afterDatasetsDraw',
         annotations: annotations
      }
   }
});