Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link in ChartJS tooltip not clickable

I've added an <a> tag to my custom tooltip, but when I click on it it doesn't redirect to the href.

I copied the custom tooltip exactly from these docs. I'll also paste the code below for ease.

Then, I added the following innerHTML:

var tableRoot = tooltipEl.querySelector("table");
    tableRoot.innerHTML = innerHtml + '<a href="www.google.com">Click Here</a>';

However, the link isn't clickable - it doesn't even trigger cursor: pointer like a normal <a> tag.

The full custom tooltip code:

custom: function(tooltipModel) {
            // Tooltip Element
            var tooltipEl = document.getElementById('chartjs-tooltip');

            // Create element on first render
            if (!tooltipEl) {
                tooltipEl = document.createElement('div');
                tooltipEl.id = 'chartjs-tooltip';
                tooltipEl.innerHTML = '<table></table>';
                document.body.appendChild(tooltipEl);
            }

            // Hide if no tooltip
            if (tooltipModel.opacity === 0) {
                tooltipEl.style.opacity = 0;
                return;
            }

            // Set caret Position
            tooltipEl.classList.remove('above', 'below', 'no-transform');
            if (tooltipModel.yAlign) {
                tooltipEl.classList.add(tooltipModel.yAlign);
            } else {
                tooltipEl.classList.add('no-transform');
            }

            function getBody(bodyItem) {
                return bodyItem.lines;
            }

            // Set Text
            if (tooltipModel.body) {
                var titleLines = tooltipModel.title || [];
                var bodyLines = tooltipModel.body.map(getBody);

                var innerHtml = '<thead>';

                titleLines.forEach(function(title) {
                    innerHtml += '<tr><th>' + title + '</th></tr>';
                });
                innerHtml += '</thead><tbody>';

                bodyLines.forEach(function(body, i) {
                    var colors = tooltipModel.labelColors[i];
                    var style = 'background:' + colors.backgroundColor;
                    style += '; border-color:' + colors.borderColor;
                    style += '; border-width: 2px';
                    var span = '<span style="' + style + '"></span>';
                    innerHtml += '<tr><td>' + span + body + '</td></tr>';
                });
                innerHtml += '</tbody>';

                var tableRoot = tooltipEl.querySelector('table');
                tableRoot.innerHTML = innerHtml;
            }

            // `this` will be the overall tooltip
            var position = this._chart.canvas.getBoundingClientRect();

            // Display, position, and set styles for font
            tooltipEl.style.opacity = 1;
            tooltipEl.style.position = 'absolute';
            tooltipEl.style.left = position.left + window.pageXOffset + tooltipModel.caretX + 'px';
            tooltipEl.style.top = position.top + window.pageYOffset + tooltipModel.caretY + 'px';
            tooltipEl.style.fontFamily = tooltipModel._bodyFontFamily;
            tooltipEl.style.fontSize = tooltipModel.bodyFontSize + 'px';
            tooltipEl.style.fontStyle = tooltipModel._bodyFontStyle;
            tooltipEl.style.padding = tooltipModel.yPadding + 'px ' + tooltipModel.xPadding + 'px';
            tooltipEl.style.pointerEvents = 'none';
        }
like image 329
crevulus Avatar asked Jun 09 '26 00:06

crevulus


2 Answers

I've faced the very problem myself, so I tried to remove this line:

tooltipEl.style.pointerEvents = 'none';

but it didn't work as expected - every time I hovered over the tooltip it disappeared.

Then, I tried this:

  1. Keep the line above just as it is;

  2. Modify events prop of the options. (See docs). By doing this, you actually remove 'mouseout' event from the default config.

    const options = {
             maintainAspectRatio: false,
             events: ['mousemove', 'click', 'touchstart', 'touchmove'],
             plugins: {
                 tooltip: {
                     enabled: false,
                     external: externalTooltipHandler
                 },
                 legend: {
                     display: false
                 }
             },
         }
    
  3. Then, remove pointer-events from the sole clickable element of the tooltip. In my case, it is li element. I did this with CSS. li { pointer-events: auto; }

When 1-3 steps are completed, the external tooltip is supposed to behave as expected AND the link elements contained in that tooltip should be clickable.

like image 167
Kit Avatar answered Jun 10 '26 14:06

Kit


Remove

        tooltipEl.style.pointerEvents = 'none';

and any of theese from CSS

        pointer-events: none;
like image 30
Stig Avatar answered Jun 10 '26 12:06

Stig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!