I can't figure out how I might draw a vertical line on a line chart at a data point when hovering over it using Chart JS. I would like the line to stay within the bounds of the chart rectangle so that it does not extend into the tick area. Any help would be appreciated!
Asked
Active
Viewed 6,857 times
2 Answers
24
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>

ɢʀᴜɴᴛ
- 32,025
- 15
- 116
- 110
-
Exactly what I was looking for. Thanks! – Timothy Barmann Aug 21 '17 at 23:13
-
1This is awesome. Can it also be done on click event as this is on hover / tooltip action? – vikash.gupta Mar 01 '18 at 05:05
-
thanks, this is awesome, everyone is trying to over complicate it but this is simple. Thanks again – Laith Ekermawi Jul 06 '20 at 14:21
-
1Thanks for the answer. I wanted a dashed line so I added `ctx.setLineDash([3, 3]);` after `ctx.beginPath();`. – Kalimah Dec 31 '20 at 16:18
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,
}

Sean Kelly
- 901
- 7
- 27