Chartjs-ChartJS โ€“ line graph, position tooltip

1๐Ÿ‘

โœ…

You can also define a custom position function for tooltips. like this,

Chart.Tooltip.positioners.custom = function(elements, eventPosition) {
var x = eventPosition.x;
var y = eventPosition.y;
var minDistance = Number.POSITIVE_INFINITY;
var i, len, nearestElement;
var top_element = elements[0];
for (i = 0, len = elements.length; i < len; ++i) {
    console.log(elements[i].tooltipPosition().y);
    if (elements[i].tooltipPosition().y < top_element.tooltipPosition().y) {
        top_element = elements[i];
    }
}
var tp = top_element.tooltipPosition();
x = tp.x;
y = tp.y;

return {
    x: x,
    y: y
};

};

Once done, you can specify it in tooltips.options.

More information :

https://www.chartjs.org/docs/latest/configuration/tooltip.html#position-modes

Hope it helps!

Leave a comment