question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ChartJs Custom Tooltip is stucked on UI

See original GitHub issue

Hi, I have included chart.js custom tooltip using example in official docs.

Problem: If someone have mouse on chart then this custom tooltip is displayed. And while the mouse is in chart, if someone uses tabs/enter to navigate to another page, this custom tooltip function is not called. As a consequence, tooltip is not removed from UI in rest of the interactions with app. I have inspected the core library and I think it was only detecting mouseout event which in this case, didn’t call this custom tooltip hook.

Below attached is my code for tooltip configuration, and I am using react-chartjs-2 wrapper to consume chart.js Chart.js version: 2.9.1 react-chartjs-2: 2.8.0

tooltips: {
              enabled: false,
              placement: 'left',
              caretSize: 5,
              cornerRadius: 6,
              custom: function(tooltipModel) {
                let tooltipEl = document.getElementById('custom-tooltip');

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

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

                let position = this._chart.canvas.getBoundingClientRect();
                let clickPointPosition = position.left + window.pageXOffset + tooltipModel.caretX;
                // Display, position, and set styles for font
                tooltipEl.style.opacity = 0.9;
                tooltipEl.style.position = 'absolute';
                tooltipEl.style.left = (clickPointPosition < tooltipEl.offsetWidth ? clickPointPosition : clickPointPosition - tooltipEl.offsetWidth) + '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';
              }
            },

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kurklecommented, Jun 16, 2020

Reproduces with master: https://jsfiddle.net/p4cjbeg7/ (in chrome, not in firefox)

Note: had to add animation: false and var tooltipModel = this to make it work with v3.

1reaction
etimbergcommented, Jun 15, 2020

I tried this in Firefox and the tooltip was hidden when i triggered the chart hiding via the keyboard.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ChartJS: Custom tooltip always displaying - Stack Overflow
Im trying to create a custom tooltip. According to the documentation, this should be easy: var myPieChart = new Chart(ctx, { type: 'pie',...
Read more >
Tooltip - Chart.js
External tooltips allow you to hook into the tooltip rendering process so that you can render the tooltip in your own custom way....
Read more >
Tooltip for bar chart is getting hide while hovering & show axis ...
Since chart width is too small, tooltip div is cropped due to overflow: hidden property of their parent element. We suggest you to...
Read more >
Tooltip | Common Settings - AnyChart Documentation
Tooltip is a text box that is hidden by default and can be displayed when a point on a chart is hovered over....
Read more >
How to use Chart.js for Dataset Visualization? - DevTeam.Space
The best option is to use a custom solution that lets you graph and animate your content in a unique and professional way....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found