I've been consuming the Chart JS documentation but I think this is probably more computer science / math based of a question. I'm trying to draw a line that extends from the bottom of my chart to the top of the highest data point. Here's a link to a working code example: https://stackblitz.com/edit/react-pvzbwc
The idea is for the chart to look something like this where the point ends exactly at the top of the data: https://i.stack.imgur.com/L8d0H.jpg
Here's what I have so far for my after draw hook:
// draw a line when someone hovers over a data point
afterDatasetDraw: (chart) => {
// console.log(chart)
if (chart.tooltip._active && chart.tooltip._active.length) {
const activePoint = chart.tooltip._active[0];
console.log(activePoint.tooltipPosition())
const ctx = chart.ctx;
const y_axis = chart.scales['y-axis-0'];
const x = activePoint.tooltipPosition().x;
const yData = activePoint._chart.config.data.datasets[activePoint._datasetIndex].data[activePoint._index].y;
const topY = y_axis.top;
const bottomY = y_axis.bottom;
// draw line
ctx.save();
ctx.beginPath();
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.lineWidth = 2;
ctx.strokeStyle = '#000';
ctx.stroke();
ctx.restore();
}
}
It works pretty well when I need to draw a line straight up through the top, but not when I want just to see it at the top of the highest datapoint. What I've noticed is that topY is a static unchanging number. I'm wondering if there's a way I can calculated the top position based on the cartesian points of the chart?
Any insight is definitely appreciated.