score:24

Accepted answer

there is no native functionality for this yet, rather you would have to create a chart plugin to accomplish so.

ᴘʟᴜɢɪɴ (ᴅʀᴀᴡ ᴠᴇʀᴛɪᴄᴀʟ ʟɪɴᴇ ᴏɴ ᴍᴏᴜꜱᴇᴏᴠᴇʀ ᴀᴛ ᴅᴀᴛᴀ-ᴘᴏɪɴᴛ) :

chart.plugins.register({
   afterdatasetsdraw: function(chart) {
      if (chart.tooltip._active && chart.tooltip._active.length) {
         var activepoint = chart.tooltip._active[0],
             ctx = chart.ctx,
             y_axis = chart.scales['y-axis-0'],
             x = activepoint.tooltipposition().x,
             topy = y_axis.top,
             bottomy = y_axis.bottom;
         // draw line
         ctx.save();
         ctx.beginpath();
         ctx.moveto(x, topy);
         ctx.lineto(x, bottomy);
         ctx.linewidth = 2;
         ctx.strokestyle = '#07c';
         ctx.stroke();
         ctx.restore();
      }
   }
});

* add this plugin at the beginning of your script

ᴅᴇᴍᴏ ⧩

chart.plugins.register({
   afterdatasetsdraw: function(chart) {
      if (chart.tooltip._active && chart.tooltip._active.length) {
         var activepoint = chart.tooltip._active[0],
            ctx = chart.ctx,
            y_axis = chart.scales['y-axis-0'],
            x = activepoint.tooltipposition().x,
            topy = y_axis.top,
            bottomy = y_axis.bottom;
         // draw line
         ctx.save();
         ctx.beginpath();
         ctx.moveto(x, topy);
         ctx.lineto(x, bottomy);
         ctx.linewidth = 2;
         ctx.strokestyle = '#07c';
         ctx.stroke();
         ctx.restore();
      }
   }
});

var chart = new chart(ctx, {
   type: 'line',
   data: {
      labels: ['jan', 'feb', 'mar', 'apr', 'may'],
      datasets: [{
         label: 'line',
         data: [3, 1, 4, 2, 5],
         backgroundcolor: 'rgba(0, 119, 290, 0.2)',
         bordercolor: 'rgba(0, 119, 290, 0.6)',
         fill: false
      }]
   },
   options: {
      responsive: false,
      scales: {
         yaxes: [{
            ticks: {
               beginatzero: true
            }
         }]
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.6.0/chart.min.js"></script>
<canvas id="ctx" height="200"></canvas>

score:0

even though there is an accepted answer, i thought i might contribute a plugin i wrote which specifically addresses this question.

the chartjs line height annotation plugin

https://www.npmjs.com/package/chartjs-plugin-lineheight-annotation

there are a few exportable classes which can help you calculate to the top of the datapoint if you need. additionally, there is a simple api which you can add to your chart's options object

/// default values
lineheightannotation: {
  // defaults to have line to the highest data point on every tick
  always: true,
  // optionally, only have line draw to the highest datapoint nearest the user's hover position
  hover: false,
  // colors of the line
  color: '#000',
  // name of yaxis
  yaxis: 'y-axis-0',
  // weight of the line
  lineweight: 1.5,
   /// sets shadow for all lines on the canvas
  shadow: {
    // color of the shadow
    color: 'rgba(0,0,0,0.35)',
    // blur of the shadow
    blur: 10,
    /// shadow offset
    offset: {
      // x offset
      x: 0,
      // y offset
      y: 3
    }
  },
  // dash defaults at [10, 10]
  nodash: true,
}

Related Query

More Query from same tag